Docker is a powerful tool that enables developers to package applications and their dependencies into a portable container, making it easy to deploy and run anywhere. In this article, we'll walk through building a Docker image for a simple Node.js application, and cover key commands for managing Docker containers. This includes writing a Dockerfile, building the image, and running the container.
Prerequisites
- Basic knowledge of Docker.
- Docker installed on your machine.
- Node.js and npm installed for testing the app locally.
Step 1: Create a Simple Node.js Application
First, letβs set up a basic Node.js application. Create a new directory for your project and initialize it:
mkdir node-docker-app
cd node-docker-app
npm init -y
Next, create an index.js
file with the following content:
// index.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('Hello, Docker!');
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Install express
as a dependency:
npm install express
Now you have a basic Node.js app that responds with "Hello, Docker!" at the root URL.
Step 2: Write the Dockerfile
A Dockerfile is a script that contains instructions for building a Docker image. Create a file named Dockerfile
in your project directory and add the following content:
# Use the official Node.js image as the base image
FROM node:18
# Set the working directory inside the container
WORKDIR /app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application files
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Define the command to run the app
CMD ["node", "index.js"]
Explanation of Dockerfile Commands
-
FROM
: Specifies the base image. We use the official Node.js image. -
WORKDIR
: Sets the working directory inside the container to/app
. -
COPY
: Copies files from your local machine to the working directory inside the container. -
RUN
: Executes commands in the Docker image (e.g.,npm install
to install dependencies). -
EXPOSE
: Exposes port 3000 to allow the application to be accessible. -
CMD
: Defines the command to run when the container starts.
Step 3: Build the Docker Image
With the Dockerfile in place, build the Docker image using the following command:
docker build -t node-docker-app .
-
-t node-docker-app
: Tags the image with a name for easy reference. -
.
: Refers to the current directory, which contains the Dockerfile.
Once the build is complete, you can list the Docker images on your system:
docker images
This should show the node-docker-app
image you just built.
Step 4: Run the Docker Container
Now that we have an image, let's run a container from it:
docker run -p 3000:3000 node-docker-app
-
-p 3000:3000
: Maps port 3000 on your local machine to port 3000 in the container. -
node-docker-app
: The name of the image to run.
Once the container is running, you can open your browser and navigate to http://localhost:3000
to see your app in action. It should display "Hello, Docker!".
Step 5: Managing Docker Containers
List Running Containers
To see the currently running containers, use:
docker ps
This will show you the container ID, image name, and other details about running containers.
Stop a Container
To stop a running container, use:
docker stop <container_id>
Replace <container_id>
with the ID of your container, which you can get from docker ps
.
Remove a Container
To remove a stopped container, use:
docker rm <container_id>
Remove an Image
If you want to remove the Docker image, use:
docker rmi node-docker-app
Make sure that no containers are using the image before you try to remove it.
Step 6: Pushing the Docker Image to Docker Hub (Optional)
If you want to share your Docker image, you can push it to Docker Hub:
-
Log in to Docker Hub:
docker login
-
Tag the image with your Docker Hub username:
docker tag node-docker-app yourusername/node-docker-app
-
Push the image to Docker Hub:
docker push yourusername/node-docker-app
Now, anyone can pull and run your image using:
docker pull yourusername/node-docker-app
docker run -p 3000:3000 yourusername/node-docker-app
Conclusion
In this article, we created a simple Node.js application and packaged it into a Docker container using a Dockerfile. We went through building, running, and managing the Docker image, and even pushing it to Docker Hub for easy distribution. Docker simplifies the process of deploying applications by providing a consistent runtime environment, making it a valuable tool for developers.
Top comments (0)