Docker container shuts down giving 'data directory has wrong ownership' error when executed in windows 10

两盒软妹~` 提交于 2020-05-14 16:33:26

问题


I have my docker installed in Windows. I am trying to install this application. It has given me the following docker-compose.yml file:

version: '2'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile-nginx
    ports:
    - "8085:80"
    networks:
      - attendizenet
    volumes:
      - .:/usr/share/nginx/html/attendize
    depends_on:
      - php
  php:
    build:
      context: .
      dockerfile: Dockerfile-php
    depends_on:
      - db
      - maildev
      - redis
    volumes:
      - .:/usr/share/nginx/html/attendize
    networks: 
      - attendizenet
  php-worker:
    build:
      context: .
      dockerfile: Dockerfile-php
    depends_on:
      - db
      - maildev
      - redis
    volumes:
      - .:/usr/share/nginx/html/attendize
    command: php artisan queue:work --daemon
    networks:
      - attendizenet
  db:
    image: postgres
    environment:
      - POSTGRES_USER=attendize
      - POSTGRES_PASSWORD=attendize
      - POSTGRES_DB=attendize
    ports:
      - "5433:5432"
    volumes:
      - ./docker/pgdata:/var/lib/postgresql/data
    networks:
    - attendizenet
  maildev:
    image: djfarrelly/maildev
    ports:
      - "1080:80"
    networks:
      - attendizenet
  redis:
    image: redis
    networks:
      - attendizenet

networks:
  attendizenet:
    driver: bridge

All the installation goes well, but the PostgreSQL container stops after stating for a moment giving following error.

2018-03-07 08:24:47.927 UTC [1] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
2018-03-07 08:24:47.927 UTC [1] HINT:  The server must be started by the user that owns the data directory

A simple PostgreSQL container from Docker Hub works smoothly, but the error occurs when we try to attach a volume to the container.

I am new to docker, so please ignore usage of terms wrongly.


回答1:


This is a documented problem with the Postgres Docker image on Windows [1][2][3][4]. Currently, there doesn't appear to be a way to correctly mount Windows directories as volumes. You could instead use a persistent Docker volume, for example:

  db:
    image: postgres
    environment:
      - POSTGRES_USER=attendize
      - POSTGRES_PASSWORD=attendize
      - POSTGRES_DB=attendize
    ports:
      - "5433:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
    - attendizenet

volumes:
  pgdata:

Other things that didn't work:

  • Set PGDATA to a subdirectory (See PGDATA Setting)
    environment:
      - PGDATA=/var/lib/postgresql/data/mnt
    volumes:
      - ./pgdata:/var/lib/postgresql/data
  • Use a Bind Mount (docker-compose 3.2)
    volumes:
      - type: bind
        source: ./pgdata
        target: /var/lib/postgresql/data
  • Running as POSTGRES_USER=root

More Information:

GitHub

  • data directory "/var/lib/postgresql/data" has wrong ownership

Docker Forums

  • postgresql-data-pgdata-has-wrong-ownership
  • postgres-to-work-on-persistent-windows-mount



回答2:


Even i had the problem i had to copy the data dir at regular intervals.

 docker cp <container-name>:/var/lib/postgresql/data C:/docker/volumes/postgres



回答3:


Owner for the data folder in postgres inside the container is Postgres user. Your current user may not have access privilege in the mounted folder. You need to give all permissions according to the requirements by given command below :

chmod 777 ./docker/pgdata

If this command is not helping to resolve this issue please refer the following link to do the user mapping from inside the container to outside the container.

https://docs.docker.com/engine/security/userns-remap/#prerequisites



来源:https://stackoverflow.com/questions/49148754/docker-container-shuts-down-giving-data-directory-has-wrong-ownership-error-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!