Skip to content

Instantly share code, notes, and snippets.

@bhubr
Last active June 5, 2022 07:28
Show Gist options
  • Save bhubr/541e8439c302d0b722373c25dbd5ffbe to your computer and use it in GitHub Desktop.
Save bhubr/541e8439c302d0b722373c25dbd5ffbe to your computer and use it in GitHub Desktop.
Run a dummy Node.js server in Docker

Run a dummy Node.js server in Docker

TL;DR

The command: curl -L -o- http://bit.do/node-hello-docker | bash

After it's run you can open http://localhost:3333

Why?

Sometimes you just want to spin a Docker container in order to test stuff. But without a "persistent" command (e.g. a server) it exits right away.

This Gist just provides a tiny shell script that will:

  • pull a Node image (node:16-alpine in this case)
  • download a mini-server (hello.js, from Node.js Getting Started)
  • Run this mini-server in the container
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
#!/bin/sh
curl -L -o- https://gist.github.com/bhubr/541e8439c302d0b722373c25dbd5ffbe/raw/049ac3dcc1a42d40b186ba290d4ee02ad1c252fe/script-run.sh | bash
#!/bin/sh
if [ ! -d /tmp/node-hello ]; then
mkdir -p /tmp/node-hello;
fi
curl -L -o /tmp/node-hello/hello.js https://gist.github.com/bhubr/541e8439c302d0b722373c25dbd5ffbe/raw/062a5244276f2e16f4a08492e889701ed310f092/hello.js
docker pull node:16-alpine
docker run -d -p 3333:3000 -v /tmp/node-hello:/app node:16-alpine node /app/hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment