Dockerize a node app in 2 minutes

For the following I’m using a project of mine : Explorer. It’s a directory listing tool. To run it only needs a configuration file.

The Dockerfile

Using node:onbuild it can’t be easier:

FROM node:onbuild
MAINTAINER "Someone"
ENV EXPLORER_CONFIG="/opt/explorer"
RUN npm rebuild -q

The http server runs on 4859 and 6859 by default. I’m using an environnement variable to set the container’s configuration path.

1. Build the container

docker build -t explorer .

2. Run it

Interactive
docker run -p 8080:4859 -it --rm -v $(pwd)/doc/examples:/opt/explorer --name explorer explorer

Here the shared configuration will be ./doc/examples/config.yml. I’m also forwarding 8080 to 4859 which is the default http port.

My node app can take it’s default configuration if none is given, so I can also start the container without volumes:

docker run -p 8080:4859 -it --rm --name explorer explorer
Daemon
docker run -p 80:4859 -p 443:6859 --name explorer -d \
-v /home/me/.config/explorer:/opt/explorer \
-v /home/me/:/home/explorer explorer

Forwarding http and https default ports. The config is now located in ~/home/me/.config/explorer and mounting /home/me will allow me to use /home/explorer in my container.