How to Create a Docker Project for a Node.js Web Application | 90 Days Of DevOps
In this blog, I’ll show you how to create a Docker project for a simple web application in Node.js. Docker is a tool that allows you to build, run, and share containerized applications. Containers are isolated environments that contain everything needed to run an application, such as code, dependencies, libraries, and configuration files. Docker makes it easy to deploy and manage your applications across different platforms and environments.
What is Dockerfile?
A Dockerfile is a text file that contains the instructions and commands that will be executed in sequence to build a Docker image. A Docker image is a template that contains the layers of files and settings that make up a container. You can use a Dockerfile to specify the base image, the files to copy, the commands to run, the ports to expose, and other options for your image. You can also use a Dockerfile to automate the process of building and deploying your application.
A Dockerfile has a specific format and syntax that you can find at Dockerfile reference. A typical Dockerfile looks something like this:
# Use an existing image as a base
FROM node:14
# Set the working directory
WORKDIR /usr/src/app
# Copy the package.json and package-lock.json files
COPY package*.json ./
# Install the dependencies
RUN npm install
# Copy the rest of the code
COPY . .
# Expose the port that the app listens on
EXPOSE 3000
# Define the command to run the app
CMD ["node", "app.js"]
Each line in a Dockerfile represents a layer in the image. The first line specifies the base image to use, which is an existing image that contains Node.js. The second line sets the working directory for the subsequent commands. The third and fourth lines copy the package.json and package-lock.json files from the current directory to the working directory in the image and install the dependencies using npm. The fifth line copies the rest of the code from the current directory to the working directory in the image. The sixth line exposes port 3000, which is the port that the app listens on. The last line defines the command to run when the container starts, which is node app.js.
Create a Dockerfile for a simple web application in Node.js
In this task, you will create a Dockerfile for a simple web application in Node.js that displays a message saying “Hello from Docker!” when accessed in a web browser. You will use the same base image and working directory as in the previous example, but you will need to write your own app.js file and copy it to the image.
To create a Dockerfile, you can use any text editor of your choice. You can name your file anything you want, but by convention, it is called Dockerfile. Here is an example of how your Dockerfile might look like:
# Use an existing image as a base
FROM node:14
# Set the working directory
WORKDIR /usr/src/app
# Copy the app.js file
COPY app.js .
# Expose the port that the app listens on
EXPOSE 3000
# Define the command to run the app
CMD ["node", "app.js"]
Notice that you don’t need to copy or install any dependencies because your app.js file does not require any modules. You also don’t need to specify any environment variables or entry points for your app.
To create your app.js file, you can use any text editor of your choice as well. Here is an example of how your app.js file might look like:
// Import the http module
const http = require('http');
// Create a server object
const server = http.createServer((req, res) => {
// Write a response header
res.writeHead(200, {'Content-Type': 'text/plain'});
// Write a response body
res.end('Hello from Docker!\n');
});
// Listen on port 3000
server.listen(3000, () => {
// Log a message when the server starts
console.log('Server running at http://localhost:3000/');
});
Notice that you are using the http module to create a server object that handles incoming requests and sends back responses. You are also listening on port 3000, which matches with what you exposed in your Dockerfile.
Build the image using the Dockerfile and run the container
In this task, you will use the docker build command to build an image using your Dockerfile, and then use the docker run command to run a container from your image.
To build an image using your Dockerfile, you need to navigate to the directory where your Dockerfile and app.js files are located, and then run this command:
docker build -t my-app .
The -t flag assigns a name and optionally a tag to your image, which in this case is my-app. The . indicates the current directory as the build context, which is where Docker will look for the files to copy to the image.
You should see something like this:
The output shows that Docker has executed each step in your Dockerfile and created a new layer for each one. The final layer is tagged with the name and tag of your image, which is my-app:latest
.
To run a container from your image, you need to run this command:
docker run -d -p 3000:3000 my-app
The -d flag runs the container in detached mode, which means it runs in the background. The -p flag publishes the port 3000 on the container to the port 3000 on the host, which allows you to access your app from outside. The my-app is the name of your image.
You should see something like this:
The output shows the ID of the container that has been created and started. You can also use the docker ps command to see the information about your running container, such as its name, image, status, and ports:
You can see that the container is running, and the port 3000 on the container is mapped to the port 3000 on the host.
Verify that the application is working as expected by accessing it in a web browser
In this task, you will verify that your web application is working as expected by accessing it in a web browser. You will use the host IP address and port to access your app.
To access your web application, you need to open your web browser and type http://localhost:3000
or http://<host-ip>:3000
, where <host-ip>
is the IP address of your host machine. You should see something like this:
This means that your web application is running inside a Docker container and responding to your requests. You can also check the logs of your container by using the docker logs command, which shows the output of the app.js file:
docker logs eloquent_gagarin
You should see something like this:
This means that your app.js file has been executed successfully and started listening on port 3000.
Push the image to a public or private repository
In this task, you will push your image to a public or private repository, such as Docker Hub, which is a service provided by Docker for finding and sharing container images. You can use this to store and distribute your images, or to collaborate with other developers.
To push your image to a repository, you need to follow these steps:
- Create an account on Docker Hub or any other repository service that you want to use.
- Log in to your account using the docker login command, which will prompt you for your username and password.
- Tag your image with the name of your repository and optionally a tag, using the docker tag command. For example, if you want to push your image to a repository called my-repo on Docker Hub, and tag it as latest, you can use this command:
docker tag my-app ajitfawade14/first-repo:my-app
- Push your image to the repository using the docker push command, which will upload your image and its layers to the repository. For example, if you want to push your image to a repository called my-repo on Docker Hub, you can use this command:
docker push ajitfawade14/first-repo:my-app
You should see something like this:
The output shows that your image and its layers have been pushed to the repository. You can also verify that your image is available on the repository by visiting its web page or using the docker search command. For example, if you want to search for your image on Docker Hub, you can use this command:
docker search ajitfawade14/first-repo
You should see something like this:
You can see that your image is listed with its name, description, stars, official status, and automated status.
Conclusion
In this blog, I have shown you how to create a Docker project for a simple web application in Node.js. I have also shown you how to create a Dockerfile, build an image, run a container, verify the application, and push the image to a repository. These steps will help you develop and deploy your applications using Docker.
I hope you have learned something new and useful from this blog. If you have any questions or feedback, please feel free to leave a comment below.
Thank you for reading and happy learning! 😊