Menu

US Region

Grandmetric LLC
Lewes DE 19958
16192 Coastal Hwy USA
EIN: 98-1615498
+1 302 691 94 10
info@grandmetric.com

EMEA Region

GRANDMETRIC Sp. z o.o.
ul. Metalowa 5, 60-118 Poznań, Poland
NIP 7792433527
+48 61 271 04 43
info@grandmetric.com

UK

Grandmetric LTD
Office 584b
182-184 High Street North
London
E6 2JA
+44 20 3321 5276
info@grandmetric.com

  • 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.

    Syntax

    List of the most critical commands:

    • Selection of a source image.
      FROM IMAGE[:TAG] [AS NAME]

      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.
      ENTRPOINT [EXECUTABLE_COMMAND]
    • 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.
      ENV NAME VALUE or ENV NAME1=VALUE1 NAME2=VALUE2
    • Change path for next commands.
      WORKDIR /PATH
    • Copy file with context to a container.
      COPY PATH_TO_FILE PATH_TO_FILE_DESTINATION
    • Auto check docker status.
      HEALTHCHECK [OPTIONS] CMD [COMMAND]
      

    Example

    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:

    flask_on_docker/
    ├── app.py
    ├── 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 app.py 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__':
        app.run(debug=True, host='0.0.0.0')

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

    flask
    flask_restful

    The last file is Dockerfile.

    FROM python:3.7
    COPY . /app
    WORKDIR /app
    RUN pip3 install -r requirements.txt
    ENTRYPOINT ["python3"]
    CMD ["app.py"]
    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 app.py 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 app.py"    7 seconds ago       Up 5 seconds (healthy)   0.0.0.0:5000->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:

    Next:

    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
     
    Grandmetric