How to Dockerize a JavaScript Application

by Solomon Eseme


Updated Mon Jul 10 2023

How to Dockerize a JavaScript Application

This post is part of the "The Definitive Guide To Docker in 2023" series

How to Dockerize a JavaScript Application

In this lesson, you will learn how to Dockerize a simple JavaScript project. The project is a simple Express 5 Todo API that I have already built.

Here’s the Postman result of the API:

Docker Project ResultIf you want to follow along with creating the project, you can read through Express 5: The Ultimate Guide for the step-by-step guide or you can clone the repository from GitHub.

Create a Dockerfile

Create a Dockerfile in the root directory of the Express 5 Todo API project and add the following script:

# Create from a base image
FROM node:16.17.0-alpine

# Set working directory
WORKDIR /usr/src/app

# Copy Package.json files
COPY package*.json ./

# Install production-only dependencies
RUN npm ci --production

# Copy all files now
COPY . .

# Expose port 3000 to your host machine

# Run Npm Start command
CMD [ "npm", 'start']

Look at the comments in the script for the explanation.

After creating your Dockerfile and adding the necessary instructions to create a container, the next step is to build the image.

Build the image

We will use the docker build or docker image build command we explored above to build the image. Open your terminal and type in the following command in the directory where the Dockerfile is located:

docker build -t my-express-app .

When you enter the command, Docker will start pulling the base image and setting up your container for you. You will see a result similar to the one below if everything is successful.

Docker build command previewThe next step is to run the container that we’ve created.

Running the image

As we have already explored, you can use the Run command to run any image. Type the following command into your terminal to run your image:

docker run --name my-express-app-container -it -p 4000:3000 my-express-app

If this works, you should be greeted with a screenshot as shown below:

running container previewAs you can see, inside the container the post is 3000 but have mapped that point to 4000 in our host system. So to preview the API in the browser or access it with Postman, we will use port localhost:4000.

If everything works properly, if you visit localhost:4000you should see the result similar to the one below:

Docker project result previewWe have practically Dockerised a simple JavaScript application. We have created a Docker image using Dockerfile, build and run it into a container

Furthermore, you can practice all the commands we have listed above to master each of them because they will become handy as you build more complex applications.

Next, let’s look at how to Dockerize a full-stack application that includes a front end, a backend, and a database. This will help us understand how to Dockerize complex applications using Docker Compose.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board

Read Full "The Definitive Guide To Docker in 2023"

This is the most comprehensive guide on Docker online.

In this docker tutorial, you will learn docker from scratch to an advanced level starting from the concept of containerization to Docker Compose in Docker.

You will learn how to dockerize a JavaScript application and how to use dockerize a full-stack application using Docker Compose.


Backend Tips, Every week

Backend Tips, Every week