Database Permission denied after running docker-compose up

I'm getting the following error after I run docker-compose up. How do I resolve this?

db_1          | chown: changing ownership of '/var/lib/mysql/': Permission denied
db_1          | chown: changing ownership of '/var/lib/mysql/@0020liquordb': Permission denied
db_1          | chown: changing ownership of '/var/lib/mysql/@0020liquordb/db.opt': Permission denied
db_1          | chown: changing ownership of '/var/lib/mysql/auto.cnf': Permission denied
db_1          | chown: changing ownership of '/var/lib/mysql/ca-key.pem': Permission denied
db_1          | chown: changing ownership of '/var/lib/mysql/ca.pem': Permission denied
db_1          | chown: changing ownership of '/var/lib/mysql/client-cert.pem': Permission denied
db_1          | chown: changing ownership of '/var/lib/mysql/client-key.pem': Permission denied

Dockerfile

FROM python:3.7


ENV PYTHONUNBUFFERED 1

ADD requirements.txt /app/

WORKDIR /app/

RUN pip install --upgrade pip


RUN pip install -r requirements.txt

docker-compose.yml

version: '3'

services:

  # Redis - result backend
  redis:
    image: redis:2.8.19
    hostname: redis

  # RabbitMQ - queue
  rabbit:
    hostname: rabbit
    image: rabbitmq:3-management
    environment:
      - RABBITMQ_DEFAULT_USER=test
      - RABBITMQ_DEFAULT_PASS=test
    ports:
      - "5672:5672"  
      - "15672:15672"  # here, we can access rabbitmq management plugin


  nginx:
    image: nginx:alpine
    container_name: nginx

    restart: always

    ports:
      - "7000:7000"
      - "443:443"

    depends_on:
      - web

    volumes:
      - /app:/app
      - ./config/nginx:/etc/nginx/conf.d
      - /static:/app/static # <-- bind the static volume
      - media_volume:/media  # <-- bind the media volume

  db:
    restart: always
    image: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      # - ./init-db:/docker-entrypoint-initdb.d
      - ./data-db:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: test
      MYSQL_DATABASE: test
    ports:
      - "3306:3306"


  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    environment:
      MYSQL_USERNAME: test
      MYSQL_ROOT_PASSWORD: test
    restart: always
    ports:
      - 8082:80
    volumes:
      - /sessions


  web:
    build:
      context: .
      dockerfile: Dockerfile
    hostname: web
    restart: always
    command: sh ./run_web.sh
    volumes:
      - .:/app
      - /static:/app/static

    depends_on:
      - db

    links:
      - rabbit
      - redis

    expose: 
    - "7000"


  # Celery worker
  worker:
    build:
      context: .
      dockerfile: Dockerfile
    command: sh ./run_celery.sh

    volumes:
      - .:/app
    links:
      - rabbit
      - redis

    depends_on:
      - rabbit

volumes:
  media_volume:  # <-- declare the media volume
  api_data:
  my-db:
  init-db:
  data-db:

Solution 1:

You can try to edit the db service in docker-compose.yml with the following changes:

volumes:
  # - ./init-db:/docker-entrypoint-initdb.d
  - ./data-db:/var/lib/mysql:rw # <- permissions
user: mysql # <- user