|
1 | 1 |
|
2 | 2 | # Python with LOGIC |
3 | 3 |
|
4 | | -TBD |
| 4 | +An opinionated base Docker image used when doing [Python with LOGIC](https://withlogic.co/ref/python). Optimized for development, CI builds and production deployments. |
| 5 | + |
| 6 | + %2C3.12%2C3.11%2C3.10-blue) %2C%20bookworm-purple?label=Variants) |
| 7 | + |
| 8 | +> [!TIP] |
| 9 | +> |
| 10 | +> Need help with a Python project in your organizations? You are at the right place. We have been working with production Python deployments for years and we would love to help. Let's get in touch at [https://withlogic.co](https://withlogic.co/ref/python). |
| 11 | +
|
| 12 | +## Why |
| 13 | + |
| 14 | +We built and published this opinionated Docker image, based on our workflow when doing Python with LOGIC. We prioritise DRY, considerate defaults and optimizing for speed and convenience across all steps in our workflow, from development to CI and deployments; preview or production. In particular this means: |
| 15 | + |
| 16 | +- Python and virtual environments optimized for Docker deployments |
| 17 | +- Optimized dependency management with uv |
| 18 | +- Preconfigured volumes for development with Docker |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +The simplest way to get started with this image is to use the `latest` tag and copy your source code in the current working directory: |
| 23 | + |
| 24 | +```dockerfile |
| 25 | +FROM ghcr.io/withlogicco/python |
| 26 | + |
| 27 | +COPY ./ ./ |
| 28 | +``` |
| 29 | + |
| 30 | +For advanced usage scenarios, including using uv with efficient Docker image caching take a look |
| 31 | + |
| 32 | +## Tags |
| 33 | + |
| 34 | +The `latest` tag ships with the most recent uv and Python versions on Debian Trixie: |
| 35 | + |
| 36 | +``` |
| 37 | +ghcr.io/withlogicco/python |
| 38 | +``` |
| 39 | + |
| 40 | +There are tags available to choose a specific supported Python versions and image variants: |
| 41 | + |
| 42 | +- Python version: `ghcr.io/withlogicco/python:{python_version}` |
| 43 | +- Image variant: `ghcr.io/withlogicco/python:{image_variant}` |
| 44 | +- Python version and image variant: `ghcr.io/withlogicco/python:{python_version}-{image_variant}` |
| 45 | + |
| 46 | +### Examples |
| 47 | + |
| 48 | +- Python 3.13 on Trixie: `ghcr.io/withlogicco/python` |
| 49 | +- Poetry 3.13 on Bookworm: `ghcr.io/withlogicco/python:bookworm` |
| 50 | +- Poetry 3.12 on Trixie: `ghcr.io/withlogicco/python:3.12` |
| 51 | +- Poetry 3.12 on Bookworm: `ghcr.io/withlogicco/python:3.12-bookworm` |
| 52 | + |
| 53 | +## Advanced usage |
| 54 | + |
| 55 | +### Start a new project with uv |
| 56 | + |
| 57 | +To start a new Python project with uv, mount your current working directory in `/usr/src/app` and just run `uv init`: |
| 58 | + |
| 59 | +```console |
| 60 | +docker run -ti -v $PWD:/usr/src/app ghcr.io/withlogicco/python uv init |
| 61 | +``` |
| 62 | + |
| 63 | +### Dependencies with uv |
| 64 | + |
| 65 | +To manage your dependencies with uv, it's suggested to just copy your `pyproject.toml` and `uv.lock` files in your Docker image first for optimized Docker build layer caching (`uv sync` will run only when your dependencies change): |
| 66 | + |
| 67 | +```dockerfile |
| 68 | +FROM ghcr.io/withlogicco/python |
| 69 | + |
| 70 | +COPY pyproject.toml uv.lock ./ |
| 71 | +RUN uv sync --no-install-project |
| 72 | + |
| 73 | +COPY . . |
| 74 | +``` |
| 75 | + |
| 76 | +> [!TIP] |
| 77 | +> |
| 78 | +> Prefer to use `--no-install-project` with `uv sync`, to only install dependencies and not the current project, since the code is not available yet in that particular build step. |
| 79 | +
|
| 80 | +### Optimize builds with cache mounts |
| 81 | + |
| 82 | +You can further optimize your Docker builds by taking advantage of Docker builder cache mounts. This means Docker can cache the `/root/.cache/uv` directory across builds, so that even when dependencies change and `uv sync` needs to run again, only the new dependencies will need to be downloaded from PyPI: |
| 83 | + |
| 84 | +```dockerfile |
| 85 | +FROM ghcr.io/withlogicco/python |
| 86 | + |
| 87 | +COPY pyproject.toml uv.lock ./ |
| 88 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 89 | + uv sync --no-install-project |
| 90 | +``` |
| 91 | + |
| 92 | +> [!IMPORTANT] |
| 93 | +> |
| 94 | +> Docker build cache mounts will **NOT** work on GitHub Actions, as they are not part of Docker image cache, but the builder daemon's. To get this feature working you will need to run your Docker builds on a host managed by you, with GitHub Actions self-hosted runners. |
| 95 | +
|
| 96 | +### Reduce layers with build bind mounts |
| 97 | + |
| 98 | +You can reduce layers of built Docker images, by mounting `pyproject.toml` and `uv.lock` in the Docker image just in time for `uv sync`, instead of copying them in an additional layer above: |
| 99 | + |
| 100 | +```dockerfile |
| 101 | +FROM ghcr.io/withlogicco/python |
| 102 | + |
| 103 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 104 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 105 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 106 | + uv sync --no-install-project |
| 107 | +``` |
| 108 | + |
| 109 | +### Persist new dependencies without rebuilding |
| 110 | + |
| 111 | +When working on an actively developed project, new dependencies can be added as the project moves forward. To avoid requiring rebuilding the Docker image at that case, you can mount a volume in `/opt/uv/venv`. |
| 112 | + |
| 113 | +The directory `/opt/uv/venv` has been configured as a volume in the image, which means that Docker volumes mounted there will be initialized with the data of the image. |
| 114 | + |
| 115 | +With Docker Compose the volume can be as simple as: |
| 116 | + |
| 117 | +```yml |
| 118 | +services: |
| 119 | + web: |
| 120 | + build: . |
| 121 | + volumes: |
| 122 | + - .:/usr/src/app |
| 123 | + - uv:/opt/uv/venv |
| 124 | +``` |
| 125 | +
|
| 126 | +> [!TIP] |
| 127 | +> |
| 128 | +> This is especially useful when working in small teams that do not update dependencies at the same time often. Rebuilding the image with new dependencies, will not udpate the contents of an existing Docker volume. |
| 129 | +
|
| 130 | +## Supported software versions |
| 131 | +
|
| 132 | +### uv |
| 133 | +
|
| 134 | +Only the latest version of uv is included in each build. |
| 135 | +
|
| 136 | +### Python |
| 137 | +
|
| 138 | +A build will be provided for each Python version still under maintenance and support. The latest Python version acts as the default in each build. |
| 139 | +
|
| 140 | +You can check the currently supported Python versions at https://devguide.python.org/versions/. |
| 141 | +
|
| 142 | +### Variants (Linux distributions) |
| 143 | +
|
| 144 | +- Debian Trixie (default): `trixie` |
| 145 | +- Debian Bookworm: `bookworm` |
| 146 | + |
| 147 | +## License |
| 148 | + |
| 149 | +This project is [MIT licensed](LICENSE). |
5 | 150 |
|
6 | 151 | --- |
7 | 152 |
|
|
0 commit comments