Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon

Dockerize a Flask Application

Migrating static content into containers was a great way to learn the basics of Docker, but real-world uses are usually dynamic, including full applications. This lab will show you the process to dockerize a Flask application. Flask is a lightweight Python WSGI micro web framework, however, you won't need to know any Python to complete this lab.

Google Cloud Platform icon

Path Info

Clock icon Beginner
Clock icon 1h 0m
Clock icon Nov 20, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create the Build Files

    1. Inspect the Flask application files to learn what files we need to include and exclude from the image. There is a Pipfile.lock, a .gitignore, and a migrations directory. We don't want those in the image.
    2. Create a .dockerignore file to exclude build and metadata information from the image.
    3. Create the Dockerfile to build the image.
      • Start with Python 3 as the base image.
      • Setup Python environment variables.
      • Install dependencies in the container from the Pipfile.
      • Set the working directory.
      • Specify the port flask will listen on.
      • Start the flask server.
  2. Challenge

    Build and Setup Environment

    1. Build an image named notesapp with version 0.1.
    2. Inspect the Docker environment to see the new image, and what else is running.
    3. Use a container of the notesapp image to set up the database.

    Note: The database can be set up via the app itself using the following command. flask db init && flask db migrate && flask db upgrade

  3. Challenge

    Run, Evaluate, and Upgrade

    1. Run the NotesApp container in the current terminal to watch its output.
    2. View the application in a browser. Sign up for an account, create a note, then edit it.
    3. View the messages in the terminal and check for warnings.
    4. Stop using Flask in debug mode.
      • Remove the line FLASK_ENV=development from the file ~/notes/.env.
    5. Since we've changed a file included in the image, build the image again as version 0.2.
    6. Run the new version of the NotesApp image in the terminal. Debug mode should no longer be on.
    7. Look for any new warnings.

      Note: We're not in debug anymore, but now Flask tells us "Do not use it in a production deployment". That's not a good way to leave our container.

    8. Stop the container.
  4. Challenge

    Upgrade to Gunicorn

    We will upgrade our Flask application to use Gunicorn, which is a production-ready WSGI HTTP server. This will involve adding a bit of code to our existing app and upgrading our build.

    1. Add Gunicorn to the dependencies using pipenv.
      • Pipenv is being used to manage the dependencies for our application. Let's use the image we have, which includes pipenv, to upgrade the Pipfile so we don't have to install a development environment on the server.
    2. Upgrade the application code for Gunicorn.
      • Flask can use dotenv automatically, but Gunicorn must be told to use it explicitly. Add the following lines to below the import statements:
    from dotenv import load_dotenv, find_dotenv
    1. Change the Dockerfile to run Gunicorn instead of Flask.
      • Replace the WORKDIR and CMD in the Dockerfile with the below code:
    WORKDIR /app
    CMD [ "gunicorn", "-b", "notes:create_app()"]
  5. Challenge

    Build a Production Image

    1. Build the NotesApp image again. Remember to increment the version.
    2. Run a container from the production image, this time using detached mode to run in the background.
    3. Inspect the Docker environment to ensure everything is running.
    4. View the application in a browser. Use the server's public IP.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans