使用本地装载卷创建postgres docker-compose

r6vfmomb  于 2023-02-03  发布在  Docker
关注(0)|答案(2)|浏览(163)

我有我的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

但是当我执行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"

我也不能给我的 Docker 。
我怎样才能修好它,使它工作呢?

h7appiyu

h7appiyu1#

我觉得这条线很重要

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

您可以尝试此解决方案(初始化卷中的卷),如:

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

ycl3bljg2#

如果您查看docker文件(如https://github.com/docker-library/postgres/blob/41bd7bf3f487e6dc0036fd73efaff6ccb6fbbacd/14/bullseye/Dockerfile中的图像所示),您将看到他们创建了一个ID为999的用户。我猜您的本地数据目录归您所有,在大多数情况下,该目录为用户1000。因此,如果您将数据目录更改为用户999,它应该可以正常工作。

相关问题