docker - 无法在 Docker 中挂载文件

标签 docker permissions docker-compose mount

错误描述

我无法在 Vagrant 或 Docker 中挂载文件,所以这似乎是由某种权限错误引起的问题。
我的操作系统是 Ubuntu 18.04 LTS (Bionic Beaver) ,我没有运行任何访问控制模块,如 SELinux据我所知。

在另一个问题中找到了与 Vagrant 相关的错误讨论:
Unable to mount files in Vagrant

Docker 故障排除

我无法使用 docker-compose 将文件挂载到 Docker 容器中,我正在尝试构建:https://github.com/fredrikaverpil/saltstack-docker

我将音量设置在 docker-compose文件到:

volumes:
  - ${PWD}/assets/master/etc/supervisor:/etc/supervisor
  - ${PWD}/assets/master/etc/salt:/etc/salt
  - ${PWD}/assets/master/var/cache/salt:/var/cache/salt
  - ${PWD}/assets/master/var/log/salt:/var/log/salt
  - ${PWD}/assets/master/srv:/srv

然而我在运行 docker-compose up 时得到了这个输出:
Creating network "saltstack_default" with the default driver
Creating salt_image
Creating salt
Creating minion
Attaching to salt, minion, salt_image
salt      | Error: could not find config file /etc/supervisor/supervisord.conf
salt      | For help, use /usr/bin/supervisord -h
salt exited with code 2
salt_image exited with code 0
minion    | [ERROR   ] DNS lookup of 'salt' failed.
minion    | [ERROR   ] Master hostname: 'salt' not found. Retrying in 30 seconds
docker inspect容器显示正确的配置:
   "Mounts": [
        {
            "Type": "bind",
            "Source": "/somedir/saltstack/assets/master/etc/salt",
            "Destination": "/etc/salt",
            "Mode": "rw",
            "RW": true,
            "Propagation": "rprivate"
        },
        {
            "Type": "bind",
            "Source": "/somedir/saltstack/assets/master/var/cache/salt",
            "Destination": "/var/cache/salt",
            "Mode": "rw",
            "RW": true,
            "Propagation": "rprivate"
        },
        {
            "Type": "bind",
            "Source": "/somedir/saltstack/assets/master/etc/supervisor",
            "Destination": "/etc/supervisor",
            "Mode": "rw",
            "RW": true,
            "Propagation": "rprivate"
        },
        {
            "Type": "bind",
            "Source": "/somedir/saltstack/assets/master/var/log/salt",
            "Destination": "/var/log/salt",
            "Mode": "rw",
            "RW": true,
            "Propagation": "rprivate"
        },
        {
            "Type": "bind",
            "Source": "/somedir/saltstack/assets/master/srv",
            "Destination": "/srv",
            "Mode": "rw",
            "RW": true,
            "Propagation": "rprivate"
        }

但是进入容器(使用另一个入口点命令),表明应该挂载的文件丢失了:
[root@salt /]# ll /etc/supervisor
total 0
[root@salt /]# ll /etc/salt
total 0
[root@salt /]# ll /var/cache/salt
total 0
[root@salt /]# ll /var/log/salt
total 0
[root@salt /]# ll /srv
total 0

结论

在复制文件时,我的本地计算机上似乎有些困惑,可能与我的用户配置方式以及将文件挂载到 VM 和容器中的访问权限有关。
如果有人能对此有所了解,我将不胜感激。

更新

更新 1

我怀疑这是 /var/lib/docker 中旧内容的问题我即将删除,但在此之前我遇到了另一个错误,可能是某种权限错误:
> cat docker-compose.yml 
# based on https://docs.docker.com/samples/library/nginx/#using-environment-variables-in-nginx-configuration
version: '2'

services:
  web:
    image: nginx
    volumes:
     - ./testfile.txt:/etc/nginx/
    ports:
     - "8080:80"
    environment:
     - NGINX_HOST=foobar.com
     - NGINX_PORT=80
    command: /bin/bash -c "nginx -g 'daemon off;'"

> docker-compose up
Creating network "docker_compose-mounting_issue_default" with the default driver
Creating docker_compose-mounting_issue_web_1 ... error

ERROR: for docker_compose-mounting_issue_web_1  Cannot start service web: b'OCI runtime create failed: container_linux.go:348: starting container process caused "process_linux.go:402: container init caused \\"rootfs_linux.go:58: mounting \\\\\\"/home/.../troubleshoot/docker_compose-mounting_issue/testfile.txt\\\\\\" to rootfs \\\\\\"/var/lib/docker/aufs/mnt/580b79241399e838c67b74021ecf2597aade5f1711811f4ab5c9c7bbcd188449\\\\\\" at \\\\\\"/var/lib/docker/aufs/mnt/580b79241399e838c67b74021ecf2597aade5f1711811f4ab5c9c7bbcd188449/etc/nginx\\\\\\" caused \\\\\\"not a directory\\\\\\"\\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type'

ERROR: for web  Cannot start service web: b'OCI runtime create failed: container_linux.go:348: starting container process caused "process_linux.go:402: container init caused \\"rootfs_linux.go:58: mounting \\\\\\"/home/.../troubleshoot/docker_compose-mounting_issue/testfile.txt\\\\\\" to rootfs \\\\\\"/var/lib/docker/aufs/mnt/580b79241399e838c67b74021ecf2597aade5f1711811f4ab5c9c7bbcd188449\\\\\\" at \\\\\\"/var/lib/docker/aufs/mnt/580b79241399e838c67b74021ecf2597aade5f1711811f4ab5c9c7bbcd188449/etc/nginx\\\\\\" caused \\\\\\"not a directory\\\\\\"\\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type'
ERROR: Encountered errors while bringing up the project.

更新 2

所以我删除了 /var/lib/docker/ 中的所有内容现在它可以正常工作,挂载我的文件。它可能在某个地方坏了。我遇到的另一个建议是授予目录权限,chmod 777似乎对某些用户有所帮助,但我认为这并不安全..

最佳答案

用户拥有什么文件夹权限(您登录的用户运行 docker-compose 命令),如果是 root 用户或用户有权访问文件夹,请将 ${PWD} 更改为相对/绝对路径文件夹(您要挂载到容器的文件夹)。
docker-compose 无法理解 ${PWD} 环境变量。

例如:如果你想挂载

${PWD}/assets/master/etc/supervisor:/etc/supervisor



它应该是

./assets/master/etc/supervisor:/etc/supervisor

关于docker - 无法在 Docker 中挂载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50568151/

相关文章:

django - 允许用户访问管理员中的特定模型

docker - 使用 SSL 的 Nginx 子域重定向不起作用

docker - 如何列出特定架构的可用 Docker 镜像

maven - docker 构建所需的敏感信息

java - 通过JMX访问docker容器中运行的tomcat

.net - 防止 .NET 写入 C :\Windows\Temp

swift - 如何在 macOS 上使用 swift 在下载文件夹中创建目录?权限异常。

docker - 多个 docker-compose 项目之间的通信

docker - 将容器添加到现有的docker-compose.yml

php - Docker 上的 Nginx 仅提供欢迎页面