简体   繁体   English

为什么我安装的docker卷文件会变成容器内的文件夹?

[英]Why are my mounted docker volume files turning into folders inside the container?

The scenario is docker inside/beside docker via a sock binding for the purpose of having an easily deployable and scalable runner agent for CI/CD tools (in this particular case, VSTS). 该方案是通过sock绑定在docker内部/旁边的docker,目的是为CI / CD工具提供易于部署和可扩展的运行代理(在此特定情况下为VSTS)。 The reason for this set up is that the various projects that I want to test use docker/compose to run tests, and configuring a CI/CD worker to be compatible with docker/compose a bunch of times gets cumbersome and time consuming. 这个设置的原因是我想要测试的各种项目使用docker / compose来运行测试,并且配置CI / CD工作器与docker / compose兼容很多次会变得麻烦和耗时。 (This'll eventually be deployed to 4+ Kubernetes Clusters) (这最终将部署到4+ Kubernetes集群)

Anyway, the problem: 无论如何,问题:

Steps to replicate 复制的步骤

  1. Run the vsts-agent image 运行vsts-agent映像

docker run \\ -it \\ -v /var/run/docker.sock:/var/run/docker.sock \\ nullvoxpopuli/vsts-agent-with-aws-ecr:latest \\ /bin/bash

  1. Run another image (to emulate docker/compose running tests) 运行另一个图像(模拟docker / compose运行测试)

echo 'test' > test-file.txt docker run -it -v file-test.txt:/file-test.txt busybox /bin/sh

  1. Check for existence of test-file.txt 检查test-file.txt是否存在

cd / ls -la # shows that test-file.txt is a directory

So, 所以,
- why are files being mounted as folders inside containers? - 为什么文件作为文件夹安装在容器中?
- what do I need to do to make the volumes mount correctly? - 要使卷正确安装,我需要做什么?

Solution A - thanks to @BMitch 解决方案A - 感谢@BMitch

# On Host machine
docker run -it \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /tmp/vsts/work/:/tmp/vsts/work \
   nullvoxpopuli/vsts-agent-with-aws-ecr:latest \
   /bin/bash

# In vsts-agent-with-aws-ecr
cd /tmp/vsts/work/
git clone https://NullVoxPopuli@bitbucket.org/group/project.git
cd project/
./scripts/run/eslint.sh
# Success! (this uses docker-compose to map files to the node-based docker image)

Docker creates containers and mounts volumes from the docker host. Docker从docker主机创建容器并装入卷。 Any time a file or directory in a volume mount doesn't exist, it gets initialized as an empty directory. 只要卷装入中的文件或目录不存在,它就会被初始化为空目录。 So if you are running docker commands from inside of a container to the docker socket those commands get interpreted outside the container on the docker host, where the file doesn't exist. 因此,如果您正在从容器内部到docker socket运行docker命令,那么这些命令将在docker主机上的容器外解释,该文件不存在。 Additionally, the docker run command requires a full path to the volume being mounted when you want a host volume, otherwise it's interpreted as a named volume. 此外,当您需要主机卷时, docker run命令需要指向要装入的卷的完整路径,否则它将被解释为命名卷。

What you likely want to do at this point is: 你现在可能想做的是:

docker volume rm file-test.txt
docker run -it -v $(pwd)/file-test.txt:/file-test.txt busybox /bin/sh

If instead you are trying to include a file from inside the container to another container, you can initialize a named volume with input redirection like this: 如果您试图将容器内的文件包含到另一个容器中,则可以使用输入重定向初始化命名卷,如下所示:

tar -cC . . | docker run -i --rm -v file-test:/target busybox tar -xC /target
docker run -it -v file-test:/data busybox /bin/sh

That uses tar to copy the contents of the current directory to stdout which is processed by the interactive docker command which then extracts those directory contents into /target inside the container which is a named volume. 它使用tar将当前目录的内容复制到stdout,后者由交互式docker命令处理,然后将这些目录内容提取到容器内的/ target内,该容器是命名卷。 Note that I didn't mount the volume in root in this second example since named volumes are directories and I didn't want to replace the root filesystem. 请注意,我没有在第二个示例中将卷装入root用户,因为命名卷是目录,我不想替换根文件系统。

Another option is to share a volume mount point between multiple containers on the docker host so that files you edit inside one container go to the host where they are mounted into the other container and visible there: 另一种选择是在docker主机上的多个容器之间共享一个卷挂载点,以便您在一个容器内编辑的文件转到主机,在那里它们被挂载到另一个容器中并在那里可见:

docker run \
  -it \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /container-data:/container-data \
  nullvoxpopuli/vsts-agent-with-aws-ecr:latest \
  /bin/bash
echo 'test' > /container-data/test-file.txt
docker run -it -v /container-data:/container-data busybox /bin/sh

I don't recommend mounting individual files into a container if these files may be modified while the container is running. 如果在容器运行时可能会修改这些文件,我建议不要将单个文件安装到容器中。 File changes often result in a changed inode and docker will have the old inode mounted into the container. 文件更改通常会导致更改的inode,并且docker会将旧的inode安装到容器中。 As a result, changes either inside or outside of the container to the file may not be seen on the other side, and if you modify the file inside the container, that change may be lost when you delete the container. 因此,在容器内部或外部对文件的更改可能在另一侧看不到,如果您修改容器内的文件,则删除容器时可能会丢失该更改。 The solution to the inode issue is to mount the entire directory into the container. inode问题的解决方案是将整个目录安装到容器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由docker容器写入已装入卷的文件的所有权 - ownership of files that are written by the docker container to mounted volume 安装到linux docker容器卷的Windows文件夹不显示所有文件夹 - Windows folder mounted to linux docker container volume not displaying all folders Docker容器未显示已装载的卷-访问问题 - Docker container not showing volume mounted - Access issue Docker挂载卷不跟踪Nginx日志文件 - Docker mounted volume not tracking nginx log files 在主机上更改已装入卷中的文件时,未在docker容器中触发文件系统事件 - File system events not triggered in docker container when files in mounted volume are changed on the host 在 Ubuntu 上运行 docker:安装的主机卷不可从容器写入 - Running docker on Ubuntu: mounted host volume is not writable from container Docker:已安装卷的权限 - Docker: Permissions for a mounted volume Docker-卷未在容器中挂载最新文件 - Docker - Volume not mounting latest files in container docker 容器的用户权限是否会影响主机对 docker-compose.yml 中挂载卷的权限? - Can user privilege of docker container impact permission of host machine on mounted volume in docker-compose.yml? 为什么不能在docker容器中找到可执行文件绑定装置 - why can't sh find executables bind mounted in docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM