Dockerizing node.js Application with Dockerfile and pushing image to Docker hub
Table of contents
Hello everyone, this is day 17 of the #90DaysOfDevops challenge. In this blog, let's learn about what a Dockerfile is and the uses of the Dockerfile, how Dockerfile works in Docker to create a container.
task:
Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
Build the image using the Dockerfile and run the container
Verify that the application is working as expected by accessing it in a web browser
Push the image to a public or private repository (e.g. Docker Hub )
Introduction to Dockerfile
Dockerfile is a script that contains a set of instructions for Docker to build a Docker image. It is a plain text file that specifies the components and configuration of a Docker container. A Docker image is a lightweight, standalone, executable package that contains everything needed to run an application, including code, libraries, dependencies, and configuration files. A Dockerfile can be used to automate the process of building, packaging, and deploying applications in a containerized environment.
Dockerfile allows developers to define a Docker image in a declarative manner. Each instruction in the Dockerfile creates a layer in the final image. This layer can be cached by Docker, so subsequent builds that don't modify the layer can reuse the cached layer, making the build process faster and more efficient.
The name of the Dockerfile must be ‘Dockerfile’ else Docker daemon will throw an error and it is case-sensitive. We use the ‘docker build’ command to create a Docker image from a Dockerfile.
Dockerfile Instructions:-
Dockerfile instructions are written in a specific format that includes a keyword followed by arguments. The most common instructions used in Dockerfile include:
FROM: Specifies the base image to use for the image being built.
RUN: Runs a command in the container during the build process.
COPY: Copies files and directories from the host to the container.
WORKDIR: Sets the working directory for the container.
EXPOSE: Exposes a port for the container.
CMD: Specifies the default command to run when the container starts.
ADD: Used to copy files from the host machine to the container directory and it can also copy TAR files and unzip them on the container directory.
ENTRYPOINT: The commands get executed post-container launch. It cannot be overwritten and a new command can be appended.
VOLUME: Is used to inform users of the Docker image that certain directories within the container should be treated as volumes when running a container based on that image.
- Create a Dockerfile for a simple web application (e.g. a Node.js or Python app).
Using the command vim open the dockerfile "vim dockerfile". And the following code is present in the file.
FROM node:12.2.0-alpine
: Use a base image with Node.js pre-installed
COPY . .
: Copy the application code to the working directory
RUN npm install package.json
: Install application dependencies which are present in package.json file.
EXPOSE 8000
: Expose the port that the application listens on
CMD ["node", "app.js"]
: Specify the command to run the application.
I have used the following repository of application : ShubhamBMatere/node-todo-cicd (github.com)
Using the git clone command, clone the repository to your machine. Then get into the directory of the cloned repository.
Build the image using the Dockerfile.
To build the image, save the Dockerfile in a directory alongside your application code and run the following command in that directory:
Run the container.
Once the image has been built, you can run a container using the following command. Where -d prompts to run the application in detached mode and -p specifies the port.
Verify that the application is working as expected by accessing it in a web browser:
Edit the inbound rules for the EC2 instance in AWS. Open port 8000 for your IP for all traffic.
Voila, the application is running as expected on web browser.
Push the image to a public or private repository (e.g. Docker Hub )
Step 1: Tag the Image:-
Before you can push the image to Docker Hub, you need to tag it with the Docker Hub username and repository name. You can do this using the following command.
Step 2: Log in to Docker Hub
To push an image to Docker Hub, you need to log in to your Docker Hub account using the following command:
Step 3: Push the Image
This command uploads the image to Docker Hub, where it can be accessed by other users.
Step 4: Verify the Image
After pushing the image to Docker Hub, you can verify that it was successfully uploaded by logging in to the Docker Hub website and checking your repository.
Conclusion
Dockerfile is a powerful tool that allows developers to create custom Docker images for their applications. By writing a Dockerfile that specifies the necessary instructions, developers can easily build an image that includes everything needed to run the application. Once the image has been built, it can be used to run a container in any environment that supports Docker.
Well, this was it for this blog. Until then, keep reading my blogs and connect with me on LinkedIn.
To help me improve my blog and correct my mistakes I am open to discussing. Do reach me and I am open to suggestions and corrections.