How to mount a host directory in a Docker container

后端 未结 25 1292
庸人自扰
庸人自扰 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
    
    0 讨论(0)
  • 2020-11-22 13:17

    boot2docker together with VirtualBox Guest Additions
    How to mount /Users into boot2docker

    https://medium.com/boot2docker-lightweight-linux-for-docker/boot2docker-together-with-virtualbox-guest-additions-da1e3ab2465c

    tl;dr Build your own custom boot2docker.iso with VirtualBox Guest Additions (see link) or download http://static.dockerfiles.io/boot2docker-v1.0.1-virtualbox-guest-additions-v4.3.12.iso and save it to ~/.boot2docker/boot2docker.iso.

    0 讨论(0)
  • 2020-11-22 13:17

    I had the same requirement to mount host directory from container and I used volume mount command. But during testing noticed that it's creating files inside container too but after some digging found that they are just symbolic links and actual file system used form host machine.

    0 讨论(0)
  • 2020-11-22 13:18

    If the host is windows 10 then instead of forward slash, use backward slash -

    docker run -it -p 12001:80 -v c:\Users\C\Desktop\dockerStorage:/root/sketches
    

    Make sure the host drive is shared (C in this case). In my case I got a prompt asking for share permission after running the command above.

    0 讨论(0)
  • 2020-11-22 13:20

    I'm just experimenting with getting my SailsJS app running inside a Docker container to keep my physical machine clean.

    I'm using the following command to mount my SailsJS/NodeJS application under /app:

    cd my_source_code_folder
    docker run -it -p 1337:1337 -v $(pwd):/app my_docker/image_with_nodejs_etc
    
    0 讨论(0)
  • 2020-11-22 13:20
    docker run -v /host/directory:/container/directory -t IMAGE-NAME /bin/bash
    
    docker run -v /root/shareData:/home/shareData -t kylemanna/openvpn /bin/bash
    

    In my system I've corrected the answer from nhjk, it works flawless when you add the -t flag.

    0 讨论(0)
提交回复
热议问题