Step 2: Setup the docker-compose environment

The goal for Step 2: Create a docker-compose.yml file with a defined volume for your Flask Quotes application.

Note

We’ll use port 20854 for the reverse proxy.

2.1. Create the docker-compose.yml file and add support files

  1. Create a folder for your Flask Quotes application called flask-quotes and create an empty docker-compose.yml file.

    cd ~
    mkdir flask-quotes
    cd flask-quotes
    touch docker-compose.yml
    
  2. Verify the information in the Dockerfile.

    Before you create your docker-compose.yml file, you will need information from your Dockerfile. Open it to verify that nothing has changed.

    WORKDIR /app    ----> For the volume   (where code files are)
    EXPOSE 5000     ----> For port mapping (the port that that the Python web server is running on)
    
  3. Write the docker-compose.yml file to use python/flask:dev and with defined a volume called quotes-app or something similar Here is your template (fill in the data from your Dockerfile):

    Destination volume

    The working directory (WORKDIR) defined in the image

    Destination port

    The port (EXPOSE) that the Python web server runs on

    Tip

    EXPOSE is a best-practice marker only. The actual port is defined in the index.py file

    app.run(host="0.0.0.0", port=int("5000"), debug=True)
    
    docker-compose.yml template
    version: '3'
    
    services:
       flask:
         image: python/flask:dev
         volumes:
           - ./quotes-app:/_____
         ports:
           - 20854:____
         restart: always
    
  4. Now, can create the volume with the index.py to create a basic hellow world Flask app, as a first-step for all new projects. 🧑‍💻🙂

    # All code files will go here
    mkdir quotes-app
    
    # copy the index.py file that you put inside of python/flask:dev
    cp ~/flask-dev-image/index.py quotes-app/
    
    # Your directory should look like this:
    sysadmin@test2:~/flask-quotes$ tree
    .
    ├── docker-compose.yml
    └── quotes-app
        └── index.py
    
    1 directory, 2 files
    
  5. Verify the config, start the container, and verify that it returns the expected result:

    sysadmin@test2:~/flask-quotes$
    sysadmin@test2:~/flask-quotes$ docker-compose config
    services:
      flask:
        image: python/flask:dev
        ports:
        - 20854:5000/tcp
        restart: always
        volumes:
        - /home/sysadmin/flask-working/flask-quotes/app:/app:rw
    version: '3.0'
    
    sysadmin@test2:~/flask-quotes$ docker-compose up -d
    Starting flaskquotes_html_1 ...
    Starting flaskquotes_html_1 ... done
    sysadmin@test2:~/flask-quotes$
    sysadmin@test2:~/flask-quotes$ curl localhost:20854
    Hello World! Привет, мир! Сәлем Әлем! ¡Hola Mundo! नमस्ते दुनिया! 안녕하세요! Hallo Welt! 你好,世界!
    sysadmin@test2:~/flask-quotes$
    

2.2. Create the reverse proxy

  1. Create sub-domain pandoc.example.com

  2. Create your Reverse Proxy using these settings

    • server_name quotes.example.com;

    • proxy_pass http://localhost:20854;

  3. Enable the Nginx Site

  4. Restart Nginx

  5. Verify using curl or using the web browser.