DockerFile components and examples

What is Dockerfile
- A Dockerfile is a text file that contains a series of commands or instructions.
- These instructions are executed in the order in which they are written.
- Execution of these instructions takes place on a base image.
- You will use a Dockerfile to create your own custom Docker image
How to create a Docker image using a docker file?
In the docker file use the docker components like — FROM, CMD, RUN, ENTRYPOINT, etc.
Docker Components:-
FROM:- The FROM instruction initiates a new build phase and sets the base image for subsequent instructions. As such, a valid Dockerfile must begin with the FROM instruction.
FROM <imge_name>:<version>
RUN:- The RUN instruction allows you to install your application and the required packages for it. It executes any command on top of the current image and creates a new layer giving the result.
RUN <command> ( the command is run in a shell)
CMD:- The CMD command defines a default command to run when your container starts.
CMD <command>
ENTRYPOINT:- An ENTRYPOINT is the same work as CMD. An ENTRYPOINT allows you to the extra argument of the command that will run as an executable.
ENTRYPOINT <command> <param1> <param2>….
COPY:- The COPY instruction copies files or directories from the Base os <src> and adds them to the filesystem of the container at the path <dest>
COPY <src> <dest>
WORKDIR:- The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
WORKDIR </path/to/workdir>
And More components to be used.
Example No. - 1
In this example run GUI in the docker Container.
Step1: First we have complete the docker installation setup in rhel 8. And after that follow this step.
Step2 : Create a directory
> mkdir GUI

make directory
Step3 : Change the directory that means go out the created Directory
> cd GUI

change the directory
Step4 : create a docker file use the vi command.
> vi dockerfile

create docker file
Step5 : To create a file for the firefox docker container image. (hint — for start typing press shift+ i )write the code .
from centos
run yum install firefox -y
cmd firefox
And after writing the code press- esc then type :wq and hit the Enter

Step6 : To Build the Docker container.
-> docker build -t firefox .

build the docker container
Step7: This is the final step. In this step to run the docker container.
-> docker run -it — name firefox — env=”DISPLAY” — net=host firefox

Finally we have show the firefox GUI
Example No. — 2
In this example predict the salary using the machine learning model.
Now, we will create a Docker image using Dockerfile
If you are interested in how to use a Dockerfile to create an image, you probably already have Docker installed on your system.
The first thing you need to do is to create a directory in which you can store all the Docker images you build.
Step1. we will create a directory and name is mlwork and move it into the directory with the commands
mkdir mlwork
cd mlwork
Step2. Now, we will create a dockerfile with the help of the below command
vi dockerfile
Step3. In this docker file we have adds the components. (hint — for start typing press shift+ i )write the code.
FROM centos:latest
RUN yum install python3 -y
RUN pip3 install pandas
RUN pip3 install scikit-learn
COPY salary_data.csv /
COPY salaryPredict.py /
ENTRYPOINT [“python3”, “/salaryPredict.py”]
CMD [“0”]
And after writing the code press- esc then type :wq and hit the Enter and we will already read about the used components above.

Step4. Put the salary_date.csv and salary_predict.py file in the working directory (mlwork Directory).
For download salary data dataset and python file to click the below link
https://github.com/TinkalShakya09/Salary_prediction-using-dockerfile/
Step5. next step is to build a Docker Image with Dockerfile using the command. If you are already in the directory where the Dockerfile is located, put a .
instead of the location
docker build -t salary_pred:v1 .

Run the below command and check the docker image is created or not
docker images

Step 6. Launch a new Docker container based on the image you created in the previous steps. We will name the container “ml_pred” and create it with the command
docker run -it --name ml_pred salary_pred:v1

By default I give the year of Experience value is “0” in the docker file

Now I am given the year of Experience value is “3” and predicting the salary.
Example No - 2
In this example, we will create an image and run the python espeak-ng created tool.
Features of this Tool:-
-> You are given the option to choose the voice ( Hindi, Male English, and Female English Voice)
-> you can also send the E-Mail
-> you can see some Funny and Interesting things like — Burning fire, running train, Dragon, Turtle, Simulating Text, etc.
Now, we will the docker image
Again same you need to do is to create a directory in which you can store all the Docker images you build.
Step1. we will create a directory and name is python_espeak and move it into the directory with the commands
mkdir python_espeak
cd python_espeak
Step2. Now, we will create a dockerfile with the help of the below command
vi dockerfile
Step3. In this docker file we have adds the components. (hint — for start typing press shift+ i )write the code from given the image

After writing the code press- esc then type :wq and hit the Enter and we will already read about the used components above and in this dockerfile we need to install many libraries. because they use python code.
Step4. Put the python code in the working directory (python_espeak Directory).

For download python code to click the below link
https://github.com/TinkalShakya09/linux-tool-using-python-and-espeak-ng
Step5. The next step is to build a Docker Image with Dockerfile using the command. If you are already in the directory where the Dockerfile is located, put an .
instead of the location
docker build -t python_espeak:v1 .

Run the below command and check the docker image is created or not
docker images
Step 6. Launch a new Docker container based on the image you created in the previous steps. We will name the container “python_espeak” and create it with the command
docker run -it — name ml_pred python_espeak:v1

they successfully created and properly run and work it.
if you want to show the demo video of the tool then click the below link- https://bit.ly/3EiNvHr
I hope you like this article about DockerFile components and examples