This is how to get a minimal docker image with just node and run a simple service.
Docker will automatically download the image for alpine-node if you do not already have it.
Use the
run command to create and start the new container.
Use
-p to publish the container port 3000 on the host at port 3000 on the IP address of the host.
Use
-it to connect interactively to the TTY so you can enter and run commands
docker run -p 3000:3000 -it mhart/alpine-node
Your command line is now inside the docker container.
Edit the code of the service.
Copy and paste the sample code below and save the file.
Run node on the service.
Open your web browser to the IP address of your host system using
:3000 to set the request port.
You should see the hello message.
Use
Ctrl+c to exit node &
Ctrl+d to exit the container.
You now have a functional container that is set up to run this simple script.
Use the
ls --all command to see the container you built.
docker container ls --all
Use the
start command to start the container up again.
Use the container ID you found from the ls command.
docker container start <CONTAINER ID>
Check that the container is running and publishing port 3000.
Use the
attach command to re-attach to the container.
Run the node command again.
var http = require('http');
var server = http.createServer( function(req, res) {
if (req.url === '/') {
res.write("Hello World!");
res.write("I have been expecting you.");
res.end();
}
if (req.url === '/snarky') {
res.write("If you code it, it will crash!");
res.end();
}
});
// Port 3000 on IPv4
server.listen(3000, "0.0.0.0");