5

I have a problem with docker-compose postgreSQL, I am working on Windows WSL 2 + Docker Desktop. When I run docker-compose on linux server it works good, but when I try to start it locally I'm getting next error:

postgres      | chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted
ngnix         | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
postgres      | The files belonging to this database system will be owned by user "postgres".
postgres      | This user must also own the server process.
postgres      |
postgres      | The database cluster will be initialized with locale "en_US.utf8".
postgres      | The default database encoding has accordingly been set to "UTF8".
postgres      | The default text search configuration will be set to "english".
postgres      |
postgres      | Data page checksums are disabled.
postgres      |
ngnix         | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
ngnix         | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
postgres      | fixing permissions on existing directory /var/lib/postgresql/data ... initdb: error: could not change permissions of directory "/var/lib/postgresql/data": Operation not permitted
ngnix         | /docker-entrypoint.sh: Configuration complete; ready for start up
postgres exited with code 1

I tried to change permission on all project like this => chmod 777 -R project_folder

Dcoker-compose.yml:

postgresdb:
    container_name: postgres
    build:
      context: ./docker/postgres
      dockerfile: Dockerfile
    environment:
      - POSTGRES_PASSWORD=password123
    volumes:
    - ./docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
    - ./postgres-data:/var/lib/postgresql/data

    ports:
    - "5432:5432"

No ideas, how to resolve it. Help me please!

3
  • Is there any reason you are not using Docker Volumes for the data directory, instead of the relative path? Commented Jul 14, 2021 at 10:30
  • Don't have reasons, I am newbie in dockers systems :) Commented Jul 14, 2021 at 11:00
  • You are running in WSL (Which is Linux). Try to open the ./postgres_data directory with the command: chmod 777 ./postgres_data. And restart the compose file. Commented Jul 14, 2021 at 13:02

4 Answers 4

6

So my guess here is, that since you are mounting a directory relative to the docker-compose file as "data" directory in WSL, the permission error occurs. You can try to fix these permissions (chmod, chown etc the local directory).

You could also use "named" volumes - which should get rid of the permission problem as well.

# docker-compose.yml

services:
  # ...
  db:
    image: postgres:latest
    volumes:
      - "dbdata:/var/lib/postgresql/data"

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

Comments

3

This can be fixed (at least in my case) by editing /etc/wsl.conf, as Hanson said in the other answer. However, the only option you should need to set is to enable metadata:

[automount]
options = "metadata"

Close all instances/terminals for the WSL VM, wait at least 8 seconds for the processes to stop, and then restart.

The defaults for uid, gid, umask, and fmask should work fine. For more on the defaults and settings in /etc/wsl.conf: https://learn.microsoft.com/en-us/windows/wsl/wsl-config#configuration-settings-for-wslconf

For more on file permissions interation between Windows and Linux through WSL: https://learn.microsoft.com/en-us/windows/wsl/file-permissions

1 Comment

It works perfectly thanks
1

My solution is to start build from Windows PowerShell, then there is no issue with permissions.

1 Comment

beside your solution that worked, my solution was to change back to mac...
1

Update the mount options for the directory in the WSL 2 instance's /etc/wsl.conf file by adding the following lines:

[automount]
options = "metadata,uid=1000,gid=1000,umask=22,fmask=11"

then

wsl --shutdown

It's work for me

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.