In Docker, how do I share a volume from a containe

2019-08-10 01:38发布

I know you can share a directory that exists on the host with a container using the VOLUMES directive, but I'd like to do the opposite--share the files that exist in the container with the host, provided that nothing exists in that directory on the host.

The use case for this is that we have contractors, and we'd like to provide them with a docker container that contains all the code and infrastructure they need to work with, but that would allow them to alter that code by running PyCharm or some other program on their local machine without having to worry about committing the container, ...etc.

2条回答
神经病院院长
2楼-- · 2019-08-10 01:54

In your Dockerfile you can easily ADD or COPY whole directory.

I guess this is what you are looking for. Otherwise you can just share the code and at the docker run so they can volume-mount the shared code.

查看更多
Explosion°爆炸
3楼-- · 2019-08-10 02:09

In order to be able to save (persist) data, Docker came up with the concept of volumes. basically Volumes are directories (or files) that are outside of the default Union File System and exist as normal directories and files on the host filesystem.

You can declare a volume at run-time with the -v flag:

$ docker run -it --name container1 -v /data debian /bin/bash

This will make the directory /data inside the container live outside the Union File System and directly accessible on the host. Any files that the image held inside the /data directory will be copied into the volume. We can find out where the volume lives on the host by using the docker inspect command on the host.

Open a new terminal and leave the previous container running and run:

$ docker inspect container1

The output will provide details on the container configurations including the volumes. The output should look something similar to the following:

...
Mounts": [
    {
        "Name": "fac362...80535",
        "Source": "//var/lib/docker/vfs/dir/cde167197ccc3e/_data",
        "Destination": "/data",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
]
...

Telling us that Docker has mounted /data inside the container as a directory somewhere under /var/lib/docker in the host.

查看更多
登录 后发表回答