SvelteKit
GitHub
Back

Dockerize a Node API

What is docker?

To keep it simple, Docker is an open-platform used for developing, running, and deploying applications in a containerized environment. Well what is a container you might ask. You can think of a container as a micro operating system that can be spun up or down at whim. The container can host media, applications, utilities, or whatever else a typical operating system can run. Multiple containers can be run simultaneously, and via a network-bridge they can even communicate with each other and the host operating system as well! In theory docker is much more, but to avoid being esoteric this is the general idea of Docker and containers.

How can I use docker?

Docker can be used in many ways, but to keep it simple we'll focus on using it for creating application images that can be made portable to host virtually anywhere!

The docker CLI tool is the best way of getting started with becoming familiar with docker and it's abilities. All documentation can be found at docker's website here.

Again this article is less about getting familiar with docker and more about using it for a controlled deployment cycle, so I'll skip the deep explanation and focus primarily on the use case. I will assume if you're reading this you have docker installed and have used it at least a handful of times.

Creating a simple application

We'll start of by putting together a fairly normal application. In this case we'll use JS for simplicity and create an API that returns a health check ( status 200 "ok" ).

I have my setup in the directory /dev/health-check-api. My index.js file is fairly plan but does the trick:

var express = require('express');
var app = express();

app.get('/health', (req, res, next) => {
    res.json({ message: 'ok' });
});

app.listen(5000, () => {
    console.log('Server running on port: 5000');
});

That's it! You can run the app to test it out ( you'll need to install express with npm, yarn, or pnpm first ). In theory it should return {"message": "ok"} with a status code of 200 every time we visit localhost:5000/health.

Time to dockerize

Now we can actually build our app inside a docker container. To do so you should have docker cli installed. Aside from the the first step is creating a docker file at the root of your project. touch Dockerfile.

The Dockerfile should look like this:

# We'll be using node alpine linux for this containers OS
FROM node:alpine as builder

# Change into the /app directory
WORKDIR /app

# Set our node environment
ENV NODE_ENV production

# Copy our package.json + lock and run npm install to build our deps
COPY ["package.json", "package-lock.json*", "./"]
RUN npm install --production

# Copy everything that was build into the root directory
COPY . .

# Run the app calling node on our index file
CMD [ "node", "index.js" ]

With this in place we can hit the last step of actually building an image using docker!

Build our docker image

I know i didn't clarify to well what an 'image' is. But just think of it as a pre-built container that we can save, commit, move, run, etc.. Now that we have everything we need let's put it all together. Using Docker CLI we can run a simple command to actually run the build.

docker build --tag node-api . - You should see a progress screen of the container being built and ultimately a success message.

You can view all of your images by running docker images which will output a list of images including your node-api image.

To run the image we can tell docker to simply run our image using: docker run node-api. You should see some output about the container starting up. Once it's built we can visit localhost:5000/health and see our output! That's it! You build a docker image and ran it as a container. This image can be sent to a container registry or run directly on a server alongside other docker containers!

Summary

Today we've put together quite a bit of development magic. We created a simple Node API, created a Docker image and ran it as an independent container. This is quite the feat! There's A LOT more that can be done with docker such as:

  • Connecting multiple containers over a network bridge
  • Orchestrating docker containers using Kubernetes
  • Building a CI/CD flow to build the images, tag them and run them automatically

Let me know if this article helped you out, or if there are any ways I could improve. Thanks!