A Docker image for running Node.js, based on Alpine Linux. This image belongs to a suite of images documented here.
This image features:
6.7.0
,latest
(Dockerfile)6.6.5
(Dockerfile)6.6.4
(Dockerfile)6.6.3
(Dockerfile)6.6.2
(Dockerfile)6.6.1
(Dockerfile)6.6.0
(Dockerfile)6.5.0
(Dockerfile)6.4.0
(Dockerfile)6.3.0
(Dockerfile)6.2.0
(Dockerfile)6.1.0
(Dockerfile)6.0.1
(Dockerfile)6.0.0
(Dockerfile)5.2.2
(Dockerfile)5.2.1
(Dockerfile)5.2.0
(Dockerfile)5.1.0
(Dockerfile)5.0.0
(Dockerfile)4.0.0
(Dockerfile)3.1.0
(Dockerfile)3.0.1
(Dockerfile)3.0.0
(Dockerfile)2.1.0
(Dockerfile)2.0.0
(Dockerfile)1.0.0
(Dockerfile)
See VERSIONS.md for image contents. See NODEJS.md to find specific Node.js versions.
To use this image include FROM smebberson/alpine-nodejs
at the top of your Dockerfile
. Inheriting from smebberson/alpine-nodejs
provides you with the ability to easily start your node.js application using s6. You have two options for process management:
- s6 can keep it running for you, restarting it when it crashes.
- The entire container can exit allowing the host machine to kick in and restart it.
To start your app, with automatic restarts:
- create a folder at
/etc/services.d/app
(or name it whatever you like, just make sure it lives within/etc/services.d
) - create a file in your new folder called
run
and give it execute permissions - inside that file start start your node.js application, for example:
#!/usr/bin/with-contenv sh
# cd into our directory
cd /app
# start our node.js application
node server.js;
When you run this container, s6 will automatically start your application and make sure it stays running for you.
You'll also need to make sure you go through the usual process of adding your node.js source, installing your packages with npm
and exposing the necessary ports for your application (usually within your Dockerfile).
To start your app in a fashion that will exit the container when your app dies, add something like the following to your Dockerfile:
CMD ["node", "/app/server.js"]
The container will execute the command and your Node app will start running. When it suddenly dies, your container will exit so you'll need processes on your host machine to restart it (i.e. docker run --autorestart
).
The above method has the benefit of exiting the container when your Node.js app dies, but it doesn't really allow you to take advantage of s6. With s6, you can have processes that are executed after your app dies. For example, in case there is something you need to clean up before your host restarts the container. To take advantage of this, create the executable run
file at /etc/services.d/app
as described in Automatic restarts.
Now, create an executable file named finish
at /etc/services.d/app
. In this file do any clean up you need to, and then use s6 to kill the container, for example:
#!/usr/bin/with-contenv sh
# perform clean-up here
# use s6 to exit the container
s6-svscanctl -t /var/run/s6/services
You can simply remove the last line, if you want s6 to automatically restart your Node.js app again.
If you're logging to stdout (which console.log
does) then you'll be able to review your logs using the standard Docker process.
The best way to handle exceptions is to:
- Capture them.
- Log them.
- Exit the process and let s6 restart your app, or exit the container.
This will ensure that your errors aren't going un-noticed, but that your application stays up and in proper functioning order. An example of this is (although, I recommend a better logging solution):
process.on('uncaughtException', function (e) {
console.log(e);
process.exit(1);
});
An example of using this image can be found in examples/user-nodejs.