Hey,
My name is Sarvar, and I am a highly skilled Senior Developer at Luxoft India. With years of experience working on cutting-edge technologies, I have honed my expertise in Cloud Operations (Azure and AWS), Data Operations, Data Analytics, and DevOps. Throughout my career, I’ve worked with clients from all around the world, delivering excellent results, and going above and beyond expectations. I am passionate about learning the latest and treading technologies.
Today, we’ll examine what a Dockerfile is. Assuming you are already familiar with the fundamentals of Docker, we will move right into the Docker files. If you are unfamiliar with Docker, don’t worry I have a separate article on the topic and will provide the link to it below. We’ll see how to utilize Docker files specifically to containerize applications, and we’ll see a commands of a Docker file. I’ve included the custom nginx-based webhosting at the end of this article to help you understand how we make custom Docker images using docker file commands and how we build Docker containers from those images. so let’s get started.
What is Docker: Link
What is Dockerfile -
A simple text file called a Dockerfile contains a set of instructions for creating a Docker image. It functions more or less as a recipe or blueprint that outlines how to build a containerized application. The base image to use, the installed dependencies, the files to copy into the image, the environment variables to set, and the commands to run when the container starts are all specified in the Dockerfile.
The Dockerfile format is straightforward and easy to understand. It usually comprises of a succession of directives, each beginning with a keyword and continue with a list of arguments. You can use the docker build command to generate a Docker image from a Dockerfile by providing the path to the directory containing the Dockerfile. In order to generate the image, Docker reads and follows the instructions in the Dockerfile step-by-step. The image can be used to launch container instances after it has been produced.
Dockerfile Instructions -
The commands and instructions used within a Dockerfile to specify the stages for creating a Docker image are known as Dockerfile instructions. Each command in a Dockerfile has a specific function and aids in setting up and configuring the containerized application as a whole. These Dockerfile instructions are some of the most popular ones:
1. WORKDIR -
The WORKDIR command in the Dockerfile defines the working directory for following instructions. It allows for the specification of a relative path and is comparable to the cd command. For instance:
WORKDIR /prod
2. FROM -
The base image to utilize as the Docker image’s starting point is specified via the FROM command. On top of which succeeding instructions will be written, it establishes the parent image. For instance:
FROM redhat:1.2.3
3. RUN -
When creating an image, the RUN command runs instructions inside the container. It is used to carry out setup operations like installing packages and running scripts. For instance:
RUN wget https://sarvar.com/server.csv
4. COPY -
The COPY command inserts files or directories into the container image from the host system. The source path from the host and the destination path inside the image are its two required inputs. For instance:
COPY deployment.yml /prod/
4. LABEL -
Metadata can be added to a Docker image using the LABEL directive. In order to provide information about the image, such as its version, maintainer, description, or any other custom metadata, you can attach key-value pairs to the image. Labels are frequently employed for organizing, identification, and documentation needs. For instance:
LABEL version="11.2.33"
LABEL maintainer="Sarvar <sarvar@mydomain.com>"
LABEL description="Frontend application module"
5. USER -
The user or UID (User Identifier) that the container process should run as when the container starts is specified by the USER instruction. It is used to provide the user context inside the container and is beneficial for privilege separation and security. Docker containers run as the root user by default. To lessen the effect of any security flaws, it is advised to run containers wherever possible with non-root users. For instance:
USER sarvar
6. VOLUME -
The VOLUME instruction states that a specific directory within the container will be mounted as a volume during runtime and creates a mount point for it. The host machine or other containers and the container can share and persist data through the use of volumes. Even if the container is destroyed and rebuilt, it enables permanent storage. Databases, file uploads, logs, and other data that must be kept safe outside the container’s lifespan are frequently stored on volumes. For instance:
VOLUME /data
7. ENV -
Environment variables are set within the container by the ENV instruction. It accepts key-value pairs as parameters and is frequently used to configure settings unique to an application. For instance:
ENV PORT=80
8. EXPOSE -
The EXPOSE command tells Docker that the container listens on a set of network ports while it is running. Neither does it establish new network connections or publish the ports. For instance:
EXPOSE 80
9. CMD -
The default command to execute when the container starts is specified by the CMD instruction. It can take arguments and be overridden by giving a command when the container is being executed. For instance:
CMD ["python", "application.py"]
10. ONBUILD -
When an image is used as the foundation for another image, triggers or actions can be added using the ONBUILD instruction. It lets you define instructions that will wait until a later build that starts with your image as a base. When constructing a basic image with general instructions that may be modified or expanded by other Dockerfiles, this is helpful. For instance:
ONBUILD COPY . /backup
ONBUILD RUN init6
Real Time Dockerfile For Creating Simple Nginx Based Website -
Here we are using docker file commands to create a custom Docker Image of the Nginx with simple webhosting. Just follow the steps.
Step 1: (Create HTML file)
Utilize the vi command to create an index.html file and add the following content.
<!DOCTYPE html>
<html>
<head>
<title> Sarvar's </title>
</head>
<body>
<body style=text-align:center;background-color:White;font-weight:900;font-size:20px;font-family:Helvetica,Arial,sans-serif>
<img src="https://www.docker.com/wp-content/uploads/2022/03/Moby-logo.png">
<h1> Welcome To My Nginx WebPage </h1>
<p> -> This Container Was Deployed On <- <div id="date"></div></p>
<script>
var date = new Date();
document.getElementById("date").innerHTML=date.toLocaleString();
</script>
</body>
</html>
Step 2: (Create Dockerfile)
On the same computer where you created the index.html file, create a file named Dockerfile. Paste the following text into the Dockerfile.
LABEL Version="1.0.0"
LABEL maintainer="Sarvar <sarvar@mydomain.com>"
LABEL description="Nginx WebHosting"
User Sarvar
FROM nginx
RUN apt-get update && apt-get upgrade -y
COPY index.html /usr/share/nginx/html
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
Step 3: (Build Docker Image using Docker File)
We’ll create a docker image using a Dockerfile after you have index.html and Dockerfile in the same location. Simply use the below command to generate a Docker image.
docker build -t webhost .
Once the docker build command has completed successfully, you will see the result on the same screen. To double-check whether your docker image has been created or not, use the command below to see if the webhost name docker image is present.
docker image ls
Step 4: (Creating Docker Container)
as soon as you can see the docker image. We are now using a Docker image to create the container. In this case, we are exposing the Docker container on port 8080. After executing the command below, the container will be created, and you can access it using the public IP address and port 8080.
docker run -d -name webhost_nginx -p 8080:80 webhost
Step 5: (Accessing the Nginx Based Web Hosting)
Following the execution of the command, you will be able to access our Nginx-based web hosting on the public IP using Port Number 8080.
<public_ip>:8080
Conclusion: It is simpler to distribute and replicate containerized apps across many contexts and platforms thanks to the Dockerfile, which offers a standardized and repeatable method for creating Docker images. It is crucial to Docker’s containerization procedure.
— — — — — — — —
Here is the End!
Thank you for taking the time to read my article. I hope you found this article informative and helpful. As I continue to explore the latest developments in technology, I look forward to sharing my insights with you. Stay tuned for more articles like this one that break down complex concepts and make them easier to understand.
Remember, learning is a lifelong journey, and it’s important to keep up with the latest trends and developments to stay ahead of the curve. Thank you again for reading, and I hope to see you in the next article!
Happy Learning!