用Docker运行PostgreSQL时存在所有权问题

11
自昨天起,我一直在尝试在Windows上启动我的Docker项目(Bootcamp MacBook Pro),但仅剩一个问题:PostgreSQL镜像。
错误信息:
postgres_1         | The files belonging to this database system will be owned by user "postgres".
postgres_1         | This user must also own the server process.
postgres_1         |
postgres_1         | The database cluster will be initialized with locale "en_US.utf8".
postgres_1         | The default database encoding has accordingly been set to "UTF8".
postgres_1         | The default text search configuration will be set to "english".
postgres_1         |
postgres_1         | Data page checksums are disabled.
postgres_1         |
postgres_1         | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1         | creating subdirectories ... ok
postgres_1         | selecting default max_connections ... 20
postgres_1         | selecting default shared_buffers ... 400kB
postgres_1         | selecting dynamic shared memory implementation ... posix
postgres_1         | creating configuration files ... ok
postgres_1         | 2019-01-22 16:57:37.016 UTC [79] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
postgres_1         | 2019-01-22 16:57:37.016 UTC [79] HINT:  The server must be started by the user that owns the data directory.
postgres_1         | child process exited with exit code 1
postgres_1         | initdb: removing contents of data directory "/var/lib/postgresql/data"
postgres_1         | running bootstrap script ... kibati-docker_postgres_1 exited with code 1

我到处搜索,尝试了所有方法,但问题仍未解决...

我尝试的是:

  • 使用 docker volume create --name postgres -d local 命令创建Docker卷,并在我的 docker-compose.yml 文件中使用它

docker-compose.yml 文件内容:

version: '2'

services:
  postgres:
    image: postgres:latest
    ports:
      - "5432:5432"
    volumes:
      - postgres:/var/lib/postgresql/data

  networks:
    internal_ip:
      ipv4_address: 192.168.1.2

  volumes:
    postgres:
      external: true
  • 在docker compose中直接添加卷

卷示例

volumes:
  postgres:
    driver: local
  • 使用相对或绝对Windows路径
  • 为不同的PostgreSQL文件夹(conf,data...)声明多个卷

我尝试了docker compose down,重新启动计算机,删除映像,但仍然出现相同的错误。

我已经勾选了Docker设置中的Shared Drive框。

我尝试过以下方法:

https://glennsarti.github.io/blog/puppet-in-docker/

https://forums.docker.com/t/trying-to-get-postgres-to-work-on-persistent-windows-mount-two-issues/12456/5

https://forums.docker.com/t/data-directory-var-lib-postgresql-data-pgdata-has-wrong-ownership/17963/28

https://github.com/docker-library/postgres/issues/435

http://www.lukaszewczak.com/2016/09/run-postgresql-using-docker-with.html

https://gdevops.gitlab.io/tuto_docker/tutoriels/postgresql/postgresql.html

https://devask.cz/questions/48645804/mount-postgres-data-to-windows-host-directory

有没有人有一个可行的解决方案?如果我找到了什么,我会继续让它工作,并更新帖子。

提前致谢。


你是否遇到了与此问题答案中相同的音量问题?https://superuser.com/questions/789754/how-to-actually-install-mediawiki-and-run-with-docker> 对于带有卷的Docker镜像,当您下载镜像时,您需要在主机上创建一个类似的卷,因为下载Docker镜像不会下载Docker卷。 - George M Reinstate Monica
这就是我所做的,手动创建卷并将其绑定到docker-compose中的postgresql镜像。但它没有起作用。 - Azuken
3个回答

9

我终于找到了问题所在,原来是在尝试使用卷存储PostgreSQL数据时出了问题。

我不知道我们使用了一个docker-compose.override.yml文件,在其中声明了一个带有Windows路径的卷。

因此,这里提供一种可行的解决方案,让Windows上的Docker中持久化PostgreSQL数据:

version: '2'

services:
  postgres:
    image: postgres:11.5
    ports:
      - 5432:5432
    volumes: 
      - pgdata:/var/lib/postgresql/data
      - pgconf:/etc/postgresql
      - pglog:/var/log/postgresql

volumes:
  pgdata:
    driver: local
  pgconf:
    driver: local
  pglog: 
    driver: local

(不需要额外的命令)


0
这个解决方案对我有效: 运行命令docker volume create --name=pgdata
   version: '2'

services:
  postgres:
    image: postgres:latest
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data

  networks:
    internal_ip:
      ipv4_address: 192.168.1.2

 volumes:
   pgdata:
      external: true

确保"volumes"与"services"处于相同的缩进级别。

-1
container_name: postgresql
image: postgres:latest
environment:
   POSTGRES_PASSWORD: postgres
#    volumes:
#      - "./database:/var/lib/postgresql/data/"
ports:
  - "5432:5432"

注释卷 这个解决方法在Windows上奏效了,我必须把这两行代码带回Mac,但这是目前最简单的工作方式,直到Docker Windows或Posgres Docker推出真正的解决方案。


3
但这不是持久化存储卷。每次容器启动时,PostgreSQL都会重新开始。 - osmanraifgunes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接