简体   繁体   English

使用本地挂载卷创建一个 postgres docker-compose

[英]Create a postgres docker-compose with a local mount volume

I have the follow code of my docker-compose.yml:我有我的 docker-compose.yml 的以下代码:

version: '3'
services:
  db:
    image: postgres:14.6
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    volumes:
      - ./data:/var/lib/postgresql/data

But when I do docker compose up , the console give me the next error:但是当我执行docker compose up ,控制台给我下一个错误:

servicios-basesdatos-db-1 exited with code 1
servicios-basesdatos-db-1  | The files belonging to this database system will be owned by user "postgres".
servicios-basesdatos-db-1  | This user must also own the server process.
servicios-basesdatos-db-1  | 
servicios-basesdatos-db-1  | The database cluster will be initialized with locale "en_US.utf8".
servicios-basesdatos-db-1  | The default database encoding has accordingly been set to "UTF8".
servicios-basesdatos-db-1  | The default text search configuration will be set to "english".
servicios-basesdatos-db-1  | 
servicios-basesdatos-db-1  | Data page checksums are disabled.
servicios-basesdatos-db-1  | 
servicios-basesdatos-db-1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
servicios-basesdatos-db-1  | creating subdirectories ... ok
servicios-basesdatos-db-1  | selecting dynamic shared memory implementation ... posix
servicios-basesdatos-db-1  | selecting default max_connections ... 20
servicios-basesdatos-db-1  | selecting default shared_buffers ... 400kB
servicios-basesdatos-db-1  | selecting default time zone ... Etc/UTC
servicios-basesdatos-db-1  | creating configuration files ... ok
servicios-basesdatos-db-1  | 2023-01-31 09:07:54.976 UTC [83] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
servicios-basesdatos-db-1  | 2023-01-31 09:07:54.976 UTC [83] HINT:  The server must be started by the user that owns the data directory.
servicios-basesdatos-db-1  | child process exited with exit code 1
servicios-basesdatos-db-1  | initdb: removing contents of data directory "/var/lib/postgresql/data"

And I can't up my docker.而且我无法拨打我的 docker。

How can I fix it so that it works?我该如何修复它才能正常工作?

Think this line is crucial here认为这条线在这里很重要

servicios-basesdatos-db-1  | 2023-01-31 09:07:54.976 UTC [83] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership

You may try this solution (initialization of volume in volumes) like:您可以尝试此解决方案(以卷为单位初始化卷),例如:

version: '2'
services:
  db:
     image: postgres:14.6
     volumes:
        - postgres:/var/lib/postgresql/data
volumes:
  postgres:   

If you look at the docker file if the image here https://github.com/docker-library/postgres/blob/41bd7bf3f487e6dc0036fd73efaff6ccb6fbbacd/14/bullseye/Dockerfile you'll see that they create a user with id 999. I guess your local data directory is owned by you, which is in most cases user 1000. So if you chown the data directory to user 999, it should work.如果您查看 docker 文件,如果图像位于此处https://github.com/docker-library/postgres/blob/41bd7bf3f487e6dc0036fd73efaff6ccb6fbbacd/14/bullseye/Dockerfile ,您会看到他们创建了一个 ID 为 999 的用户。我猜你的本地数据目录归您所有,在大多数情况下是用户 1000。因此,如果您将数据目录更改为用户 999,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM