Getting Started with Docker and NodeJS

I've wanted to play with Docker for a while and, with the Docker Toolbox, this is becoming easier over time for Windows based degenerates like me.

Docker allows us to seperate services from the host operating system in small, self contained packages that are not coupled to the O/S. The analogy used across their site is of a container ship: the host O/S being the ship and each service being a "container" stored on the ship as a seperate entity.

You package your service up in an "image" which contains both the logic and a docker file to initialise the environment. The image can then be run on a Docker machine as a container completely seperately from all other docker images hosted on the O/S.

Below is a basic getting started guide showing how to run a NodeJS module on a Docker VM as a container.

Hello World

Firstly follow this guide to install the Docker Toolbox locally on Windows. You could install the native application instead from here however it doesn't support Windows 10 Home edition (I'm assuming because it needs Hyper-V support).

The Toolbox sets up Oracle VirtualBox and provides the "Docker Quickstart Terminal" which does all the heavy lifting for you in getting a container ready VM up and running.

Once it's install run the aforementioned Terminal; this will take a while as it downloads an ISO and gets everything started.

Once finished; in ther terminal run:

docker run hello-world

This downloads the hello world image (hosted on Docker Hub here) and runs it on the Docker VM.

Building a NodeJS Image

Below I'm using this guide to create a NodeJS image for a very basic module.

In the root of your NodeJS module add a new file called "Dockerfile".

Paste in these instructions:

# Import the official NodeJS image; which sets up node for us in the container
FROM node:argon

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

# Run a command with arguments
CMD [ "node", "test-run.js" ]

This image will then:

  • Setup NodeJS in the container using the node:argon image
  • Copy your app over to the /usr/src/app directory
  • Install any dependencies through npm
  • Start the test-run.js script in node

Now we need to build the image so it's ready for deployment. To do this run this in the root of your NodeJS project:

docker build -t <your-username>/my-app .

The -t flag simply tags the build so we can find it again.

The operation will tell you if it was able to process each command correctly.

If you then run docker images it should appear in the list.

Running a NodeJS Image

Simply run:

docker run -d <your-username>/my-app

We don't have to define the VM to run against here as the "Docker Quickstart Terminal" sets a default for us.

Running this will output a long id like this:


This is you container id.

Debugging a NodeJS container

You can see all running containers and their ids using: docker ps.

You can get the output from the docker container using docker logs <container-id>.

If the NodeJS module you used exits automatically (process.exit()), the container will stop as well. You can still get the logs however

That is literally it

I'm honestly surprised at how easy it was to get started with Docker. The toolbox handles the heavy lifting to such a degree that hosting new containers is a doddle.