How to Deploy Flask Using Docker: Step-by-Step Guide
To deploy a Flask app using
Docker, create a Dockerfile that sets up Python, copies your app, installs dependencies, and runs the app. Then build the Docker image with docker build and run it using docker run to start your Flask app inside a container.Syntax
A typical Dockerfile for Flask deployment includes these parts:
- Base image: Starts with a Python image.
- Working directory: Sets where files go inside the container.
- Copy files: Moves your Flask app code into the container.
- Install dependencies: Uses
pipto install required packages. - Expose port: Opens the port Flask listens on.
- Run command: Starts the Flask app with
python.
dockerfile
FROM python:3.11-slim WORKDIR /app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 5000 CMD ["python", "app.py"]
Example
This example shows a simple Flask app deployed with Docker. It includes the Flask app code, a requirements.txt file, and a Dockerfile. You build the image and run the container to serve the app on port 5000.
plaintext
# app.py from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello from Flask in Docker!" if __name__ == '__main__': app.run(host='0.0.0.0', port=5000) # requirements.txt flask # Dockerfile FROM python:3.11-slim WORKDIR /app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . EXPOSE 5000 CMD ["python", "app.py"] # Commands to build and run: # docker build -t flask-docker-app . # docker run -p 5000:5000 flask-docker-app
Output
When you visit http://localhost:5000 in your browser, you will see: Hello from Flask in Docker!
Common Pitfalls
Common mistakes when deploying Flask with Docker include:
- Not setting
host='0.0.0.0'inapp.run(), so the app is not accessible outside the container. - Forgetting to expose the correct port in the
Dockerfileor map ports withdocker run -p. - Not copying all necessary files or missing dependencies in
requirements.txt. - Using the default Flask debug server in production (consider using a production server like Gunicorn).
python
# Wrong: app.py without host set from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello" if __name__ == '__main__': app.run() # Defaults to localhost only # Right: app.py with host set from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello" if __name__ == '__main__': app.run(host='0.0.0.0') # Accessible outside container
Quick Reference
Tips for deploying Flask with Docker:
- Always set
host='0.0.0.0'inapp.run(). - Use a slim Python base image for smaller containers.
- Keep dependencies in
requirements.txt. - Expose and map the correct port (usually 5000).
- Build images with
docker build -t your-app-name .. - Run containers with
docker run -p 5000:5000 your-app-name.
Key Takeaways
Create a Dockerfile that installs dependencies and runs your Flask app with host set to 0.0.0.0.
Build your Docker image using docker build and run it with docker run mapping the ports.
Expose the Flask port in the Dockerfile and map it correctly to access the app from outside.
Include all necessary files and dependencies to avoid runtime errors.
Avoid using Flask's default server in production; consider production-ready servers for real deployments.