Skip to main content
Welcome to this detailed guide on building a custom Docker image for a simple Python Flask web application. In this tutorial, you’ll learn how to set up the application, manually run it, containerize it using Docker, and finally push your image to Docker Hub. The complete project is available on my GitHub page.

Application Overview

Our web application comprises a single file, app.py, which defines two routes:
  • The default route (/) displays a welcome message.
  • The /how are you route returns the response “I am good, how about you?”.
Below is the complete source code for app.py:

Deploying the Application Manually

Before containerizing the application, you can deploy it manually on an Ubuntu host. Follow these steps:

Installing Dependencies

Update your package lists and install Python along with required packages:
Next, install Flask and its MySQL helper using pip:
To run the application manually, execute the command below:
Once running, access the following URLs in your web browser:
  • http://<IP>:5000 – should display “Welcome!”
  • http://<IP>:5000/how%20are%20you – should display “I am good, how about you?”
The console output will look similar to:

Running the Application Inside a Docker Container

Containerizing your application isolates the environment and simplifies dependency management.

Starting an Ubuntu Container

Launch an interactive Ubuntu container with a bash shell:
Inside the container, update the package index and install Python:
If you encounter errors while installing Python due to an outdated package index, make sure to run apt-get update before attempting the installation.

Installing pip and Flask

If you receive a “pip: command not found” error, install pip:
Then install Flask using pip:
Successful installation will display messages confirming that Flask and its dependencies (itsdangerous, click, Werkzeug, Jinja2, MarkupSafe) have been installed.

Running the Application

After copying your application code into the container (for example, place it in /opt/app.py), run the application with:
The expected output is:
You can now access the application using the container’s IP on port 5000. Visiting the /how%20are%20you route should trigger a GET request and display the correct response.

Recording the Steps

It’s a good practice to record the commands executed for troubleshooting or later use. Below is an example command history:
Keep these steps handy as you move forward to Dockerize your application.

Dockerizing the Application

After verifying your web application inside a Docker container, you can now create a Docker image.

Creating a Dockerfile

Create a project directory (e.g., my-simple-webapp) and add a file named Dockerfile with the following content:
Ensure that your app.py file is in the same directory as your Dockerfile.

Building the Image

Build your Docker image by running:
Docker caches each layer, so subsequent builds without changes will be faster. To verify the image was built, list your Docker images:
Your image my-simple-webapp should appear in the list.

Running the Docker Image

To run your containerized application, execute:
Without port mapping, the application is accessible only from the host or via Docker’s internal IP. To make the application accessible externally, run:
Then, navigate to http://<HOST_IP>:5000 in your web browser.

Pushing the Image to Docker Hub

Sharing your application on Docker Hub is simple. Follow these steps:
  1. Tag your image using your Docker Hub username (e.g., if your username is mmumshad):
  2. Log in to Docker Hub:
    Enter your username and password when prompted.
  3. Push the image to Docker Hub:
If you encounter an error like “requested access to the resource is denied,” ensure that you have tagged your image with your Docker Hub account name and that you are logged in. Upon a successful push, your image will be available in your Docker Hub repository. You can view it on your Docker Hub dashboard.
The image shows a Docker Hub webpage listing various public repositories with details on stars, pulls, and options to view more information.
Others can pull your image by running:
For private images, note that free Docker Hub accounts are limited to one private repository.
The image shows a Docker Hub repository settings page, offering options to make the repository private or delete it, with warnings about irreversible actions.

Summary

In this guide, we covered:
  • Setting up a basic Flask web application.
  • Manually installing dependencies and running the application on an Ubuntu host.
  • Containerizing the application inside an Ubuntu Docker container.
  • Recording the installation and execution steps.
  • Creating a Dockerfile to build a custom image.
  • Building, running, and verifying the Docker image.
  • Tagging and pushing the image to Docker Hub for public distribution.
Practice these steps to master Docker containerization and share your applications with the community. Happy containerizing!

Watch Video

Practice Lab