Skip to main content

Command Palette

Search for a command to run...

How to Write a Dockerfile for a Node.js App (Step-by-Step with Example)

Updated
3 min read
How to Write a Dockerfile for a Node.js App (Step-by-Step with Example)
M

👋 Hi there! I'm a DevOps enthusiast with a deep passion for all things Cloud Native. I thrive on learning and exploring new technologies, always eager to expand my knowledge and skills. Let's connect, collaborate, and grow together as we navigate the ever-evolving tech landscape! SKILLS: 🔹 Languages & Runtimes: Python, Shell Scripting, YAML 🔹 Cloud Technologies: AWS, Microsoft Azure, GCP 🔹 Infrastructure Tools: Docker, Terraform, AWS CloudFormation 🔹 Other Tools: Linux, Git and GitHub, Jenkins, Docker, Kubernetes, Ansible, Prometheus, Grafana

Want to deploy your Node.js app with Docker? You need a Dockerfile! This blog explains how to create a simple, production-ready Dockerfile to containerize your Node.js project.

Dockerfile for a Node.js application

you can use any text editor of your choice. Below is an example of how to create a simple Dockerfile using the nano text editor:

  1. Open a terminal.

  2. Navigate to the directory where you want to create the Dockerfile.

  3. Type nano Dockerfile and press Enter. This will open the nano text editor with a new file named Dockerfile.

  4. Enter the Dockerfile contents according to your requirements.

  5. Once you’re done editing, press Ctrl + X to exit nano. It will prompt you to save the changes. Press Y to confirm saving, and then press Enter to save the file with the name Dockerfile.

Here’s an example of a simple Dockerfile for a Node.js application:

# Use an official Node.js runtime as a parent image
FROM node:14-alpine

# Set the working directory in the container
WORKDIR /usr/src/app

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

# Install dependencies
RUN npm install

# Copy the rest of the application source code to the container
COPY . .

# Expose port 3000 to the outside world
EXPOSE 3000

# Define environment variable
ENV NODE_ENV=production

# Command to run the application
CMD ["node", "app.js"]

This Dockerfile:

  • Uses the official Node.js Docker image with version 14 based on Alpine Linux for a smaller image size.

  • Sets the working directory inside the container to /usr/src/app.

  • Copies package.json and package-lock.json files from the host to the container.

  • We run npm install to install dependencies defined in package.json.

  • Copies the rest of the application source code from the host to the container.

  • Exposes port 3000 to allow communication with the container.

  • Sets an environment variable NODE_ENV to "production".

  • Specifies that the command to run when the container starts is node app.js.

After creating the Dockerfile, you can build a Docker image using the docker build command.

docker build -t my-node-app .

Replace my-node-app with the desired name for your Docker image.

After building the image, you can run a container from it using:

docker run -p 3000:3000 my-node-app

This command runs a container based on your Docker image, forwarding port 3000 from the container to port 3000 on your host machine. Adjust the port mapping as needed based on your application’s requirements.


🚀 Want to Learn Docker from Scratch?

👉 Read: How to Create a Dockerfile – Step-by-Step
📌 Follow me for DevOps tutorials every week

🔗 Read now: meghasharma.hashnode.dev/dockerfile-for-a-python-application
☕ Support me:

More from this blog

Megha Sharma's Blog

87 posts