返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
docker

Use PDM to manage dependencies in a Python project's Docker image.

Using PDM in Docker to replace pip? Modify the Dockerfile to adjust compose settings, disable virtual environment and set the path. For uwsgi, a gcc image is required; slim won't work. PDM runs more smoothly than poetry and is barely usable.

曦远 (DealiAxy)约 10 分钟
目录9 个章节

Preface

In the DjangoStarter project, I have been using pdm as the default package manager and no longer use pip directly.

Therefore, when deploying, the Dockerfile and docker-compose configurations also need to be adjusted.

dockerfile

First, modify the dockerfile.

ARG PYTHON_BASE=3.11

FROM python:$PYTHON_BASE

Set Python Environment Variables

ENV PYTHONUNBUFFERED=1

Disable Update Check

ENV PDM_CHECK_UPDATE=false

Setting up Domestic Sources

RUN pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple/ &&
# Install pdm pip install -U pdm &&
# Configure mirror pdm config pypi.url "https://mirrors.cloud.tencent.com/pypi/simple/"

Copy Files

COPY pyproject.toml pdm.lock README.md /project/ COPY . /project/

Install Dependencies and Project into Local Package Directory

WORKDIR /project RUN pdm install --check --prod --no-editable &&
pip install uwsgi

ENV PATH="/project/.venv/bin:$PATH"


Here are a few points to note:

- If you need to use uwsgi, you must use the Python image without slim, as uwsgi's compilation depends on gcc, which is absent in slim images.
- By default, pdm will create a virtual environment. Since we are inside a Docker image, you might not need a virtual environment. In this case, you can disable it by configuring `pdm config python.use_venv false`.
- The environment variable configuration on the last line is crucial. It is necessary to add the virtual environment created by pdm to the PATH, so that the correct environment is used when executing commands later in the compose setup.
## docker-compose configuration

As usual, let's start with the configuration, and the notes will follow.

```yaml
services:
  redis:
    image: redis
    restart: unless-stopped
    container_name: $APP_NAME-redis
    expose:
      - 6379
    networks:
      - default
  web:
    container_name: $APP_NAME
    restart: always
    build: .
    environment:
      - ENVIRONMENT=docker
      - URL_PREFIX=
      - DEBUG=true
    command: pdm run ./src/manage.py runserver 0.0.0.0:8000
    # command: uwsgi uwsgi.ini
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - redis
    networks:
      - default
      - swag

networks:
  default:
    name: $APP_NAME
  swag:
    external: true

Notes:

  • The newer versions of compose no longer require a version number in the configuration file, so the version line on the first line can be removed.
  • All containers have been configured with container_name. The container names are set using environment variables. My approach is to create a .env file in the root directory to store these variables. They can also be specified on the command line.
  • The network name is also based on the APP_NAME environment variable, as mentioned above.
  • The entry command here has been changed to pdm run ./src/manage.py runserver 0.0.0.0:8000. Using pdm run automatically activates the virtual environment.
  • The uwsgi version can be used, but this compose configuration doesn't include an NGINX container. After the entire project is started, swag (likely a container itself) might not handle the project's static files well. Therefore, it's still necessary to include a lightweight web server like NGINX or Candy in the compose file (in fact, NGINX is already very lightweight).

Summary

That's it—just a few minor details.

It's worth mentioning that PDM is much smoother to use than Poetry, and I haven't encountered any strange issues when using it in Docker. Positive review 👍

Resources

_EOF_

Use PDM to manage dependencies in a Python project's Docker image.

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…

Use PDM to manage dependencies in a Python project's Docker image. · StarBlog