"volume" 参数在 docker 中如何工作?

How does the "volume" argument work in docker?

我的 .gitlabci 文件中有以下几行:

- echo "$PWD"
- ls -l "$PWD"
- docker run --volume "$PWD":/code debian:stable ls -l /code

下面是输出:

$ echo "$PWD" 
/builds/ben/project 

$ ls -l "$PWD" total 52
-rw-rw-rw-    1 root     root           946 Feb 19 14:53 ChangeLog
-rw-rw-rw-    1 root     root           294 Feb 19 14:53 INSTALL
-rw-rw-rw-    1 root     root          4341 Feb 19 14:53 Jenkinsfile
-rw-rw-rw-    1 root     root           353 Feb 19 14:53 README.md
-rw-rw-rw-    1 root     root           535 Feb 19 14:53 TODO 
drwxrwxrwx    4 root     root          4096 Feb 19 14:53 application
drwxrwxrwx    2 root     root          4096 Feb 19 14:53 library 
drwxrwxrwx    3 root     root          4096 Feb 19 14:53 public 
drwxrwxrwx    3 root     root          4096 Feb 19 14:53 tests 

$ docker run --volume "$PWD":/code debian:stable ls -l /code 
total 0

以下是我的 .gitlabci 文件:

code_quality:
  image: docker:stable

  variables:
    DOCKER_DRIVER: overlay

  allow_failure: true

  services:
    - docker:dind

  script:
    - echo "$PWD"
    - ls -l "$PWD"
    - docker run --volume "$PWD":/code debian:stable ls -l /code

最后是我的跑步者的config.toml:

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "test"
  url = "http://docker.int.com:1040/"
  token = "*************"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "docker:stable"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock","/cache"]
    extra_hosts = ["mygitlab.com:XXX.XXX.XXX.XXX"]
    shm_size = 0
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

我不明白为什么最后一条命令没有显示我的 $PWD 目录的内容 (/builds/ben/project)。我的卷似乎没有安装成功,但我没有收到任何错误。

我确定您正在使用 docker-in-docker 模式。

在这种模式下,管道中的 docker 命令有效地连接到 HOST 机器的 docker 守护进程并将所有命令发送到那里。结果,host 尝试将 /builds/ben/project 路径绑定到容器中,但没有这样的路径。同样,按照绑定安装行为,此文件夹将被创建为空并映射到容器中。

如果您正在使用非特权模式(映射 /var/run/docker.sock),请尝试切换到使用 docker:dind 作为服务。在这种情况下,守护进程将从您的构建作业中获取上下文。否则您必须手动将数据复制到卷中。

UPD

当使用 /var/run/docker.sock 时,您实际上将 docker 命令未修改地传递给守护进程,守护进程在他自己的上下文 中执行它们 。这意味着他使用他的文件系统,而不是你的(工作的)。我已经设法通过 手动克隆 源到新容器中使用像

这样的命令来绕过这个
git clone ${CI_REPOSITORY_URL} --branch ${CI_COMMIT_REF_NAME} --single-branch /tmp/project

缺点是您需要将 git 安装到您的图像中。

这就是gitlab writes:

Sharing files and directories from the source repo into containers may not work as expected since volume mounting is done in the context of the host machine, not the build container