Post Image
By Daniel Tanque26 de Outubro, 2023In DevOpsDockerLearning

Docker – Dockerfile

Docker is a popular platform for developing, packaging, and deploying applications inside lightweight, self-contained containers. These containers are similar to virtual machines but more efficient and portable. Docker is used to package an application and its dependencies into a single unit called a container, ensuring that the application runs consistently across various environments, from a developer’s laptop to a production server.

A Dockerfile is a text-based script used to define and create a Docker image. Docker images are the basis for Docker containers, and a Dockerfile contains a set of instructions that specify how an image should be built.

In this article you will learn how to write a dockerfile so you can build an image, publish on docker hub and how it works.

Docker Images & Docker Hub

We have seen how to install a docker image, how to remove and overall use it. But you can create your own docker images, per example you have an application you can pass it as a docker image and ease the process of replication or scale your overall structure.

For that you can use the Dockerfile a document in which you state the technologies you want to use. Here is an example of a Dockerfile:

FROM Ubuntu

RUN apt-get update
RUN apt-get install python

RUN pip install flask
RUN pip install flask-mysql

COPY . /opt/source-code

ENTRYPOINT FLASK-APP=/opt/source-code/ flask run

As you can see it’s a script based file in which you can see the tasks that will be performed on running it.

Now to build it you run the following command (you need to be inside the folder that contains the Dockerfile):

docker build Dockerfile -t <name of the image>

One key aspect to have in mind is that the Dockerfile will be creating the container based on a layered architecture, stacking each line on top of the following. Per example we can se that firstly you have the OS setup, then the dependencies and libraries necessaries and then the part of the app.

Now once you have the dockerfile done you can easily migrate it to the Docker Hub, for that you need:

docker build -t <name of the organization>/<image name>
docker login
docker push

After that you can navigate to your profile on Docker Hub and check the image uploaded there. You just have one repository private, in free account mode.

ENV Variables and Commands

You can use environment variables and even merge with commands plus adding/passing arguments.

To know what variables and commands are available you can use the following command:

docker inspect <name of the image>

It’s possible to see in the Config section, under Env subsection.


Here you can see an example to deploy an app with port mapping and providing a specific name to the container, as well providing a environment variable:

docker run -p <desired port outside>:<existing port inside> --name <name of the container> -e <env TAG>=<value> -d <name of the image>

docker exec -it blue-app env

The -it tag will bring an interactive shell with access to the terminal.

Another example is to deploy a mysql database, for cases like this it’s important to check the documentation to ensure you use the proper environment TAGs, here is the example:

docker run -d -e MYSQL_ROOT_PASSWORD=db_pass123 --name mysql_db mysql

docker exec -it mysql_db env


Now you know how to setup your Dockerfile, change your application into an image, how to add variables and be able to publish onto Docker Hub.

svgDocker - Interoperability
svgClean Code - Functions

Leave a reply