DEV Community

vladyslav nykoliuk
vladyslav nykoliuk

Posted on • Updated on

Dockerizing a Node.js application [2024 revised]

For this tutorial, I will assume you are starting a brand new Node.js application. If you already have a Node.js application you want to Dockerize, head over to Step 3.

*Before starting this tutorial, please make sure you have Docker downloaded.

Step 1

Initialize a new node.js app with

npm init
Enter fullscreen mode Exit fullscreen mode

This will give you a package.json file that looks something like this

// package-lock.json
  "name": "sample_app",
  "version": "1.0.0",
  "description": "My First Dockerized Codebase",
  "author": "Your Name",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  "dependencies": {
    "express": "^4.17.1"
Enter fullscreen mode Exit fullscreen mode

then run npm i (previously npm install) which will generate a package-lock.json.

Step 2

Next, we will create an app.js file

// app.js
const express = require('express');
const app = express();

const PORT = [Your-port-here];

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

app.listen(PORT, '' () => {
  console.log('Server listening on Port ${PORT}');
Enter fullscreen mode Exit fullscreen mode

Step 3

Here comes the fun part...
First, create a Dockerfile

touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

Inside the Dockerfile, you will need to define the following:

# you can find your node version with: node --version
FROM node:15

# Create app directory

# Install app dependencies
COPY package*.json ./

# Install dependecies
RUN npm install

# Bundle app
COPY . .

# Define your port

# Tell Docker how to run your app
CMD [ "node", "app.js" ]

Enter fullscreen mode Exit fullscreen mode

Step 4

Additional files are needed for Docker to successfully build your app.


# anything else you want for Docker to ignore
Enter fullscreen mode Exit fullscreen mode


version: '3.7'   # you can find yours with python --version
        image: [your-image-name]
        build: .
        command: node app.js
            - "4040:4040"
            - ./[your-image-name]/app
            - /app/node_modules
            - mongodb
        image: "mongo"
            - "27017:27017"
Enter fullscreen mode Exit fullscreen mode

Step 5

Now that we have all the files, we can build and run the container.

Build a Docker container
docker build -t [your-app-name] .

Run the Docker container
docker run -it -p 3000:3000 [your-app-name]

Congratulations πŸ₯³
If you have made it this far, you have successfully Dockerized your Node.js app.

Addition Docker Commands

docker ps
Check for running containers

docker-compose run [app-name] npm run migrate
Run migration and create table

docker pull
Pulls an image or a repository from a registry

Top comments (2)

olexandrpopov profile image
Oleksandr Popov

Recently somebody posted Docker Best Practices on There are many usefull things.

vladyslavnua profile image
vladyslav nykoliuk

Indeed it has lots of useful information. Thanks for the comment!