DEV Community

Cover image for Dockerfile for a Node.js application
Megha Sharma
Megha Sharma

Posted on

2

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:

  • Open a terminal.
  • Navigate to the directory where you want to create the Dockerfile.
  • Type nano Dockerfile and press Enter. This will open the nano text editor with a new file named Dockerfile.
  • Enter the Dockerfile contents according to your requirements.
  • 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"]
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay