Encountering issues using PostgreSQL with the python-slim image.
Using python-slim images with PostgreSQL? Getting an error about missing libpq.so.5, who even cares? Just switch to psycopg2-binary to fix it, or manually install the library—what a hassle. Slim images are lightweight, but PostgreSQL doesn’t work because of missing libraries, and who’s responsible for that? Using a precompiled package is way easier.
目录8 个章节
Preface
Didn't we recently get the Docker setup for DjangoStarter working again?
Initially, the demo deployment was using SQLite, and it worked fine, but issues quickly arose when switching to PostgreSQL...
Error
After starting Docker, the app container throws an error
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 or psycopg module
Troubleshooting
At first, I thought the issue was either with installing the psycopg2 library or that dependencies from the python_builder stage weren't correctly copied to the final stage container. However, after adjusting a few things and retrying, it still didn’t work.
Next, I used python src/manage.py shell to enter the shell and tested whether the psycopg2 library could be imported.
>>> import psycopg2
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/project/.venv/lib/python3.11/site-packages/psycopg2/__init__.py", line 51, in <module>
from psycopg2._psycopg import ( # noqa
ImportError: libpq.so.5: cannot open shared object file: No such file or directory
Alright, experienced Docker users should have already spotted the issue—it’s related to the system environment.
Switching from the slim image to a standard Python image resolved the problem…
Solution
OK, now that the root cause has been identified, we can proceed to tackle it.
Using psycopg2-binary
The simplest way is to replace the psycopg2 library with psycopg2-binary, which is a pre-compiled psycopg2 package that doesn't require a compilation environment, making installation much more convenient.
Manually Installing the libpq Library
Since the libpq library is missing, let's install it within the Docker image.
I haven't tested this method yet; it's troublesome and not elegant.
Use Python 3.11 slim base image
FROM python:3.11-slim
Installing psycopg2 Dependencies
RUN apt-get update && apt-get install -y
libpq-dev
gcc
# Clear cache to reduce image size
&& apt-get clean
&& rm -rf /var/lib/apt/lists/*
## Different Python Base Images
Python images are based on the Debian system, using the latest stable version of Debian by default. You can also specify the version yourself; for example, `python3.11-slim-bookworm` indicates using Debian 12 ("bookworm") as the base image.
- The image built using the `python3.11-slim-bookworm` base image is 424MB in size, and the `python3.11-slim` base image produces the same size.
- Using the `python3.11` base image results in a size of 1.3GB, which is several times larger.
You can also opt for Alpine-based images, but they may come with more compatibility issues. I won’t bother with that—it’s mentally exhausting.
## Summary
Generally speaking, relying on slim images is perfectly fine. Since they are based on Debian, compatibility issues are rarely a concern.
Occasional minor problems are usually quite straightforward to resolve.
Alpine images, on the other hand, are a different story altogether and can be quite a handful...
_EOF_
Encountering issues using PostgreSQL with the python-slim image.
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
从挖矿木马入侵到 Docker Rootless 加固,我的服务器安全复盘
非要等服务器沦为矿场,才肯承认 Docker 默认 Root 权限是给黑客开后门?配置 Rootless 虽琐碎,但也比重装系统划算。连端口映射和 ACL 权限都搞不定,还是趁早关机退网,别在生产环境裸奔了。
2026-01-13 · docker · 约 1 分钟
真正的生产力来了!Docker迁移部署两步搞定!
打包应用目录和命名数据卷,再复制过去解压启动,过程繁琐得令人发指。导出导入数据卷更是鸡肋,还不如直接重装。一键迁移脚本倒是挺酷,但谁会用这种花里胡哨的东西?
2025-06-30 · docker · 约 1 分钟
国内使用docker镜像加速器
国内镜像源 国内的镜像源有 docker官方中国区 https://registry.docker-cn.com 网易 http://hub-mirror.c.163.com ustc http://docker.mirrors.ustc.edu.cn 阿里云 http://<你的ID>.mirror.aliyuncs.com 注意 注意registry-mirrors千万不要用https,而是用
2021-12-18 · 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 条加载评论中…