无法在Docker中挂载文件 [英] Unable to mount files in Docker

查看:197
本文介绍了无法在Docker中挂载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm unable to mount files in Vagrant or Docker, so it seems like it's an issue caused by some kind of a permission error.
My OS is Ubuntu 18.04 LTS (Bionic Beaver), I'm not running any access control modules like SELinux as far as I'm aware.

与流浪者有关的错误讨论在另一个问题中找到: 无法在Vagrant中挂载文件

The Vagrant-related discussion of the error is found in another question: Unable to mount files in Vagrant

我无法使用docker-compose将文件装载到Docker容器中,我正在尝试构建:

I'm unable to mount files into a Docker container with docker-compose, I'm trying to build: https://github.com/fredrikaverpil/saltstack-docker

我将docker-compose文件中的卷设置为:

I set my volumes in the docker-compose file to:

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时得到了以下输出:

Yet I'm getting this output when running 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放置容器显示正确的配置:

docker inspecting the container shows correct configuration:

   "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"
        }

但是进入容器(使用另一个entrypoint命令),表明应该挂载的文件丢失:

But entering the container (with another entrypoint command), shows that the files that are supposed to be mounted are missing:

[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和容器中所需的访问权限有关.
如果有人能对此有所启发,我将不胜感激.

Conclusion

It seems something is very messed up on my local machine when it comes to copying over files, possibly has to do with how my user is configured and what access it has to mounting files into VMs and containers.
If anyone can shed some light on this I'd appreciate it.

我怀疑这是我要删除的/var/lib/docker中旧内容的问题,但是在此之前,我遇到了另一个错误,也许是某种权限错误:

I'm suspecting this is an issue with old stuff in /var/lib/docker which I'm about to remove, but before that here's another error I'm getting, maybe some kind of permission error:

> 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似乎可以帮助某些用户,但我认为这样做不安全.

Update 2

So I removed everything in /var/lib/docker/ and it now works as it should, mounting my files. It was probably broken somewhere. Another suggestion I came across was giving permissions to the directory, chmod 777 seemed to help some users but I don't think this is that safe..

推荐答案

该用户具有的文件夹权限(您登录的文件夹权限运行docker-compose命令)(如果该用户是root用户或该用户具有访问权限)到文件夹,然后将$ {PWD}更改为文件夹的相对/绝对路径(要安装到容器的相对路径). docker-compose无法理解$ {PWD} env变量.

what folder permission that the user has(the one you are logged into run the docker-compose command), if that is is root user or the user has access to the folders and please change the ${PWD} to the relative/absolute path of the folder(one which you want to mount to container). docker-compose cant understand the ${PWD} env variable.

例如:如果要挂载

$ {PWD}/资产/master/etc/supervisor:/etc/supervisor

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

应该是

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

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

这篇关于无法在Docker中挂载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆