initdb: could not change permissions of directory on Postgresql container

Viewed 20725

I am new to the docker ecosystem and I am trying to spin up a simple postgres container along with a volume so it persists its data, by using a yaml composer file. The file is as follows:

# Use postgres/example user/password credentials
version: '3.3'
services:
    db:
        image: postgres
        environment:
            POSTGRES_DB: recrow
            POSTGRES_USER: recrow
            POSTGRES_PASSWORD: recrow_db_1000
            PGDATA: /var/lib/pgsql/data/pgdata
        volumes:
          - ./pgsql/data:/var/lib/pgsql/data/pgdata

However, upon calling docker-compose -f stack.yml up I get the following error:

fixing permissions on existing directory /var/lib/postgresql/data/pgdata ... initdb: could not change permissions of directory "/var/lib/postgresql/data/pgdata": Operation not permitted

/var/lib/pgsql/data/pgdata is supposed to be a directory relative to the container's root, while ./pgsql/data is a path on the host. I am running the container from an ntfs-3g partition mounted on /mnt/storage. What could be the problem? I am also running docker without root permissions, by adding my user to the docker group and this user also has full access to the beforementioned mount point /mnt/storage.

3 Answers

Following off of @liam-mitchell's note above, that is the answer. Use named volumes such like the following:

services:
  db:
    image: postgres:12-alpine
    volumes:
      - "postgres:/data/postgres"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - PGDATA=/data/postgres

...

volumes:
  postgres:

I work with OpenShift and had the same problem to run this official image from Docker Hub.

In my case, the solution was to use the official postgres image from red hat repository, the image from red hat repository has fixed this problem, this is can be an alternative.

Related