Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Permission denied to local MySQL volume

I'm new with Docker and I don't know Linux well. I'm trying to build my own environment for local development with Docker. I’m using docker-compose utility. I want to store MySQL data in the local volume. When I run docker-compose build and docker-compose up -d commands for the first time, there are no errors. Data from MySQL container goes into the local folder. Everything works well except one: when I want to change my docker-compose.yml file and rebuild containers I get an error

vo@vo-ThinkPad-Edge-E330:~/www/test$ docker-compose build
mysql uses an image, skipping
nginx uses an image, skipping
Building app
Traceback (most recent call last):
  File "bin/docker-compose", line 3, in <module>
  File "compose/cli/main.py", line 67, in main
  File "compose/cli/main.py", line 126, in perform_command
  File "compose/cli/main.py", line 302, in build
  File "compose/project.py", line 468, in build
  File "compose/project.py", line 450, in build_service
  File "compose/service.py", line 1125, in build
  File "docker/api/build.py", line 160, in build
  File "docker/utils/build.py", line 30, in tar
  File "docker/utils/build.py", line 49, in exclude_paths
  File "docker/utils/build.py", line 214, in rec_walk
  File "docker/utils/build.py", line 214, in rec_walk
  File "docker/utils/build.py", line 214, in rec_walk
  [Previous line repeated 1 more time]
  File "docker/utils/build.py", line 184, in rec_walk
PermissionError: [Errno 13] Permission denied: '/home/vo/www/test/docker/mysql/dbdata/performance_schema'
[301838] Failed to execute script docker-compose

I found out that the owner of the folder is systemd-coredump from root group. So I have 2 ways:

  1. sudo docker-compose build
  2. Delete /home/vo/www/test/docker/mysql/dbdata folder with sudo permissions and run docker-compose build again.

So, my question: Is this how it should be or is it possible to solve the permissions problem?

My project structure:

/
├── docker
│   ├── mysql
│   │   ├── conf
│   │   │   └── my.cnf
│   │   └── dbdata
│   ├── nginx
│   │   └── conf
│   │       └── nginx.conf
│   └── php
│       ├── conf
│       │   └── local.ini
│       ├── config
│       │   └── local.ini
│       └── Dockerfile
├── docker-compose.yml
└── src

My docker-compose.yml:

version: "3.7"
services:
  
  #PHP Service
  app:
    build:
      args:
        user: laravel
        uid: 1000
      context: ./
      dockerfile: ./docker/php/Dockerfile
    image: laravel-image
    container_name: laravel
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev  
    working_dir: /var/www/
    volumes:
      - ./src:/var/www
      - ./docker/php/config/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - laravel
  
  #MySQL Service  
  mysql:
    image: mysql:5.7
    container_name: mysql
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_PASSWORD: secret
      MYSQL_USER: laravel
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ./docker/mysql/dbdata:/var/lib/mysql
      - ./docker/mysql/conf/my.cnf:/etc/mysql/my.cnf  
    networks:
      - laravel
  
  #Nginx Service
  nginx:
    image: nginx:1.17-alpine
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    environment:
      SERVICE_NAME: nginx
      SERVICE_TAGS: dev        
    volumes:
      - ./src:/var/www
      - ./docker/nginx/conf:/etc/nginx/conf.d
    networks:
      - laravel    

#Networks
networks:
  laravel:
    driver: bridge
like image 636
Vlodko Avatar asked Nov 15 '22 01:11

Vlodko


1 Answers

Ok, I found a trick. In my docker-compose.yml in service volume section I have to use named volumes instead of path. For example, 'mysqldbvolume' instead of './docker/mysql/dbdata'. Then I have to define a named volume in the top-level volumes key:

services: 
  #MySQL Service  
  mysql:
    image: mysql:5.7
    ...
    volumes:
      - mysqldbvolume:/var/lib/mysql
      - ./docker/mysql/conf/my.cnf:/etc/mysql/my.cnf
    ...

...

# Volumes
volumes:
  mysqldbvolume:
    driver: local

So, where is my volume now? If I want to see list of my volumes, I have to run docker volume ls:

DRIVER    VOLUME NAME
local     test_mysqldbvolume
local     test_postgresdbvolume

Inspect volume - docker volume inspect test_mysqldbvolume:

[
    {
        "CreatedAt": "2020-12-17T21:54:53+02:00",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "test",
            "com.docker.compose.version": "1.27.4",
            "com.docker.compose.volume": "mysqldbvolume"
        },
        "Mountpoint": "/var/lib/docker/volumes/test_mysqldbvolume/_data",
        "Name": "test_mysqldbvolume",
        "Options": null,
        "Scope": "local"
    }
]

So, path is "Mountpoint": "/var/lib/docker/volumes/test_mysqldbvolume/_data"

Run with regular user ls -la /var/lib/docker/volumes/test_mysqldbvolume/_data says access is denied. But if I run sudo ls -la /var/lib/docker/volumes/test_mysqldbvolume/_data I see my volume data:

drwxrwxrwt 6 systemd-coredump systemd-coredump     4096 дек 17 21:54 .
drwxr-xr-x 3 root             root                 4096 дек 17 21:42 ..
-rw-r----- 1 systemd-coredump systemd-coredump       56 дек 17 21:42 auto.cnf
-rw------- 1 systemd-coredump systemd-coredump     1676 дек 17 21:42 ca-key.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump     1112 дек 17 21:42 ca.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump     1112 дек 17 21:42 client-cert.pem
-rw------- 1 systemd-coredump systemd-coredump     1680 дек 17 21:42 client-key.pem
-rw-r----- 1 systemd-coredump systemd-coredump        2 дек 17 21:54 ed50eca9e01e.pid
-rw-r----- 1 systemd-coredump systemd-coredump  6093953 дек 17 21:54 general.log
-rw-r----- 1 systemd-coredump systemd-coredump      445 дек 17 21:49 ib_buffer_pool
-rw-r----- 1 systemd-coredump systemd-coredump 79691776 дек 17 21:54 ibdata1
-rw-r----- 1 systemd-coredump systemd-coredump 50331648 дек 17 21:54 ib_logfile0
-rw-r----- 1 systemd-coredump systemd-coredump 50331648 дек 17 21:42 ib_logfile1
-rw-r----- 1 systemd-coredump systemd-coredump 12582912 дек 17 21:54 ibtmp1
drwxr-x--- 2 systemd-coredump systemd-coredump     4096 дек 17 21:47 laravel
drwxr-x--- 2 systemd-coredump systemd-coredump     4096 дек 17 21:42 mysql
drwxr-x--- 2 systemd-coredump systemd-coredump     4096 дек 17 21:42 performance_schema
-rw------- 1 systemd-coredump systemd-coredump     1680 дек 17 21:42 private_key.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump      452 дек 17 21:42 public_key.pem
-rw-r--r-- 1 systemd-coredump systemd-coredump     1112 дек 17 21:42 server-cert.pem
-rw------- 1 systemd-coredump systemd-coredump     1680 дек 17 21:42 server-key.pem
drwxr-x--- 2 systemd-coredump systemd-coredump    12288 дек 17 21:42 sys

Most importantly, the permission error is gone.

like image 104
Vlodko Avatar answered Dec 15 '22 16:12

Vlodko