Prerequisites
- Node.js and npm installed on your machine
- Docker installed on your machine
- An existing Node.js web application or create one using Express
- IDE (Visual Studio Code, IntelliJ IDEA, etc.)
Step 1: Create a Node.js Web Application
If you don't have an existing Node.js web application, you can create a new one using Express.
Open your terminal and run the following commands:
mkdir my-node-app
cd my-node-app
npm init -y
npm install express
Create a file named index.js
in the root directory of your project:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, Docker!');
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
This will create a simple Node.js web application that listens on port 3000.
Step 2: Create a Dockerfile
Create a file named Dockerfile
in the root directory of your Node.js project. This file will contain instructions for building the Docker image.
# Use an official Node runtime as a parent image
FROM node:20
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose port 3000
EXPOSE 3000
# Run the application
CMD ["node", "index.js"]
Explanation:
FROM node:20
: Use the official Node.js image as the base image.WORKDIR /app
: Set the working directory inside the container to/app
.COPY package*.json ./
: Copypackage.json
andpackage-lock.json
to the container.RUN npm install
: Install the project dependencies.COPY . .
: Copy the rest of the application code to the container.EXPOSE 3000
: Expose port 3000 to the host.CMD ["node", "index.js"]
: Run the application.
Step 3: Build the Docker Image
Run the following command to build the Docker image:
docker build -t my-node-app .
Explanation:
docker build
: The Docker command to build an image.-t my-node-app
: Tags the image with the namemy-node-app
..
: Specifies the current directory as the build context.
Step 4: Run the Docker Container
Run the following command to start a Docker container from the image:
docker run -p 3000:3000 my-node-app
Explanation:
docker run
: The Docker command to run a container.-p 3000:3000
: Maps port 3000 of the container to port 3000 on the host machine.my-node-app
: The name of the Docker image to run.
Step 5: Verify the Application
Open a web browser and navigate to http://localhost:3000
. You should see the message "Hello, Docker!" from your Node.js application.
Additional Docker Commands
6.1 List Docker Images
To list all Docker images on your system, run:
docker images
6.2 List Running Containers
To list all running Docker containers, run:
docker ps
6.3 Stop a Running Container
To stop a running Docker container, run:
docker stop <container_id>
Replace <container_id>
with the actual container ID obtained from the docker ps
command.
6.4 Remove a Docker Container
To remove a Docker container, run:
docker rm <container_id>
Replace <container_id>
with the actual container ID.
6.5 Remove a Docker Image
To remove a Docker image, run:
docker rmi my-node-app
Conclusion
In this guide, you have learned how to dockerize a Node.js web application. We covered:
- Creating a simple Node.js web application.
- Writing a Dockerfile to build and run the Node.js application.
- Building the Docker image.
- Running the Docker container.
- Verifying the application.
By following these steps, you can easily package your Node.js application into a Docker container, providing a consistent and portable deployment environment.
Comments
Post a Comment
Leave Comment