[Fixed]-Docker compose could not open directory permisson denied

9đź‘Ť

âś…

You’re trying to mount ./data/db in /var/lib/postgresql/data and you’re executing docker-compose with a non-privileged user.

So, we can have two possibilities:

  1. Problem with ./data/db permissions.
  2. Problem with /var/lib/postgresql/data

The simpiest solution is execute docker-compose with a privileged user (root), but if you don’t want to do that, you can try this:

  • Give permissions to ./data/db (I see your EDIT that you’ve already done it).
  • Give permissions to /var/lib/postgresql/data

How can you give /var/lib/postgresql/data permissions? Read the following lines:

First, note that /var/lib/postgresql/data is auto-generated by postgre
docker, so, you need to define a new Dockerfile which modifies these
permissions. After that, you need also modify docker-compose to use
this new Dockerfile.

./docker-compose.yml

version: '3'
services:
  db:
    build: 
      context: ./mypostgres
      dockerfile: Dockerfile_mypostgres
    container_name: dummy_project_postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data

  event_planner:
    build: ./dumy_project
    container_name: dummy_project
    volumes:
      - .:/web
    ports:
      - "8000:8000"
    depends_on:
      - db
    links:
      - db:postgres

./dumy_project/Dockerfile –> Without changes

./mypostgres/Dockerfile_mypostgres

FROM postgres
RUN mkdir -p /var/lib/postgresql/data
RUN chmod -R 777 /var/lib/postresql/data
ENTRYPOINT docker-entrypoint.sh

17đź‘Ť

I solved by adding “:z” to end of volume defintion

version: '3'
services:
  db:
    image: postgres
    container_name: dummy_project_postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data:z

  event_planner:
    build: ./dummy_project
    container_name: dummy_project
    volumes:
      - .:/web
    ports:
      - "8000:8000"
    depends_on:
      - db
    links:
      - db:postgres

What “:z” means

Labeling systems like SELinux require that proper labels are placed on
volume content mounted into a container. Without a label, the security
system might prevent the processes running inside the container from
using the content. By default, Docker does not change the labels set
by the OS.

To change the label in the container context, you can add either of
two suffixes :z or :Z to the volume mount. These suffixes tell Docker
to relabel file objects on the shared volumes. The z option tells
Docker that two containers share the volume content. As a result,
Docker labels the content with a shared content label. Shared volume
labels allow all containers to read/write content. The Z option tells
Docker to label the content with a private unshared label. Only the
current container can use a private volume.

https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container—volumes-from

what is 'z' flag in docker container's volumes-from option?

👤Farid Escate

1đź‘Ť

This solution is for case that your user is not present in docker group.

  1. First check if your user is in docker group:
grep 'docker' /etc/group
  1. Add user to docker group:
  • If the command return is empty, then create docker group:
sudo groupadd docker
  • Else if your user is not present in command return then add him to the group:
sudo usermod -aG docker $USER
  1. Reboot your system

  2. Test it again:

docker run hello-world

Tip: Remember to have the docker service started

If it works, try your docker-compose command again.

👤btd1337

Leave a comment