23

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.

5 Answers 5

11

I'm guessing this is going to be an incompatibility with ntfs-3g. The PostgreSQL image contains an entrypoint script that is doing some permission changes on container start: https://github.com/docker-library/postgres/blob/972294a377463156c8d61297320c872fc7d370a9/9.6/docker-entrypoint.sh#L32-L38. I found another relevant question at https://askubuntu.com/questions/11840/how-do-i-use-chmod-on-an-ntfs-or-fat32-partition that talks about being able to set permissions at mount time. But not being able to change via chmod or chown (which is likely the reason for the failure in this case).

Unfortunately, I think the answer here is that you cannot use ntfs-3g safely for backing Docker host volume mounts.

Sign up to request clarification or add additional context in comments.

5 Comments

Any update on this in modern times with WSL or WSL 2?
I personally have not tried it. But WSL/WSL2 should natively support Linux permissions. If it isn't working and there is a problem that is probably a separate issue and warrants a separate question.
It works fine if I use a named volume but not if I use volumes: - ./pgsql/data:/var/lib/pgsql/data/pgdata
@LiamMitchell what do you mean a named volume ? i also getting the error using WSL2 ... trying to install postgres docker on windows so the volume value of docker-compose is C:\postgres\:/var/lib/postgres/data ... edit: don't know why only after i write a comment i find the answer, i've created the folders of the final location on windows (`C:\postgres`) restarted it and it worked ...
@RickyLevi Put it in WSL not in windows. wsl mkdir yourproject cd yourproject touch docker-compose.yml code ./ Write the files as needed In same dir in wsl terminal run your docker-compose up -d
11

I had the same issue with docker on WSL2. Setting the :Z flag for the mount and not mounting to a Windows file system directory (/mnt/*) but a linux directory (/home/*) worked for me.

my compose:

version: '3.3'
services:
    postgres:
        container_name: dbs2-postgres
        environment:
            - POSTGRES_PASSWORD=mysecretpassword
            - PGDATA=/var/lib/postgresql/data/pgdata
        volumes:
            - './data:/var/lib/postgresql/data:Z'
        image: postgres

1 Comment

Thanks! Just using the Linux directory without ":Z" worked for me 🚀
10

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:

Comments

8

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.

2 Comments

This comment should be a gem. In my case, I was using Jhipster to deploy to Openshift. After I changed the Postgres image to one from the Redhat Repository I also had to change the following environment variable names in the deployment config (that was generated by JHipster). POSTGRES_USER to POSTGRESQL_USER POSTGRES_PASSWORD to POSTGRESQL_PASSWOD POSTGRES_DATABASE to POSTGRESQL_DATABASE basically, I appended QL onto POSTGRES.
Here are some of the images: github.com/sclorg/postgresql-container
1

I recently had this same issue on my WSL2 (Win10/Ubuntu) and landed on this question.

For other future people who come here.

I was trying to map a volume from my PGDATA to a windows folder on the WSL2 mount point itself (/mnt/c/...).

The solution for my case was to edit /etc/wsl.conf and add the metadata option in the automount settings.

[automount]
options = "metadata"

After adding the option, restarting WSL, my container started normally.

Here you can find more information about the metadata option: https://learn.microsoft.com/en-us/windows/wsl/wsl-config#automount-options

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.