Home > OS >  Permissions error with docker compose on macOS
Permissions error with docker compose on macOS

Time:12-25

I'm trying to spin up a postgresl database using docker compose on my mac running macOS 12.4.

My docker-compose.yml file is

version: '3.8'
services:
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: gymbro
    ports:
      - 5438:5432
    volumes:
      - ./pgdata:/var/lib/postgresql/data

Running docker-compose up results in the container exiting with status 1. Here is the relevant output

Attaching to docker-compose-gymbro-db-1
docker-compose-gymbro-db-1  | The files belonging to this database system will be owned by user "postgres".
docker-compose-gymbro-db-1  | This user must also own the server process.
docker-compose-gymbro-db-1  | 
docker-compose-gymbro-db-1  | The database cluster will be initialized with locale "en_US.utf8".
docker-compose-gymbro-db-1  | The default database encoding has accordingly been set to "UTF8".
docker-compose-gymbro-db-1  | The default text search configuration will be set to "english".
docker-compose-gymbro-db-1  | 
docker-compose-gymbro-db-1  | Data page checksums are disabled.
docker-compose-gymbro-db-1  | 
docker-compose-gymbro-db-1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
docker-compose-gymbro-db-1  | creating subdirectories ... ok
docker-compose-gymbro-db-1  | selecting dynamic shared memory implementation ... posix
docker-compose-gymbro-db-1  | selecting default max_connections ... 100
docker-compose-gymbro-db-1  | selecting default shared_buffers ... 128MB
docker-compose-gymbro-db-1  | selecting default time zone ... Etc/UTC
docker-compose-gymbro-db-1  | creating configuration files ... ok
docker-compose-gymbro-db-1  | running bootstrap script ... ok
docker-compose-gymbro-db-1  | performing post-bootstrap initialization ... 2022-12-24 17:42:20.791 UTC [40] FATAL:  data directory "/var/lib/postgresql/data" has invalid permissions
docker-compose-gymbro-db-1  | 2022-12-24 17:42:20.791 UTC [40] DETAIL:  Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).
docker-compose-gymbro-db-1  | child process exited with exit code 1
docker-compose-gymbro-db-1  | initdb: removing contents of data directory "/var/lib/postgresql/data"
docker-compose-gymbro-db-1 exited with code 1

Seems like there is a permissions issue,

docker-compose-gymbro-db-1  | performing post-bootstrap initialization ... 2022-12-24 17:42:20.791 UTC [40] FATAL:  data directory "/var/lib/postgresql/data" has invalid permissions
docker-compose-gymbro-db-1  | 2022-12-24 17:42:20.791 UTC [40] DETAIL:  Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).

I can spin up the db using a docker volume, but I'm just curious as to why this is happening and if there is a possible solution.

CodePudding user response:

On Linux here, so this might not work on Mac. However, why not avoid using the local volum?

Like:

version: '3'

services:

  db:
    image: postgres:13.2
    env_file:
      - .env
    volumes:
      - data:/var/lib/postgresql/data

volumes:
  data: {}

This is a solution I always use and solved my issues once and for all when I used to have it.

However, there is a more interesting discussion on this here that you can go through: https://forums.docker.com/t/data-directory-var-lib-postgresql-data-pgdata-has-wrong-ownership/17963/22

I have seen some items I honestly won't recommend like changing the PG_DATA, but if you're comfortable with it, have a go.

Let me know what you think :)

  • Related