You are here because your awesome app is ready and now it’s time to package it.

Prerequisites

  • Your app
  • Docker to build standalone images to serve your production app.

Getting Started

Dockerizing your app ensures consistency, portability, isolation, and several other benefits. If you haven’t installed Docker already, you can find the installation instructions for various operating systems in the official docs.

Let’s create a simple Node.js script that runs an HTTP server on port 3000 and when the URL / is hit, it prints Hello World!

app.js
const express = require('express');
const app = express();
const port = process.env.PORT ?? 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

We can run this app by running the following command:

node app.js

That’s it, we have our Node.js app running.

Writing the Dockerfile

Now, let’s create a Dockerfile for the above app. A Dockerfile is a set of instructions that tells Docker how to get our app working in our environment.

Dockerfile
# Use the official Node.js image with version 20 based on Alpine Linux
# Alpine images are smaller in size hence preferred
FROM node:20-alpine

# Set environment variable for the application's port
# this is used in app.js
ENV PORT=3000

# Set env to production to tell app and other tools that the app is running in production mode
# will be useful to disable debugging logs, skip few things etc.,
ENV NODE_ENV=production

# Set the working directory inside the container to /app
# this will be the root context of our app,
# we will be copying and running script from inside this folder
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package.json package-lock.json* ./

# Install dependencies specified in package-lock.json
# using npm ci for a clean install
RUN npm ci

# Copy the rest of the application files to the working directory
# while copying this respects the contents given in dockerignore
COPY . .

# Expose the application port to be accessible from outside the container
EXPOSE ${PORT}

# Start the application
CMD ["node", "app.js"]

As you can see, the Dockerfile is a set of instructions that automate the steps you would take manually to get the app running on your machine.

Though Docker supports Multi-Stage Builds, we won’t be using that here since Node.js needs all the source code and node_modules to be present while running the application. Prefer to opt-in for multistage builds whenever possible.

In real-world scenarios, you might have more than just an HTTP server. You might have other services running parallelly too, like schedulers, workers, notification services, gateways, etc,. You’ll need to create a Dockerfile for each and link them while creating the Helm charts.

You might also choose to run everything in the same container, which is mostly not recommended, since you might lose isolation, scalability, etc.

It is heavily recommended to have a .dockerignore file to ignore certain prebuilt binaries and non-project-related stuff being copied from our local to the Docker container.

Read more on .dockerignore here. An example ignore file:

.dockerignore
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.git

It is essential to ignore folders like node_modules since when installing dependencies, few libraries might build binaries depending on your platform. We don’t want them to be copied since it might replace the binaries built inside the container itself while running npm ci, causing the application to break or misbehave.

Examples for Dockerfiles for Other Languages

All these Dockerfiles are references to how you might package the application; actual steps might vary depending on the application you build and the features you use.

Building Images

To build your Docker image, use the following command in the directory containing your Dockerfile:

docker build --platform linux/amd64 -t node-app:latest .

This command builds the node-app image for platform linux/amd64 and tags it as latest. If you are locally testing your application, you can skip the platform key to build the images

# only for testing in local machine
docker build -t node-app:latest .

Running Your Docker Image

Now, we have images, its time run the image. Use the following command:

docker run \
  -it \
  --rm \
  --name node-app \
  -e PORT=3000 \
  -d \
  -p 3000:3000 \
  node-app
  • -it: enables interactivity with TTY
  • --rm: to tell the Docker Daemon to clean up the container and remove the file system after the container exits
  • --name node-app: Name of the container node-app.
  • -e PORT=3000: Sets the environment variable PORT in docker 3000.
  • -d: Runs the container in detached(background) mode. You can skip the flag to see the logs directly in your terminal window.
  • -p 3000:3000: Maps port 3000 on your host to port 3000 in the container.
  • node-app in the end is the name of the image

This command runs your Docker image, mapping port 3000 of the container to port 3000 on your local machine. Once the service is up, visit http://localhost:3000 to see the app we built running.

Hurray 🎉. Now we have package our app for production use using docker.

Next Steps

Once the docker is ready, the next steps will be