Dockers — How to build a Dockerfile — Tutorial 3

Gal Hever
2 min readJan 10, 2022

Intro

This tutorial will focus on the commands that build a Dockerfile. If you never red about Dockers in the past I really recommend you to read the previous tutorials before you start:

Docker — Basic Actions and Installations — Tutorial 1

Dockers — Build your first Image — Tutorial 2

A Short Recap

A Dockerfile is an instructions file that describes how the image should be built. Now, we will go over the content of this file and explain in more detail about each command.

From

The command ‘From’ determines what is the base image for the new image that we want to build. This image is supposed to fit to the host operation system. Every image is initialized with some base image that is used as an infrastructure for the new image, we will never start to build an image from zero. For example, if we build a python app we can use a python image as a base image:

From python:3.10.0

RUN

If there are required packages for running our docker we can use the RUN command as below:

RUN pip install --trusted-host pypi.python.org -r requirements.txt

We need to specify all the packages that are required to install before we run the docker in the “requirements” file and to save it in the same path.

WORKDIR

“WORKDIR” command sets the working directory in the container.

COPY

“COPY” command copies the content of the local src directory to the working directory.

EXPOSE

“EXPOSE” make the specified port available to the world. But, whoever wrote the image doesn’t know where the container is going to run and which containers are going to run in parallel, that is why when we run the container we need connect between the port that is open to the one that is available on my computer. For example:

docker run -p 8888:80 helloworld

8888 is the entry point on my computer and 80 is the open port that was designated on Dockerfile.

ENTRYOINT

The ‘ENTRYPOINT’ command determines which app to activate. This is the command that is running on the container when we it starts.

A Full Dockerfile for Example

Now that we went over all the commands, we know how to read Dockerfiles! Below you can see a full example:

FROM python:3.10.0

WORKDIR /code

COPY . .

RUN pip install --trusted-host pypi.python.org -r requirements.txt

EXPOSE 80

ENV NAME World

CMD [ "python", "app.py" ]

End Notes

This is the third and last tutorial of the serie blog-posts that I wrote about Dockers. I hope that now you have a better understanding of how it actually works in practice and that you already built your own image with an adjusted Dockerfile according to this tutorial:)

--

--