Error starting postgres container - mkdir: Permission denied

Viewed 3094

I am starting a postgres container using following docker-compose.yml

version: '3'
services:
  db:
    image: postgres:latest
    container_name: postgres
    environment:
      POSTGRES_USER: usr
      POSTGRES_PASSWORD: pswd
      POSTGRES_DB: db
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
        - 5432:5432
    volumes:
        - nfs_cur_dir:/var/lib/postgresql/data

volumes:
  nfs_cur_dir:
    driver: local
    driver_opts:
      type: nfs
      o: "addr=10.15.187.88,rw"
      device: ":/u/uname/home/database"

I am getting following error when starting the container

$sudo ./docker-compose  up db
Starting postgres ... done
Attaching to postgres
postgres    | mkdir: cannot create directory ‘/var/lib/postgresql/data’: Permission denied
postgres exited with code 1

The permissions on database directory are 777

drwxrwxrwx 3 uname grpname 4096 May 5 22:57 database

After the failure I also see pgdata directory created as this -

drwx------ 2 polkitd root 4096 May 5 22:57 pgdata

Note:

  • The data directory for the postgres is mapped to an NFS location. Hence I have defined a new NFS volume in the docker-compose and mapped that to the postgres container.
  • I am using PGDATA env variable to define a different location for the data directory.
  • Other than above two things there is nothing out of ordinary. If I use a local drive location for the data directory this works fine !
1 Answers

You should check the permissions that NFS share exposes. According to what you said, if you use a local drive it works fine. That's why I think the NFS share's permissions aren't working as you expect. Maybe you should create the directory before trying to run your application.

Related