Dockerize a Flask Application

1 hour
  • 5 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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.
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
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.
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 __init__.py below the import statements:
      from dotenv import load_dotenv, find_dotenv
      load_dotenv(find_dotenv())
  3. 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 0.0.0.0:80", "notes:create_app()"]
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.

Additional Resources

Scenario

You have successfully migrated your company's website into a container. Thanks to your efforts, you have proven the value of containers to your boss and have now been tasked with migrating another application into a container. This time, however, it's a full application and not just a static website. Using the knowledge you've gained from working with static content, migrate the Notes application into a container.

This lab will be using the Notes application available in the notes folder in your user's home directory.

If you want to learn to build this application yourself, check out the fantastic course Introduction to Python Development on A Cloud Guru or Linux Academy.

Logging in to the lab environment

Log in to the server using the credentials provided:

ssh cloud_user@<PUBLIC_IP_ADDRESS>

What are Hands-on Labs

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.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?