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.
目录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
composeno longer require a version number in the configuration file, so theversionline 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.envfile 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_NAMEenvironment variable, as mentioned above. - The entry command here has been changed to
pdm run ./src/manage.py runserver 0.0.0.0:8000. Usingpdm runautomatically activates the virtual environment. - The
uwsgiversion can be used, but thiscomposeconfiguration 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 thecomposefile (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 许可协议。转载请注明出处!
相关文章
解决Centos下docker容器无法联网
vi /etc/sysctl.conf 找到net.ipv4.ip_forward 设置允许转发 # Controls IP packet forwarding net.ipv4.ip_forward = 1 #使之生效 sysctl -p #重启docker systemctl restart docker
2021-12-18 · docker · 约 1 分钟
使用python-slim镜像遇到无法使用PostgreSQL的问题
python-slim镜像用PostgreSQL?报错libpq.so.5缺失,谁在乎?换psycopg2-binary搞定,或者手动安装库,麻烦死了。slim镜像小,PostgreSQL却不行,缺库谁管?用预编译包省事点。
2024-08-20 · docker · 约 1 分钟
真正的生产力来了!Docker迁移部署两步搞定!
打包应用目录和命名数据卷,再复制过去解压启动,过程繁琐得令人发指。导出导入数据卷更是鸡肋,还不如直接重装。一键迁移脚本倒是挺酷,但谁会用这种花里胡哨的东西?
2025-06-30 · docker · 约 1 分钟
一次生产环境的docker MySQL故障
问题 昨天下午本来要去吃下午茶,然后前端小伙伴突然说接口怎么崩了,我登上sentry一看,报错了 (2005, Unknown MySQL server host 'mysql' (-3)) 啥意思啊,连不上数据库,host是mysql因为这是在docker环境里 直接ssh连上服务器查看,没啥问题呀,MySQL容器也正常运行着 奇怪 然后我重启了一波docker,结果发现MySQL容器没有正常重
2022-05-27 · docker · 约 1 分钟
讨论
共 0 条加载评论中…