Takashi Miyabe
Takashi Miyabe

Reputation: 95

docker-compose up postgresql error, chown: changing ownership of ‘/var/lib/postgresql/data’: Operation not permitted

I'm working in a project and we use docker. The project was fine until last friday and today I started my computer (mac mini - macOS Sierra version 10.12.5 (16F73)) with an error. I'm trying to run docker-compose -f dev.yml -f docker-compose.yml up, but when I execute this it returns the following message :

db_1 | chmod: changing permissions of ‘/var/lib/postgresql/data’: Operation not permitted.

I had deleted all the docker containers and images docker rm $(docker ps -a -q) docker rmi $(docker images -q)but the error persists.

My docker-compose.yml is this:

 version: '2'

 services:

 rabbitmq:
     restart: always
     image: rabbitmq:3.6
     environment:
       RABBITMQ_DEFAULT_USER: my_user
       RABBITMQ_DEFAULT_PASS: my_password
     ports:
       - "5672:5672"
       - "15672:15672"
   django:
     build: ./django
     command: gunicorn contactto.wsgi:application -b 0.0.0.0:8000
     environment: 
       - "TZ=Brazil/East"
     restart: always
     volumes: 
       - ./django:/usr/src/app
       - ./django/static:/usr/src/app/contactto/static
       - ./logs:/logs/
       - /asterisk/:/etc/asterisk/
     ports:
       - "8000:8000"
     links:
       - rabbitmq:rabbitmq
   node:
     build: ./node_6_8
     environment:
       - "TZ=Brazil/East"
     volumes_from:
       - django
   nginx:
     build: ./nginx
     restart: always
     environment: 
       - "TZ=Brazil/East"
     ports:
       - "80:80"
       - "443:443"
     volumes: 
       - /www/static
     volumes_from:
       - django
     links:
       - django:django
   worker:
     build: ./django
     command: su -m worker -c "celery worker -A contactto.celeryconf -Q default -n default@%h"
     environment: 
       - "TZ=Brazil/East"
     restart: always
     volumes:
       - ./django:/usr/src/app
       - ./django/static:/usr/src/app/contactto/static
       - ./logs:/logs/
       - /asterisk/:/etc/asterisk/
     links:
       - rabbitmq:rabbitmq

 volumes: 
   dbdata:

My dev.yml is this:

version: '2'

services:
  db:
    image: postgres:9.5
    restart: always
    environment:
      POSTGRES_USER: my_user
      POSTGRES_PASSWORD: my_password
      POSTGRES_DB: my_db
    volumes:
      - /psqldata:/var/lib/postgresql/data
  django:
    environment:
      - "DJANGO_CONFIG_MODE=Development"
    depends_on:
      - db
  worker:
    environment:
      - "DJANGO_CONFIG_MODE=Development"
    links:
      - db:db

Any help would be really appreciated.

Upvotes: 5

Views: 35419

Answers (3)

Evandro Coan
Evandro Coan

Reputation: 9418

You can manage it by directly replacing and running postgres entrypoint.

  1. First run as root
  2. For each postgres version, the initialization script may change, please check the initialization steps for your version (for example, on https://hub.docker.com/layers/library/postgres/16.3-bullseye/images/sha256-aee00675422eba5d11fd529c870d1b2baa634a00f47aec53782494c198b9deb4?context=explore and https://github.com/docker-library/postgres/commits/master/)
  3. Run the setup scripts as root, and when starting postgres, use guso to use the user postgres
  postgres:
    image: postgres:16.3
    restart: on-failure
    stop_grace_period: 1s
    environment:
      - POSTGRES_PORT=5432
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
    command:
      - /bin/bash
      - -c
      - >
        set -x;
        mkdir -p /tmp/postgresql;
        chown postgres:postgres /tmp/postgresql;
        tail -F /tmp/postgresql/postgresql.log &
        bash /usr/local/bin/docker-enforce-initdb.sh;
        bash docker-entrypoint.sh;
        gosu postgres postgres \
          -c log_statement=all \
          # ...
          # -c log_temp_files=0 \
          # -c log_file_mode=0777 \
          # -c log_directory=/tmp/postgresql \
          # -c log_filename=postgresql.log \
          # -c logging_collector=on
    volumes:
      - ./containerdata/postgresql_data:/var/lib/postgresql/data/
      - /etc/localtime:/etc/localtime:ro
    ports:
      - 5432:5432

Upvotes: 0

Konst_
Konst_

Reputation: 122

Use a named volume instead of bind volume

Based on this answer, which helped me: https://superuser.com/a/1781335

Notice no slashes on the left hand side:

  • pg-data:/var/lib/postgresql/data

docker-compose.yml:

version: '3'
services:
  db:
    image: postgres
    volumes:
      - pg-data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
volumes:
  pg-data:

Upvotes: 1

Takashi Miyabe
Takashi Miyabe

Reputation: 95

I entered at / and stay analyzing the folders. Than I set the command ls -la there and I saw that the folder was with root user. First I deleted the folder sudo rm -rf psqldata and after I setted permission sudo chmod 777 / to create the folder with my user mkdir psqldata and now it is working. I don't know why before it was working and suddenly it stoped. I hope this answer can help you too.

old folder ls -la drwxr-xr-x 2 root wheel 68 Jun 19 14:58 psqldata

new folder ls -la drwxr-xr-x 2 gtmiyabe wheel 68 Jun 20 10:07 psqldata

Upvotes: -3

Related Questions