You already know how docker is popular among deployers. Just download the existing docker image from the server and works directly inside the container. But if there is something that you want to do inside the docker then you have to do manually. To solve this issue we always create a docker file and using it we build a docker image. In this entire post, you will learn how to build docker image from dockerfile
Suppose the following things I want to do inside the container.
- Download Ubuntu Os.
- Update the OS
- Install the Nano editor
- Install the Python editor
- Make a Python file.
- Run the python file
You can easily do all these things inside the container manually. But for doing all these things automatically you have to build a Dockerfile. Let’s Build the docker file. I have done all the things in Ubuntu OS.
Step1: Create a file with the name Dockerfile.
sudo nano Dockerfile
Step 2: Download or pull Ubuntu OS from the Docker hub.
To pull or download the latest version of the ubuntu os uses the FROM command. Write inside the docker file.
FROM ubuntu: latest
Here the latest is the version tag that tells will tell the docker to pull the latest version of the Ubuntu OS.
Step 3: Update the OS and Install the Nano editor
To update and install you already know you have run the apt-get command in ubuntu.
apt-get -y update
For installing Nano editor
apt-get install nano
So, you have to tell the docker to run this command after step 2.
RUN apt-get -y update
RUN apt-get install nano
Step 4: Install the Python Editor
Just like step 3 for installation, you have to tell the docker to run the command for installing python.
RUN apt-get install -y python3
Step 5: After step four now let’s build the image using the docker file. Run the below command.
docker build -t python-dsl
It will create a docker image with the name python-dsl.
Full Dockerfile Lines
FROM ubuntu:latest RUN apt-get -y update RUN apt-get install nano RUN apt-get install -y python3
How to run the python file inside the container?
Now You have successfully created an image from the docker file. Let’s edit the docker file and make some changes to run the python file using the python-dsl image.
Edit the Dockefile and Write the following command
FROM python-dsl:latest ADD run.py /run.py CMD ["python3","run.py"]
Dockerfile says start with the python-dsl image and copy the run.py from the host to the container and at last run the python file using the command python3 run.py. Let’s build the container using the same command above.
docker build -t run-python .
After building, run it you will see the following output.
docker run -ti --rm run-python
Hello Data Science learner
Doing this way you can start the work where you have left. That’s why docker is very popular. I hope this article helps you to clear your question on how to build a docker image from the dockerfile. If you have any question then contact us. You can also follow the official Docker Documentation for more queries.