- A Flask app with an entry point named
app.py. - A
requirements.txtfile listing Python dependencies. - Docker installed locally (see Docker documentation).
Dockerfile. Double-click to open the empty file and add the following content step by step.
- Choose a lightweight Python base image
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.
- Set the working directory inside the container
WORKDIR sets the working directory for subsequent commands and the process run inside the image. It helps keep paths consistent.
- Copy the application source code and install dependencies from
requirements.txt
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.
- Expose the Flask default port (5000) and define environment variables and the run command
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.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.
Build and test locally
- Build the image (run from the project root where the Dockerfile resides):
- Run the container and map the port to your host:
- Visit
http://localhost:5000in your browser to verify the app is running.
- If dependencies fail to install, check
requirements.txtfor 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>.