Docker is a powerful platform that allows developers to package their applications and dependencies into lightweight, portable containers. By dockerizing your Node.js application, you can ensure that it runs consistently across various systems, simplifying deployment and reducing compatibility issues.

In this blog post, we'll take you through the process of dockerizing your Node.js application, making it easier to deploy, manage, and scale across different environments.


Getting Started

  • In your terminal, create your Node JS file using npm init -y it will create the package.json file for you.
 "name": "ghcr",
 "version": "1.0.0",
 "description": "",
 "main": "index.js",
 "scripts": {
   "start": "node index.js"
 "keywords": [],
 "author": "",
 "license": "ISC"
  • Next, create your index.js file and paste the following code or you can work on your own express js configurations for your application. An index.js file is a common entry point or the main file in a JavaScript or Node.js application or module. When a module is imported or required in another file, the JavaScript runtime (such as Node.js) looks for the index.js file in the module's directory by default. This file typically contains the core logic of the application or exports the module's public API.
const express = require('express')
const app = express()
const port = 3000

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

app.listen(port, () => {
 console.log(`Example app listening on port ${port}`)
  • In your directory, delete your node modules folder as you don’t need them anymore.

Create and Build a Dockerfile

  • Create your Dockerfile, and configure it for your Express JS application needs before we create our Dockerfile, create an account, and sign in to Docker Hub.
  • Next, log in to your DockerHub account using the docker login command in your terminal.
  • Write and configure your Dockerfile with the following configurations. A Dockerfile is a script containing a set of instructions used to create a Docker image for a specific application or service. It defines the environment, dependencies, and configurations required to run the application inside a Docker container. A Dockerfile is essentially a blueprint for building our Node image, which can then be run as a container on any system with Docker installed. A Dockerfile typically consists of various instructions that define the base image for our Node app, install dependencies, copy application files, set environment variables, expose ports, and specify the entry point or command to run the application.
FROM node:slim

# copy from your current directory to your destination which is /app

COPY . /app

# after that go ahead and run some commands

RUN npm install


CMD node index.js
  • Next, build your image but tag it with your DockerHub name and image name using the command docker build -t <username>/<image_name>:0.0.1 .

The docker build -t command is used to build a Docker image from a Dockerfile and assign a name (and optionally a tag) to the created image. The -t flag stands for tag and allows you to specify the name and tag in the format name:tag. If you don't provide a tag, Docker will assign the default latest tag to the image.

  • Check to see your Docker image using the command docker images. You’ll see your Node image details in your terminal.
  • Next, access your Node App in your browser using the port you built your image on.
  • Next, after building your image, push it to the Docker registry in DockerHub using the command docker push <dockerhub name>/<docker_image>tag
  • Back in DockerHub, you can see your image and your latest tag.

Pushing Docker image to GitHub Container Registry

  • Create a Personal Access Token (PAT) on GitHub:
  • Go to your GitHub account settings. Click on Developer settings in the left sidebar, click on Personal access tokens and then Generate new token.
  • Give your token a descriptive name, and select the following scopes: read:packages, write:packages, and delete:packages. You might also need to select the repo scope if your image is private.
  • Click Generate token and copy the generated token. Store it securely, as you won't be able to see it again.

  • Next, log in to GHCR using the Docker CLI, open your terminal, and run the following command. docker login -u YOUR_GITHUB_USERNAME -p YOUR_PERSONAL_ACCESS_TOKEN replace YOUR_GITHUB_USERNAME with your GitHub username, and YOUR_PERSONAL_ACCESS_TOKEN with the token you generated earlier.
  • Build your Docker image if you haven’t already using the Docker command docker build -t .

Replace OWNER with your GitHub username or organization name, IMAGE_NAME with the desired name for your image, and TAG with the desired version or tag for your image. For example:

  • Next, push the Docker image to GHCR with the docker push command: docker push Replace OWNER, IMAGE_NAME, and TAG with the same values you used earlier.

After the push is complete, your Docker image will be available on GHCR under your account or organization. You can manage and view your container images by visiting, replacing OWNER with your GitHub username or organization name.


Empower your development experience by learning how to harness the full potential of GHCR. Our open-source workflows for GHCR lets you gain valuable insights into streamlining your containerized workflow so you can optimize your project development process.