Search code examples
dockerdocker-composedocker-volume

How do I give access to container to have full access for a bind volume


I have a problem deploying some docker images when I use bind volumes and when I check the logs I see errors like access denied when the docker application try to create a folder. For example the following docker compose create two containers, one for the postgres database and one for the postgres admin panel.

version: '3.7'

services:

  PostgresDB:
    image: postgres    
    environment:
      - POSTGRES_DB=MyDatabase
      - POSTGRES_USER=MyUser
      - POSTGRES_PASSWORD=MyPassword
    volumes: 
      - ./data:/var/lib/postgresql/data
    ports:
      - '5432:5432'

  PostgresDBAdmin:  
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: MyPasword
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
       - pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"

volumes:
    pgadmin:

For the database I use bind volume but for the panel I use normal volume. The application works fine. If I change the panel container to use bind volumes my docker compose file looks like this

version: '3.7'

services:

  PostgresDB:
    image: postgres    
    environment:
      - POSTGRES_DB=MyDatabase
      - POSTGRES_USER=MyUser
      - POSTGRES_PASSWORD=MyPassword
    volumes: 
      - ./data:/var/lib/postgresql/data
    ports:
      - '5432:5432'

  PostgresDBAdmin:  
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: MyPasword
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
       - ./pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"

This will have as a result the panel container to fail because of directory permission problem. The generated error log looks like this

PostgresDBAdmin_1  | ERROR  : Failed to create the directory /var/lib/pgadmin/sessions:
PostgresDBAdmin_1  |            [Errno 13] Permission denied: '/var/lib/pgadmin/sessions'
PostgresDBAdmin_1  | HINT   : Create the directory /var/lib/pgadmin/sessions, ensure it is writeable by
PostgresDBAdmin_1  |          'pgadmin', and try again, or, create a config_local.py file
PostgresDBAdmin_1  |          and override the SESSION_DB_PATH setting per
PostgresDBAdmin_1  |          https://www.pgadmin.org/docs/pgadmin4/6.18/config_py.html
PostgresDBAdmin_1  | Traceback (most recent call last):
PostgresDBAdmin_1  |   File "/pgadmin4/pgadmin/setup/data_directory.py", line 82, in create_app_data_directory
PostgresDBAdmin_1  |     _create_directory_if_not_exists(config.SESSION_DB_PATH)
PostgresDBAdmin_1  |   File "/pgadmin4/pgadmin/setup/data_directory.py", line 20, in _create_directory_if_not_exists
PostgresDBAdmin_1  |     os.mkdir(_path)
PostgresDBAdmin_1  | PermissionError: [Errno 13] Permission denied: '/var/lib/pgadmin/sessions'

This kind of problem is rare and I try to find a way to give access to the container to create the directory but I did not find a way to do it. The reason that I want to be able to use bind volumes is because in cases like NopCommerce it makes easier for me to have access to the files in order to create a theme.

Can someone help me to solve this problem?


Solution

  • You have to specify user:root inside the PostgreAdmin Service. And the result of the docker compose file look like this :

    PostgresDBAdmin:  
      image: dpage/pgadmin4
      user: root
      environment:
        PGADMIN_DEFAULT_EMAIL: [email protected]
        PGADMIN_DEFAULT_PASSWORD: MyPasword
        PGADMIN_CONFIG_SERVER_MODE: 'False'
      volumes:
        - ./pgadmin:/var/lib/pgadmin
      ports:
        - "5050:80"