Home Explore Blog Models CI



docker

2nd chunk of `content/guides/python/develop.md`
8d9079f9672a036456048d024eb6cdb912ba1fe2939b694a0000000100000fa0
   Create a file named `Dockerfile` with the following contents.

   ```dockerfile {collapse=true,title=Dockerfile}
   # syntax=docker/dockerfile:1

   # Comments are provided throughout this file to help you get started.
   # If you need more help, visit the Dockerfile reference guide at
   # https://docs.docker.com/go/dockerfile-reference/

   # Want to help us make this template better? Share your feedback here: https://   forms.gle/ybq9Krt8jtBL3iCk7

   ARG PYTHON_VERSION=3.11.4
   FROM python:${PYTHON_VERSION}-slim as base

   # Prevents Python from writing pyc files.
   ENV PYTHONDONTWRITEBYTECODE=1

   # Keeps Python from buffering stdout and stderr to avoid situations where
   # the application crashes without emitting any logs due to buffering.
   ENV PYTHONUNBUFFERED=1

   WORKDIR /app

   # Create a non-privileged user that the app will run under.
   # See https://docs.docker.com/go/dockerfile-user-best-practices/
   ARG UID=10001
   RUN adduser \
       --disabled-password \
       --gecos "" \
       --home "/nonexistent" \
       --shell "/sbin/nologin" \
       --no-create-home \
       --uid "${UID}" \
       appuser

   # Download dependencies as a separate step to take advantage of Docker's    caching.
   # Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
   # Leverage a bind mount to requirements.txt to avoid having to copy them into
   # into this layer.
   RUN --mount=type=cache,target=/root/.cache/pip \
       --mount=type=bind,source=requirements.txt,target=requirements.txt \
       python -m pip install -r requirements.txt

   # Switch to the non-privileged user to run the application.
   USER appuser

   # Copy the source code into the container.
   COPY . .

   # Expose the port that the application listens on.
   EXPOSE 8001

   # Run the application.
   CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=8001
   ```

   Create a file named `compose.yaml` with the following contents.

   ```yaml {collapse=true,title=compose.yaml}
   # Comments are provided throughout this file to help you get started.
   # If you need more help, visit the Docker Compose reference guide at
   # https://docs.docker.com/go/compose-spec-reference/

   # Here the instructions define your application as a service called "server".
   # This service is built from the Dockerfile in the current directory.
   # You can add other services your application may depend on here, such as a
   # database or a cache. For examples, see the Awesome Compose repository:
   # https://github.com/docker/awesome-compose
   services:
     server:
       build:
         context: .
       ports:
         - 8001:8001
   # The commented out section below is an example of how to define a PostgreSQL
   # database that your application can use. `depends_on` tells Docker Compose to
   # start the database before your application. The `db-data` volume persists the
   # database data between container restarts. The `db-password` secret is used
   # to set the database password. You must create `db/password.txt` and add
   # a password of your choosing to it before running `docker compose up`.
   #     depends_on:
   #       db:
   #         condition: service_healthy
   #   db:
   #     image: postgres
   #     restart: always
   #     user: postgres
   #     secrets:
   #       - db-password
   #     volumes:
   #       - db-data:/var/lib/postgresql/data
   #     environment:
   #       - POSTGRES_DB=example
   #       - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
   #     expose:
   #       - 5432
   #     healthcheck:
   #       test: [ "CMD", "pg_isready" ]
   #       interval: 10s
   #       timeout: 5s
   #       retries: 5
   # volumes:
   #   db-data:
   # secrets:
   #   db-password:
   #     file: db/password.txt
   ```

   Create a file named `.dockerignore` with the following contents.

   ```text {collapse=true,title=".dockerignore"}
   # Include any files or directories that you don't want to be copied to your

Title: Dockerfile and Compose File Setup
Summary
This section provides the content for a `Dockerfile` which sets up a Python 3.11.4 environment, installs dependencies from `requirements.txt`, creates a non-privileged user, copies the source code, exposes port 8001, and runs the application using uvicorn. It also gives the content for a `compose.yaml` file to define the application as a service and an example of how to define a PostgreSQL database as a service with persistent data. Finally, the content of a `.dockerignore` file is provided.