linux Docker:对本地MySQL卷的权限被拒绝

k2fxgqgv  于 2022-12-11  发布在  Linux
关注(0)|答案(2)|浏览(190)

我是Docker的新手,对Linux不太了解。我正在尝试使用Docker构建自己的本地开发环境。我正在使用docker-compose实用程序。我希望将MySQL数据存储在本地卷中。当我第一次运行docker-compose builddocker-compose up -d命令时,没有错误。2 MySQL容器中的数据进入本地文件夹。3一切正常,除了一个错误:当我想更改我的 docker-compose.yml 文件并重建容器时,我收到一个错误

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

我发现文件夹的所有者是root组中的systemd-coredump。因此,我有两种方法:

  1. sudo docker-compose build
    1.使用sudo权限删除 /home/vo/www/test/docker/mysql/dbdata 文件夹,然后再次运行docker-compose build
    所以,我的问题是:这是应该的吗?或者有可能解决权限问题吗?
    我的项目结构:
/
├── docker
│   ├── mysql
│   │   ├── conf
│   │   │   └── my.cnf
│   │   └── dbdata
│   ├── nginx
│   │   └── conf
│   │       └── nginx.conf
│   └── php
│       ├── conf
│       │   └── local.ini
│       ├── config
│       │   └── local.ini
│       └── Dockerfile
├── docker-compose.yml
└── src

我的 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
vvppvyoh

vvppvyoh1#

好的,我发现了一个窍门。在我的 docker-compose.yml in servicevolume部分,我必须使用命名卷而不是路径。例如,'mysqldbvolume' 而不是'*./docker/mysql/dbdata *'。然后我必须在顶级volumes键中定义一个命名卷:

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

那么,我的卷现在在哪里?如果我想查看我的卷列表,我必须运行docker volume ls

DRIVER    VOLUME NAME
local     test_mysqldbvolume
local     test_postgresdbvolume

检查体积-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"
    }
]

因此,路径为 “装载点”:“/var/lib/文件夹/卷/测试目录卷/数据”
以一般使用者ls -la /var/lib/docker/volumes/test_mysqldbvolume/_data执行会显示存取被拒绝。但如果我执行sudo ls -la /var/lib/docker/volumes/test_mysqldbvolume/_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

最重要的是,权限错误消失了。

gtlvzcf8

gtlvzcf82#

我也有这个问题,但原因与这里的大多数答案不同。
我有一个双 Boot 设置,Linux和Windows都可以访问第2个磁盘。
我有我的docker图像和代码在第2盘,一个NTFS-3G驱动器。我尝试了所有的变通办法与chmod和chown,但它只是不会工作。
当NTFS-3G引起这个问题时,我将Docker移回默认配置,系统磁盘上的所有内容都包括我的项目代码。
图像和容器再次位于默认位置/var/lib/docker,我将代码移到了~/code/project
一旦我这样做了,所有的权限问题就消失了。

相关问题