Docker Project for DevOps Engineers
Dockerfile:
A Dockerfile is a text file that contains a set of instructions to build a Docker image. It provides a standardized and reproducible way to create containerized applications.
It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.
Dockerfile components:
Base Image:
The Dockerfile typically starts with a base image, which serves as the starting point for the container. It can be a minimal operating system or a preconfigured image with specific tools and dependencies.
Instructions:
The Dockerfile consists of a series of instructions that define the steps to build the image. Here are some common instructions:
FROM: Specifies the base image.
RUN: Executes commands to install dependencies, configure the environment, or run any necessary setup steps.
COPY or ADD: Copies files from the host machine to the image.
WORKDIR: Sets the working directory for subsequent instructions.
ENV: Sets environment variables.
EXPOSE: Declares the network ports that the container will listen on.
CMD or ENTRYPOINT: Specifies the command to run when the container starts.
TASK:
For this project, we will use an ec2 instance where we will first create the NodeJS application. This will require NodeJS installed in the instance.
To install npm:
sudo apt install npm
Once the app is installed , we will use the command 'npm init -y' to initialize the Node. The output will be like this:
We will also install any dependencies that will be required for the Node.js application. For example, we will install Express:
npm install express
Once express is installed, we will create a file named 'index.js' and add the following code to create a basic Express server:
To check if the application works using the ec2 instance public Ip, run the command, 'node index.js'. The server will start running on port 8080
Now, in order to containarize the application, we will write a Dockerfile in the same directory:
We will then create a Docker image using the following command:
Recommended by LinkedIn
sudo docker build -t my-node-app .
Since we did not give it any tag, it will automatically assign the tag 'latest' to it.
We can see the created image using the command 'docker images':
Lastly, we will run the docker container using the command:
docker run -d -p 8080:8080 my-node-app
The docker container will run the application and can be accessed on the port 8080.
Pushing the image to Dockerhub:
To push the docker images to the Dockerhub we need to have a Dockerhub account. Once the account is created, we can use below commands to push the image to Dockerhub:
docker login
Once the username and password are filled, we will be logged into the Dockerhub account.
Tagging the Docker image with Docker Hub username and the desired repository name:
docker tag my-node-app:latest your-dockerhub-username/my-node-app:latest
Pushing the tagged image to Docker Hub using the following command:
docker push your-dockerhub-username/my-node-app:latest
Once the image is pushed to the Dockerhub, we can see it as below:
This pushed image can we pulled again and can be run as per requirement.
Thank you for reading!