Containerizing a Node.js Application with Docker: A Step-by-Step Guide with Examples
Prerequisites
Before we start, make sure you have the following installed on your machine:
- Docker Desktop (or Docker Engine) installed on your machine
- Node.js and NPM (Node Package Manager) installed on your machine
- A Node.js application that you want to containerize
Step 1: Create a Dockerfile
A Dockerfile is a script that contains instructions for building a Docker image. In your Node.js application directory, create a new file called Dockerfile
and add the following code:
# Use an official Node.js runtime as a parent image
FROM node:latest
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages
RUN npm install
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Define the command to run your app
CMD ["npm", "start"]
This Dockerfile starts with a Node.js base image, sets the working directory to /app
, copies the contents of the current directory into the container, installs any needed packages with npm install
, exposes port 8080 to the outside world, and defines the command to run the application with npm start
.
Step 2: Build the Docker Image
To build the Docker image from the Dockerfile, open your terminal and navigate to your Node.js application directory. Then, run the following command:
docker build -t my-node-app .
This command builds the Docker image with the tag my-node-app
and uses the current directory (.
) as the build context. The build context is the set of files that Docker uses to build the image.
This command may take a few minutes to complete, especially if you have many dependencies.
Step 3: Run the Docker Container
Now that you’ve built the Docker image, you can run it as a container. Run the following command in your terminal to start the container:
docker run -p 8080:8080 my-node-app
This command runs the container with the image my-node-app
, maps port 8080 in the container to port 8080 on your machine, and starts the Node.js application.
You should now be able to access the Node.js application by navigating to http://localhost:8080
in your web browser.
Step 4: Customize Your Node.js Application
If you want to customize your Node.js application or change the ports it listens on, you can modify the Dockerfile
and rebuild the Docker image. For example, if your Node.js application listens on port 3000, you can modify the EXPOSE
and CMD
lines in the Dockerfile
like this:
# Use an official Node.js runtime as a parent image
FROM node:latest
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages
RUN npm install
# Make port 3000 available to the world outside this container
EXPOSE 3000
# Define the command to run your app
CMD ["node", "app.js"]
Then, rebuild the Docker image with the following command:
docker build -t my-node-app .
Finally, run the container with the following command:
docker run -p 3000:300
And that’s it, now you know how to containerize a nodejs with Docker.