Skip to main content
Welcome back. In this lesson we’ll create a Dockerfile to containerize a Flask application so it can be built, tested locally, and deployed in a scalable way. The steps below walk you through creating a minimal, production-friendly Dockerfile based on a slim Python image, placing the app in a working directory, installing dependencies, and exposing the Flask port. Prerequisites:
  • A Flask app with an entry point named app.py.
  • A requirements.txt file listing Python dependencies.
  • Docker installed locally (see Docker documentation).
Open your Cloud9 editor, right-click the project folder, choose New File, and name it Dockerfile. Double-click to open the empty file and add the following content step by step.
  1. Choose a lightweight Python base image
Explanation: python:3.10-slim provides a small footprint image with the Python runtime you need. Using a slim base reduces build time and image size.
  1. Set the working directory inside the container
Explanation: WORKDIR sets the working directory for subsequent commands and the process run inside the image. It helps keep paths consistent.
  1. Copy the application source code and install dependencies from requirements.txt
Explanation: COPY . . copies your project into the image. The RUN pip install --no-cache-dir -r requirements.txt installs dependencies without caching to keep the image lean.
  1. Expose the Flask default port (5000) and define environment variables and the run command
Explanation: EXPOSE 5000 documents the port the app listens on. Setting FLASK_RUN_HOST=0.0.0.0 ensures the Flask development server listens on all network interfaces inside the container so the service is reachable from outside the container. The CMD runs the Flask development server by default.
If you want to avoid sending build-context files into the image (for example .git or local virtualenvs), add a .dockerignore file listing those paths.
Warning: development server vs production
The Flask development server is not intended for production use. For production deployments, replace the flask run command with a production-grade WSGI server such as gunicorn (for example: CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]), and ensure proper configuration for logging, health checks, and process management.
Final combined Dockerfile
Quick reference table: Dockerfile directives and purpose Build and test locally
  1. Build the image (run from the project root where the Dockerfile resides):
  1. Run the container and map the port to your host:
  1. Visit http://localhost:5000 in your browser to verify the app is running.
Troubleshooting tips
  • If dependencies fail to install, check requirements.txt for platform-specific packages or missing packages.
  • Use docker build --no-cache -t flask-app . to rebuild from scratch if you suspect a caching issue.
  • Inspect the running container logs with docker logs <container-id>.
Links and references With the Dockerfile in place you can now build and run the image locally to verify the containerized app works as expected. The next section can walk you through pushing the image to a registry or deploying it to a container platform.

Watch Video