代码之家  ›  专栏  ›  技术社区  ›  Joaquin

如何从docker compose或docker run写入已填充的现有卷

  •  0
  • Joaquin  · 技术社区  · 6 年前

    问题是:

    我想要一个在几个容器之间共享的卷。其思想是在使用docker run或docker compose初始化容器时向这个共享卷添加/写入信息(还有其他方法吗?)。

    shared-volume :

    docker volume create shared-volume
    

    必须 写入该卷,如果该卷为空,则 some-directory-with-content/ 会被写进书里,这很好:

    docker run -ti --rm --mount source=shared-volume,target=/some-directory-with-content/ custom-image
    

    共享卷 它包含中的文件 /some-directory-with-content/ ,这里是我不知道什么是正确的方式来写这个 从docker run或docker compose。

    当我试着对第二个容器做同样的操作时:

    docker run -ti --rm --mount source=shared-volume,target=/another-directory-with-content/ custom-image
    

    它只返回前面的内容,我知道这是预期的,所以这里是我不知道的地方,如果是不可能实现这种写入 共享卷 使用docker run或docker compose或什么是正确的方法。

    0 回复  |  直到 6 年前
        1
  •  1
  •   Rob Bednark Bohdan    5 年前

    你可以通过 sh somefile 一些文字 在文件中。

    只需多次运行此命令即可进行测试。

    docker run \
      --rm \
      --mount source=shared-volume,target=/some-directory-with-content/ \
      alpine \
      sh -c "touch /some-directory-with-content/somefile && cat /some-directory-with-content/somefile && echo "some-text" >> /some-directory-with-content/somefile"
    

    这是预期结果:

    neo@neo-desktop:shared-vol-demo$ docker run --rm --mount source=shared-volume,target=/some-directory-with-content/ alpine sh -c "touch /some-directory-with-content/somefile && cat /some-directory-with-content/somefile && echo "some-text" >> /some-directory-with-content/somefile"
    neo@neo-desktop:shared-vol-demo$ docker run --rm --mount source=shared-volume,target=/some-directory-with-content/ alpine sh -c "touch /some-directory-with-content/somefile && cat /some-directory-with-content/somefile && echo "some-text" >> /some-directory-with-content/somefile"
    some-text
    neo@neo-desktop:shared-vol-demo$ docker run --rm --mount source=shared-volume,target=/some-directory-with-content/ alpine sh -c "touch /some-directory-with-content/somefile && cat /some-directory-with-content/somefile && echo "some-text" >> /some-directory-with-content/somefile"
    some-text
    some-text
    
    推荐文章