How to mount a directory in the docker container to the host?

后端 未结 5 2135
广开言路
广开言路 2020-12-31 16:05

It\'s quite easy to mount a host directory in the docker container.

But I need the other way around.

I use a docker container as a development environment fo

相关标签:
5条回答
  • 2020-12-31 16:40

    Why not just do: docker run ... -v /usr/share/wordpress/:/usr/share/wordpress. Now your local /usr/share/wordpress/ is mapped to /usr/share/wordpress in the Docker container and both have the same files. You could also mount elsewhere in the container this way. The syntax is host_path:container_path, so if you wanted to mount /usr/share/wordpress from your host to /my/new/path on the container, you'd just do: docker run ... -v /usr/share/wordpress/:/my/new/path.

    0 讨论(0)
  • 2020-12-31 16:41

    I had a similar need of exposing the files from container to the host. There is an open issue on this as of today. One of the work-arounds mentioned, using binds, is pretty neat; it works when the container is up and running:

    container_root=$(docker inspect --format {{.State.Pid}} "$container_name")/root
    sudo bindfs --map=root/"$USER" "$container_root/$app_folder" "$host_folder"
    

    PS: I am not sure this is good for production, but it should work in development scenarios!

    0 讨论(0)
  • 2020-12-31 16:47

    You can share the files using smb with svendowideits samba container like this:

    docker run --rm -v $(which docker):/docker -v /var/run/docker.sock:/docker.sock svendowideit/samba <container name>
    
    0 讨论(0)
  • 2020-12-31 17:04

    It's possible to do if you use volume instead of filesystem path. It's created for you automatically, if it already doesn't exist.

    docker run -d -v usr_share_wordpress:/usr/share/wordpress --name your_container ... image
    

    After you stop or remove your container, your volume will be stored on your filesystem with files from container.

    You can inspect volume content during lifetime of your_container with busybox image. Something like:

    docker run -it --rm --volumes-from your_container busybox sh
    

    After shutdown of your_container you can still check volume with:

    docker run -it --rm -v usr_share_wordpress:/usr/share/wordpress busybox sh
    

    List volumes with docker volume ls.

    0 讨论(0)
  • 2020-12-31 17:05

    Copying the WordPress files to the mounted folder was the solution.

    I move the files in the container from the original folder to the mounted folder and use symbolic links to link them back to the original folder.

    The important part is, the container can follow symbolic links in the container and but the host can't. So just using symbolic links from the original folder to the mounted folder doesn't work, because the host cannot follow symbolic links in the container!

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