Skip to main content
Welcome to this step-by-step guide on building, testing, and debugging your Flask application locally within a Docker container. By the end of this tutorial, you’ll be able to:
  • Containerize a Flask app using Docker
  • Identify and fix common configuration typos
  • Run and verify your application on a custom host port
Ensure you have Docker installed and Python 3.8+ on your local machine before you begin.

Prerequisites

  • Python Flask application (app.py)
  • requirements.txt listing Flask (and any other dependencies)
  • A Dockerfile to containerize your application

Project Structure

1. Initial Dockerfile

Below is our starting Dockerfile—note the typo in the CMD instruction that we’ll address later:

2. Building the Docker Image

Open your terminal and execute:
You should see output similar to:
Verify the image exists:

3. Running the Container

Try starting the container and mapping container port 5000 to host port 5000:

Common Port Binding Error

If you see this, remap the host port (e.g., to 5001):

Flask Option Parsing Error

This happens because Flask expects --host in the form --host=<address>, not --host:<address>.

4. Fixing the Dockerfile

Update the CMD line to use = instead of ::
Rebuild the image:

5. Running Successfully

Start the container on host port 5001:
You should see:
Open your browser at http://localhost:5001 to confirm your Flask app is live.
This built-in Flask server is for development only. For production deployments, use a WSGI server like Gunicorn or uWSGI.

6. Command Reference

Next Steps

  1. Commit your changes and push to GitHub.
  2. Integrate with a CI/CD pipeline for automated builds.
  3. Deploy to a cloud platform knowing your container behaves the same as it does locally.

Watch Video