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

18,339

Solution 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:

    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

Docker Forums

Solution 2

Please refer reinierkors' answer from here. The answer is as follows copied as is from the link here for reader's convenience and works for me

I solved this by mapping my local volume one directory below the one Postgres needs:

version: '3'
services:
  postgres:
    image: postgres
    restart: on-failure
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
      - PGDATA=/var/lib/postgresql/data/pgdata
      - POSTGRES_DB=postgres
    volumes:
      - ./postgres_data:/var/lib/postgresql
    ports:
      - 5432:5432

Solution 3

I was having the same issue after downgrading my Docker from WSL 2 to WSL 1 and what Thomas Taylor's pertaining, I solved the issue by using named volume.

version: '3.8'

services:
  postgres:
    image: timescale/timescaledb:latest-pg12
    ...
    volumes:
      - pgdata:/var/lib/postgresql/data
    ...


volumes:
  pgdata:
Share:
18,339

Related videos on Youtube

sphinx
Author by

sphinx

I started programming 2 years ago ;) I am a really quick learner and I love to learn any language you offer me! Love to work with people. Love to work with you in the future and thanks for taking a visit to my profile!

Updated on June 15, 2022

Comments

  • sphinx
    sphinx almost 2 years

    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 starting 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.

  • sphinx
    sphinx about 6 years
    ./docker/pgdata folder in in windows, having all executable rights.
  • leeman24
    leeman24 about 3 years
    Using a named volume is the right solution. I prefer mounted volumes as it's easier for me but this will suffice. Thanks.