Docker Project Using Dockerfile
Day 17 #90daysofdevopschallenge

Hi there! My name is Yashswini and I'm a Linux Engineer with 3 years of experience in automation, continuous integration, and deployment. My expertise lies in DevOps and Cloud Computing with a focus on AWS. I have hands-on experience in various tools and technologies related to infrastructure automation, managed services, containerization, and monitoring and logging. I have a passion for troubleshooting and resolving issues and I'm always eager to learn and explore new technologies
In the last blog, we learned how to create containers and run through the docker image and how to pull a docker image from the registry or docker hub. Say, for example, you pull down the latest Ubuntu image for development. Before you can develop with that container, there are a number of modifications you want to make to the image (such as upgrading software and adding the necessary development packages.
For this, you could manually edit each image as needed or you could construct a Dockerfile for each variation. Once you have your Dockerfile constructed, you can quickly build the same image over and over, without having to take the time to do it manually. So the basic definition of Dockerfile will be
1. Dockerfile
It is a simple text file with a set of commands or instructions. These commands/instructions are executed successively to perform actions on the base image to create a new docker image.
2. The syntax for writing a Dockerfile and Format
FROM:
A FROM statement defines which image to download and start from. It must be the first command in your Dockerfile. eg
FROM ubuntu:latestMAINTAINER :
This statement is a kind of documentation, which defines the author who is creating this Dockerfile
MAINTAINER Firstname LastnameRUN :
The RUN statement defines running a command through the shell, waiting for it to finish, and saving the result. It tells what process will be running inside the container at the run time.
RUN apt-get updateRUN apt-get install -y nginxCOPY:
It is used to copy your local files/directories to Docker Container.
COPY nginx.conf /etc/nginx/WORKDIR :
To set a working directory for the container
eg:
WORKDIR /dataThis means after creating the container working directory will be /data.
CMD :
CMD specifies the whole command to run. The main purpose of the CMD command is to launch the software required in a container.
CMD ["nginx", "-g", "daemon off;"]EXPOSE :
EXPOSE statement maps a port into the container. The ports can be TCP or UDP but by default, it is TCP.
EXPOSE 8080ENTRYPOINT :
It specifies the starting of the expression to use when starting your container. Simply ENTRYPOINT specifies the start of the command to run.
ENTRYPOINT ["/start.sh"]ENV :
ENV statement sets the environment variables both during the build and when running the result.
ENV env_name xyzARG :
A variable that can be provided at build time is defined by an ARG Instruction. Once it has been specified in the Dockerfile, you can specify it using the –build-arg switch when creating the image. The Dockerfile supports multiple ARG instructions. The only instruction in the Dockerfile that can come before the FROM instruction is ARG.
USER :
It sets which user’s container will run as. This can be useful if you have shared network directories involved that assume a fixed username or a fixed user number.
VOLUME :
The VOLUME statement defines shared volumes or ephemeral volumes depending upon whether you have one or two arguments.
If you have two arguments, it maps a host path into a container path.
VOLUME ["/host/path" "/container/path/"]
3. Docker build
This method allows the users to build their own Docker images.
docker build -t <ImageName:TagName> dir
-t − is to mention a tag to the image
ImageName − This is the name you want to give to your image.
TagName − This is the tag you want to give to your image.
Dir − The directory where the Docker File is present.
Now it is hands-on time...
4. Tasks
- Create a Dockerfile for a simple web application (node.js)
Step 1- Install the docker in the system and then clone the code from Git

Step 2- Create Dockerfile for the code

Step 3- After Dockerfile creation we will build an image

verify the image

Step 4- Now we build a container of this image

verify 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 )
now how to push an image in the docker hub and how to create an account in the docker hub
Step 1 − Log into Docker Hub and create your repository. This is the repository where your image will be stored.

Step 2- Issue the Docker login command to login into the Docker Hub repository from the command prompt.

NOTE: In the password section password of DockerHub is not working you need to generate Acess Token for that you need to go
Profile > Account Setting > Security >Acess Token > Generate New Token
and use that token as a login to the local machine
Step 3- Now after login tag an image to the relevant repository.
docker tag <imageID> <Repositoryname>Step 4 -it’s now time to push the image to the Docker Hub repository.
docker push <Repository name>

Happy Learning !!



![Jenkins Freestyle Project for DevOps Engineers[Day-23 Task]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1690291895202%2Fc7012bdd-d2f6-44e4-8d64-6d071c0c7c39.png&w=3840&q=75)
