How to mount a host directory in a Docker container

后端 未结 25 1605
庸人自扰
庸人自扰 2020-11-22 12:43

I am trying to mount a host directory into a Docker container so that any updates done on the host is reflected into the Docker containers.

Where am I doing somethin

25条回答
  •  一生所求
    2020-11-22 13:16

    There are a couple ways you can do this. The simplest way to do so is to use the dockerfile ADD command like so:

    ADD . /path/inside/docker/container
    

    However, any changes made to this directory on the host after building the dockerfile will not show up in the container. This is because when building a container, docker compresses the directory into a .tar and uploads that context into the container permanently.

    The second way to do this is the way you attempted, which is to mount a volume. Due to trying to be as portable as possible you cannot map a host directory to a docker container directory within a dockerfile, because the host directory can change depending on which machine you are running on. To map a host directory to a docker container directory you need to use the -v flag when using docker run like so:

    docker run -v /host/directory:/container/directory -other -options image_name command_to_run
    

提交回复
热议问题