- 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.txtlistingFlask(and any other dependencies)- A
Dockerfileto containerize your application
Project Structure
1. Initial Dockerfile
Below is our startingDockerfile—note the typo in the CMD instruction that we’ll address later:
2. Building the Docker Image
Open your terminal and execute:3. Running the Container
Try starting the container and mapping container port 5000 to host port 5000:Common Port Binding Error
Flask Option Parsing Error
--host in the form --host=<address>, not --host:<address>.
4. Fixing the Dockerfile
Update theCMD line to use = instead of ::
5. Running Successfully
Start the container on host port 5001:6. Command Reference
| Command | Description |
|---|---|
docker build -t flask-docker-demo . | Build the Docker image for the Flask app |
docker images | List all local Docker images |
docker run -p HOST:5000 flask-... | Run container, mapping host port to container |
docker run -p 5001:5000 flask-docker-demo | Remap host port if default is unavailable |
Next Steps
- Commit your changes and push to GitHub.
- Integrate with a CI/CD pipeline for automated builds.
- Deploy to a cloud platform knowing your container behaves the same as it does locally.