It's no secret that Docker is one of the most popular containerization technologies available today. Docker lets you package an application along with its dependencies into an isolated, compact container. This process is known as containerization.
When running your application, a small change in a package/library can cause errors or change functionality. With the help of Docker, you can containerize the application which allows the application to execute the application the same way in every computer or server with no errors or functionality changes.
Using Docker, you can make an image of a code base and its packages, and then run a container based on that image. Additionally, you can share your images with others.
An image can be created automatically by Docker by reading instructions in a Dockerfile. This is a text document that contains all the commands that could be used to assemble an image. For more details here.
In this blog, You will be learning how to containerize simple Node.js API using Docker.
- Basic understanding of Python and Flask
- Basic understanding of Linux
- A code editor
- Basic Understanding of Docker
Throughout this blog, we will be using basic code from my github, which is a simple node.js application. You can clone the repository from here.
Application Structure looks like this:
└── resume-project ├── controllers │ └── authController.js ├── middlewares │ └── verifyToken.js ├── model │ └── User.js ├── package.json ├── package-lock.json ├── README.md ├── routes │ ├── auth.js │ └── posts.js ├── server.js └── validation.js
Dockerfiles are used to create images of our application, which can be run in any Docker installed server or environment.
FROM node WORKDIR /app COPY . /app RUN npm install ENTRYPOINT ["node"] CMD ["app.js"]
The above Dockerfile contains the commands for building our image.
Now, It's time to make an image from the docker file. To build an image with the name ‘mysite’, you can use the following command.
docker build . -t mysite
We have successfully built an image from the docker file. We can view our images with the following command.
We can easily run our container from our image with the following command.
Docker run -p 8000:3000 -d mysite --name mysite
Docker run command is used to run any container, p flag is being used to map the external port(8000) to the port inside the container(3000), d flag is being used to run the container as a background process.
You can view all the running containers using
docker containers ls
Since the container is running, you can access the website on 0.0.0.0:8000.
This is how you can containerize simple node js application. This much for this blog, If you like it, Follow me for more blogs.