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

# Python Tutorial

> Build a secure and minimal python app using the Minimus python image

In this tutorial, we will build a custom Python app using Docker Compose and a multi-stage Dockerfile. The build stage uses `latest-dev` because it requires the package installer `pip`. The runtime stage uses the fully distroless image to achieve the most secure app.

<Steps>
  <Step title="Create Docker Compose file">
    Create a project directory and save the code below to a new `docker-compose.yml` file:

    ```yaml theme={null}
    services:
      flask-app:
        build: ./app
        ports:
          - "5000:5000"
    ```
  </Step>

  <Step title="Create a Python project">
    Create a subdirectory and name it `app`. Add the following to it:

    * Save the following sample script as a new `main.py` file.

      ```python expandable theme={null}
      from flask import Flask
      from datetime import datetime

      app = Flask(__name__)

      @app.route("/")
      def show_time():
          now = datetime.now()
          return f"Current date and time: {now.strftime('%Y-%m-%d %H:%M:%S')}"

      if __name__ == "__main__":
          app.run(host='0.0.0.0', port=5000)
      ```
    * Save a `requirements.txt` file to list the Python packages that the project depends on. Python’s default package installer `pip` uses it. For our simple example, save only:

      ```markdown theme={null}
      flask>=3.1.1
      ```
  </Step>

  <Step title="Create the Dockerfile">
    In the same directory, save the code below to a new `Dockerfile`:

    ```dockerfile expandable theme={null}
    # === Build Stage ===
    FROM reg.mini.dev/python:latest-dev as builder

    WORKDIR /app

    RUN python -m venv venv
    ENV PATH="/app/venv/bin":$PATH
    COPY requirements.txt requirements.txt
    RUN pip install -r requirements.txt

    # === Runtime Stage ===
    FROM reg.mini.dev/python:latest

    WORKDIR /app

    COPY main.py main.py
    COPY --from=builder /app/venv /app/venv
    ENV PATH="/app/venv/bin:$PATH"

    ENTRYPOINT ["python", "main.py"]
    ```

    Note that the builder stage uses `reg.mini.dev/python:latest-dev` so it can utilize PIP. The runtime stage uses the fully distroless production image - `reg.mini.dev/python:latest`.
  </Step>

  <Step title="Review the project directory">
    Your project directory should now look like this:

    ```
    your-project-root/
    ├── docker-compose.yml
    └── app/
        ├── Dockerfile
        ├── main.py
        └── requirements.txt
    ```
  </Step>

  <Step title="Authenticate to the Minimus registry">
    Before building the app, authenticate to the Minimus registry using your Minimus token:

    ```bash theme={null}
    docker login reg.mini.dev -u minimus -p {pull-token}
    ```
  </Step>

  <Step title="Build the app">
    You are now ready to build the app using Docker Compose:

    ```bash theme={null}
    docker compose build
    ```

    Docker Compose will build the app from the `app` folder. Once built, you will see a confirmation:

    ```
    ✔ flask-app  Built
    ```
  </Step>

  <Step title="Run and test the app">
    Run the app:

    ```
    docker compose up
    ```

    Test the app by sending it a request:

    ```
    curl http://127.0.0.1:5000
    ```

    You should get a response with the current date and time.
  </Step>

  <Step title="Clean up">
    Once ready to clean up, run the following command to remove the container:

    ```
    docker compose down
    ```
  </Step>
</Steps>
