我正在尝试使用 docker 启动 postgres 容器
- 我尝试过使用多个版本的 postgres docker 镜像
- 我曾尝试使用上述卷这里
- 尝试将
POSTGRES_USER
环境变量设置为 root,因为 root 是 /var/lib/postgres 的所有者 - 尝试使用
--user root
但仍然没有使用
用于启动 postgres 容器的命令
docker run -d -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres:15.2-alpine
错误:
data directory "/var/lib/postgresql/data" has wrong ownership
日志:
chmod: /var/run/postgresql: Operation not permitted
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 20
selecting default shared_buffers ... 400kB
selecting default time zone ... UTC
creating configuration files ... ok
2023-04-24 21:50:29.172 UTC [53] FATAL: data directory "/var/lib/postgresql/data" has wrong ownership
2023-04-24 21:50:29.172 UTC [53] HINT: The server must be started by the user that owns the data directory.
child process exited with exit code 1
initdb: removing contents of data directory "/var/lib/postgresql/data"
running bootstrap script ... %
我正在使用 arch linux(内核 v6.2)和 docker(v23)
如何解决此错误?
答案1
使用命名卷而不是绑定卷
version: "3"
services:
db:
image: postgres:11.2
environment:
- POSTGRES_USER=test
- POSTGRES_PASSWORD=test
- POSTGRES_DB=test
volumes:
- postgres-data:/var/lib/postgresql/data
ports:
- "5433:5432"
请注意左侧没有斜杠,这将创建一个未从主机系统安装位置的命名卷,并导致权限错误。