US Region

Grandmetric LLC
Brookfield Place Office
200 Vesey Street
New York, NY 10281
EIN: 98-1615498
+1 302 691 94 10

EMEA Region

ul. Metalowa 5, 60-118 Poznań, Poland
NIP 7792433527
+48 61 271 04 43

  • en
  • pl
  • Dockerfile – How to set up Docker

    Design & Configure

    Dockerfile – How to set up Docker

    Dockerfile – introduction

    According to Docker’s official documentation, Dockerfile is a text file that contains all commands, in order, needed to build a given image.

    It defines:

    • source image,
    • list of commands executed while creating the image,
    • list of commands to automatic run,
    • open network port,
    • volumes with databases.


    List of the most critical commands:

    • Selection of a source image.

      This command is optional; the default image is scratch. The image is download from Docker Hub.

    • Execution of commands during the creation of the image in a new layer.
      RUN COMMAND or RUN [“COMMAND”, ”ARG1”, “ARG2”]
    • Configuration of a container that will run as an executable.
    • Sets default command and/or parameters, which can be overwritten from the command line when docker container runs.
      CMD [“COMMAND”, “ARG1”, “ARG2”]
    • Set environment variables.
    • Change path for next commands.
    • Copy file with context to a container.
    • Auto check docker status.


    This example presents how to create a docker container with an active Flask application.

    Firstly we need to create a project folder and three necessary text files. The file tree should look like this:

    ├── Dockerfile
    └── requirements.txt

    Flask is a Python application; hence we can use a python3 base image. We can also use a Ubuntu image, but it is a redundant way since a python image is substantial enough. We should choose images that meet our requirements but use the least resources.

    File contains some basic python code implementing basic Flask API.

    from flask import Flask
    from flask_restful import Resource, Api
    app = Flask(__name__)
    api = Api(app)
    class HelloWorld(Resource):
        def get(self):
            return {'hello': 'world'}
    api.add_resource(HelloWorld, '/')
    if __name__ == '__main__':, host='')

    In python script, we import package flask and flask_restful so that we can write this to the requirements file.


    The last file is Dockerfile.

    FROM python:3.7
    COPY . /app
    WORKDIR /app
    RUN pip3 install -r requirements.txt
    ENTRYPOINT ["python3"]
    CMD [""]
    HEALTHCHECK --interval=15s --timeout=3s \
    CMD curl -f http://localhost:5000/ || exit 1

    The Dockerfile copies the current folder to folder app into the container then installs requirements located in the requirements.txt file and runs script in python.

    Additionally, we will add a HEALTHCHECK instruction, which checks every 15 seconds if it is possible to query our API correctly. If we get a reply, then the status container is healthy, if not unhealthy.

    Run docker with Dockerfile

    Build an image with parameter (‘-t’ – name and optionally a tag):

    docker build -t flask_on_docker .

    Run the created image with parameters (‘-d’ – run container in background and print container ID, ‘-p’ – publish a container’s port to the host):

    docker run -d -p 5000:5000 flask_on_docker

    Check the docker status with the command:

    ms@GRANDMETRIC:~/flask_on_docker$ docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS                    NAMES
    cd8d2e9b6874        flask_on_docker     "python3"    7 seconds ago       Up 5 seconds (healthy)>5000/tcp   funny_hoover

    If docker status is healthy, you can check response API using curl tools:

    #send GET request using curl
    curl localhost:5000

    or via a web browser:


    In this post, I showed you how to set up docker with Dockerfile. If you have already created a single docker container, try to create a few of them and make them work together. The communication between containers works similarly to microservice architecture. If you want to learn more about how to efficiently manage relationships, control, and run containers in one environment, here’s my recent post about container management.



    Author: Mateusz Sobkowiak