Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Build and Run a Node.js App with Docker

Install Docker on Ubuntu, write a small Express app, put it in a container and run it.

VPS and Cloud Servers2 min read25 screenshots
A Node.js app in a Docker container

Part 1: Install Docker on Ubuntu

Step 1: Update the package list

sudo apt update
Updating the repositories

Step 2: Install the helper packages

sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
Installing the requirements

Step 3: Add the Docker GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Adding the Docker key

Step 4: Add the Docker repository

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Adding the repository

Step 5: Install Docker Engine

sudo apt update
Updating the package list
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
Installing Docker
  • docker-ce is the Docker engine.
  • docker-ce-cli is the command-line tool.
  • containerd.io is the container runtime.
  • docker-compose-plugin adds Docker Compose.

Step 6: Test it

sudo docker run hello-world
Checking the Docker installation

Step 7 (optional): Use Docker without sudo

sudo usermod -aG docker ${USER}
Adding the user to the docker group
newgrp docker
Starting a shell with the new group

Part 2: Create a simple Node.js app

Install Node.js and npm first, if you have not done so.

Step 1: Make a project folder

mkdir nodejs-docker-app && cd nodejs-docker-app
Creating the project folder

Step 2: Start a Node.js project

npm init -y
Running npm init

Step 3: Install Express

npm install express
Installing Express

Step 4: Create the app file

touch app.js
Creating app.js

Step 5: Add the code

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
  res.send('Hello from Node.js in Docker on Ubuntu 24.04!');
});
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});
The code in app.js

Step 6: Test it on your machine

node app.js
Running the app locally

Open http://localhost:3000. You see the hello message.

The message in the browser

Part 3: Put the app in a container

Step 1: Create a Dockerfile

touch Dockerfile
Creating the Dockerfile

Step 2: Add the content

FROM node:20-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
The Dockerfile content

Step 3: Create a .dockerignore file

touch .dockerignore
Creating .dockerignore

Step 4: List what to leave out

node_modules
npm-debug.log
Dockerfile
.dockerignore
.git
.gitignore
The .dockerignore entries

Part 4: Build and run

Step 1: Build the image

docker build -t nodejs-app .
Building the image

Step 2: Run the container

docker run -p 3000:3000 -d --name my-nodejs-app nodejs-app
Running the container

Step 3: Check that it runs

docker ps
The running container

Step 4: Test the app

Open http://localhost:3000 (or http://SERVER_IP:3000). The same message shows up, now served from the container.

The containerized app in the browser

Quick recap

  • Install Docker from its official repository.
  • A Dockerfile copies your app and starts it with node app.js.
  • docker build makes the image and docker run -p 3000:3000 starts it.