DEV Community

Cover image for "Containerize Your Node.js App: A Beginner's Guide to Creating Docker Images"
Rishi Kumar
Rishi Kumar

Posted on • Edited on

2

"Containerize Your Node.js App: A Beginner's Guide to Creating Docker Images"

Creating a Docker image for a Node.js app running on localhost involves several steps. Docker allows you to package your application and its dependencies into a portable container. Here's a step-by-step guide:

  1. Install Docker:
    If you haven't already, install Docker on your machine. You can download it from the official Docker website: https://www.docker.com/get-started

  2. Prepare Your Node.js App:
    Make sure your Node.js application is working correctly on your localhost. Create a directory for your Docker configuration and files.

  3. Create a Dockerfile:
    Create a file named Dockerfile (no file extension) in your application directory. The Dockerfile contains instructions for building your Docker image.

   # Use an official Node.js runtime as the base image
   FROM node:16

   # 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 app dependencies
   RUN npm install

   # Copy the rest of the application files to the container
   COPY . .

   # Expose the port that the app will run on
   EXPOSE 3000

   # Command to start the application
   CMD ["node", "app.js"]
Enter fullscreen mode Exit fullscreen mode

Replace "app.js" with the main file of your Node.js app if it has a different name.

  1. Build the Docker Image: Open a terminal in your application directory and run the following command to build the Docker image. Replace "my-node-app" with a suitable name for your image and "1.0" with a version tag:
   docker build -t my-node-app:1.0 .
Enter fullscreen mode Exit fullscreen mode

The . at the end specifies the build context (current directory).

  1. Run the Docker Container: After building the image, you can run a container based on that image. Use the following command:
   docker run -p 3000:3000 -d my-node-app:1.0
Enter fullscreen mode Exit fullscreen mode

This maps port 3000 from the container to port 3000 on your localhost. The -d flag runs the container in detached mode.

  1. Access Your App: You can access your Node.js app by opening a web browser and navigating to http://localhost:3000.

Remember that Docker containers are isolated environments. The dependencies installed within the container might differ from your local environment. Additionally, this guide provides a basic setup; you might need to adjust it based on your app's specific requirements.

Finally, to stop and remove a running container, you can use the following command:

docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode

Replace <container_id> with the actual ID of the running container, which you can get using docker ps.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (3)

Collapse
 
code42cate profile image
Jonas Scholz

I comment that a lot, but you really shouldnt use Node 14 anymore and instead use a Node version that still has support: nodejs.dev/en/about/releases/

Collapse
 
code42cate profile image
Jonas Scholz

Thanks for updating it!

Collapse
 
mrrishimeena profile image
Rishi Kumar

Thanks for pointing out

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more