title: Postgres mounting volume in docker. Permission denied
body: <p>I try to mapping folder on the host to postgres container in order to save my data even if container destroy. </p>
  
  <p>Firstly I created new directory pg-data</p>
  
  <p><a href="https://i.stack.imgur.com/6LpSO.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/6LpSO.jpg" alt="enter image description here"></a></p>
  
  <p>Secondly I describe container in docker-compose.yml</p>
  
  <pre><code>version: '3.7'
  
  networks:
    backend-network:
      driver: bridge
    frontend-network:
      driver: bridge
  
  volumes:
     redis-data:
     pg-data:
  
  services:
    &amp;app-service app: &amp;app-service-template
      container_name: k4fntr_app
      build:
        context: ./docker/php-fpm
        args:
          UID: ${UID?Use your user ID}
          GID: ${GID?Use your group ID}
          USER: ${USER?Use your user name}
      user: "${UID}:${GID}"
      hostname: *app-service
      volumes:
        - /etc/passwd/:/etc/passwd:ro
        - /etc/group/:/etc/group:ro
        - ./:/var/www/k4fntr
      environment:
        APP_ENV: "${APP_ENV}"
        CONTAINER_ROLE: app
        FPM_PORT: &amp;php-fpm-port 9000
        FPM_USER: "${UID:-1000}"
        FPM_GROUP: "${GID:-1000}"
      depends_on:
        - redis
        - database
      networks:
        - backend-network
  
    &amp;queue-service queue:
      &lt;&lt;: *app-service-template
      container_name: k4fntr_queue
      restart: always
      hostname: *queue-service
      depends_on:
        - app
      environment:
        CONTAINER_ROLE: queue
  
    &amp;schedule-service schedule:
      &lt;&lt;: *app-service-template
      container_name: k4fntr_schedule
      restart: always
      hostname: *schedule-service
      depends_on:
        - app
      environment:
        CONTAINER_ROLE: scheduler
  
    &amp;php-fpm-service php-fpm:
      &lt;&lt;: *app-service-template
      container_name: k4fntr_php-fpm
      user: 'root:root'
      restart: always
      hostname: *php-fpm-service
      ports: [*php-fpm-port]
      entrypoint: /fpm-entrypoint.sh
      command: php-fpm --nodaemonize -d "opcache.enable=0" -d "display_startup_errors=On" -d "display_errors=On" -d "error_reporting=E_ALL"
      networks:
        - backend-network
        - frontend-network
  
    mail:
      container_name: k4fntr_mail
      image: mailhog/mailhog
      ports:
      - "1025:1025"
      - "8025:8025"
        - backend-network
  
    database:
      container_name: k4fntr_database
      build: ./docker/postgres
      restart: always
      environment:
        ENV: ${APP_ENV}
        TESTING_DB: ${DB_DATABASE_TESTING}
        POSTGRES_DB: ${DB_DATABASE}
        POSTGRES_USER: ${DB_USERNAME}
        POSTGRES_PASSWORD: ${DB_PASSWORD}
      ports:
        - "15432:5432"
      volumes:
        - ./docker/postgres/prod/:/prod
        - ./docker/postgres/pg-data:/var/lib/postgresql/data:Z
      networks:
        - backend-network
  
      - backend-network
  
    database:
      container_name: k4fntr_database
      build: ./docker/postgres
      restart: always
      environment:
        ENV: ${APP_ENV}
        TESTING_DB: ${DB_DATABASE_TESTING}
        POSTGRES_DB: ${DB_DATABASE}
        POSTGRES_USER: ${DB_USERNAME}
        POSTGRES_PASSWORD: ${DB_PASSWORD}
      ports:
        - "15432:5432"
      volumes:
        - ./docker/postgres/prod/:/prod
        - ./docker/postgres/pg-data:/var/lib/postgresql/data:rw
      networks:
        - backend-network
  
      - backend-network
  
    nginx:
      container_name: k4fntr_nginx
      image: nginx
      volumes:
      - ./docker/nginx/config:/etc/nginx/conf.d
      - ./:/var/www/k4fntr
      depends_on:
        - *php-fpm-service
      ports:
        - "8084:80"
      networks:
        - frontend-network
  
    redis:
      container_name: k4fntr_redis
      image: redis
      restart: always
      command: redis-server
      volumes:
        - ./docker/redis/config/redis.conf:/usr/local/etc/redis/redis.conf
        - ./docker/redis/redis-data:/data:rw
      ports:
       - "16379:6379"
      networks:
        - backend-network
  
    database:
      container_name: k4fntr_database
      build: ./docker/postgres
      restart: always
      environment:
        ENV: ${APP_ENV}
        TESTING_DB: ${DB_DATABASE_TESTING}
        POSTGRES_DB: ${DB_DATABASE}
        POSTGRES_USER: ${DB_USERNAME}
        POSTGRES_PASSWORD: ${DB_PASSWORD}
      ports:
        - "15432:5432"
      volumes:
        - ./docker/postgres/prod/:/prod
        - ./docker/postgres/pg-data:/var/lib/postgresql/data:Z
      networks:
        - backend-network
  </code></pre>
  
  <p>I also have Dockerfile in docker/postgres/ which is contains the next code</p>
  
  <pre><code>    FROM postgres:10.5-alpine
  
      COPY /docker-entrypoint-initdb.d/ /docker-entrypoint-initdb.d/
  
      RUN apk add openssh-client
      RUN chmod a+r /docker-entrypoint-initdb.d/*
  
      RUN mkdir /prod
  
      RUN chmod a+r /prod
  </code></pre>
  
  <p>Then I build my image with docker-compose exec -d --build</p>
  
  <p>After it my user changes on 70:root</p>
  
  <p><a href="https://i.stack.imgur.com/PN6lJ.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/PN6lJ.jpg" alt="enter image description here"></a></p>
  
  <p>and I can't doing any with this folder without sudo. Also when I try to rebuild the container I got an error</p>
  
  <pre><code>/usr/local/bin/docker-compose up -d --build
  Building database
  Traceback (most recent call last):
    File "bin/docker-compose", line 6, in &lt;module&gt;
    File "compose/cli/main.py", line 72, in main
    File "compose/cli/main.py", line 128, in perform_command
    File "compose/cli/main.py", line 1077, in up
    File "compose/cli/main.py", line 1073, in up
    File "compose/project.py", line 548, in up
    File "compose/service.py", line 351, in ensure_image_exists
    File "compose/service.py", line 1106, in build
    File "site-packages/docker/api/build.py", line 160, in build
    File "site-packages/docker/utils/build.py", line 30, in tar
    File "site-packages/docker/utils/build.py", line 49, in exclude_paths
    File "site-packages/docker/utils/build.py", line 214, in rec_walk
    File "site-packages/docker/utils/build.py", line 184, in rec_walk
  PermissionError: [Errno 13] Permission denied: '/home/ubuntu/PhpstormProjects/fntr/docker/postgres/pg-data'
  [16332] Failed to execute script docker-compose
  </code></pre>
  
  <p>And I need to remove this folder (with sudo) and then do rebuild.</p>
  
  <p>What I tried:</p>
  
  <p>1 - I have read the instruction from <a href="https://hub.docker.com/_/postgres" rel="noreferrer">https://hub.docker.com/_/postgres</a> (Arbitrary --user Notes) and tried to volume /etc/passwd inside the container and user: "${UID}:${GID}", but got an error</p>
  
  <pre><code>initdb: could not access directory "/var/lib/postgresql/data": permission denied
  </code></pre>
  
  <p>2 - use PGDATA into another place and got the same error with rebuild</p>
  
  <p>3 - create /var/lib/postgresql/data in Dockerfile with 777 permissions, but postgres entrypoints from image just removed this folder and recreated it</p>
  
  <p>I really need help with this issue because I spent 3 days without effect</p>