> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Work on creating the Dockerfile

> Creating a Dockerfile to containerize a Flask app, install dependencies, expose port, and run or prepare for production deployment with recommendations.

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](https://docs.docker.com/get-docker/)).

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

```dockerfile theme={null}
# Use an official Python runtime as a parent image
FROM python:3.10-slim
```

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.

2. Set the working directory inside the container

```dockerfile theme={null}
# Set the working directory in the container
WORKDIR /usr/src/app
```

Explanation: `WORKDIR` sets the working directory for subsequent commands and the process run inside the image. It helps keep paths consistent.

3. Copy the application source code and install dependencies from `requirements.txt`

```dockerfile theme={null}
# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r 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.

4. Expose the Flask default port (5000) and define environment variables and the run command

```dockerfile theme={null}
# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variables for Flask
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0

# Run the Flask application
CMD ["flask", "run"]
```

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.

<Callout icon="lightbulb" color="#1CB2FE">
  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.
</Callout>

Warning: development server vs production

<Callout icon="warning" color="#FF6B6B">
  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.
</Callout>

Final combined Dockerfile

```dockerfile theme={null}
# Use an official Python runtime as a parent image
FROM python:3.10-slim

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variables for Flask
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0

# Run the Flask application
CMD ["flask", "run"]
```

Quick reference table: Dockerfile directives and purpose

| Directive | Purpose                                          | Example / Notes                                      |
| --------- | ------------------------------------------------ | ---------------------------------------------------- |
| `FROM`    | Base image with runtime                          | `FROM python:3.10-slim`                              |
| `WORKDIR` | Sets working directory inside container          | `WORKDIR /usr/src/app`                               |
| `COPY`    | Copies project files into the image              | `COPY . .`                                           |
| `RUN`     | Runs commands at build time (e.g., install deps) | `RUN pip install --no-cache-dir -r requirements.txt` |
| `EXPOSE`  | Documents the port the container listens on      | `EXPOSE 5000`                                        |
| `ENV`     | Set environment variables inside the image       | `ENV FLASK_APP=app.py`                               |
| `CMD`     | Default command to run when container starts     | `CMD ["flask", "run"]`                               |

Build and test locally

1. Build the image (run from the project root where the Dockerfile resides):

```bash theme={null}
docker build -t flask-app .
```

2. Run the container and map the port to your host:

```bash theme={null}
docker run -p 5000:5000 flask-app
```

3. 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

* [Dockerfile reference — Docker Docs](https://docs.docker.com/engine/reference/builder/)
* [Flask documentation](https://flask.palletsprojects.com/)
* [Gunicorn — Python WSGI HTTP Server](https://gunicorn.org/)

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.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/building-scalable-microservices-on-aws-deploy-a-crypto-app/module/f2cfee46-980a-49cb-b81a-dd46bfce3824/lesson/9eacc776-f8d1-40e5-9269-a9fef1a9b4d3" />
</CardGroup>
