How to mount a host directory in a Docker container

后端 未结 25 1296
庸人自扰
庸人自扰 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:20

    i had same issues , i was trying to mount C:\Users\ folder on docker
    this is how i did it Docker Toolbox command line

     $ docker run -it --name <containername> -v /c/Users:/myVolData <imagename>
    
    0 讨论(0)
  • 2020-11-22 13:21

    2 successive mounts: I guess many posts here might be using two boot2docker, the reason you don't see anything is that you are mounting a directory from boot2docker, not from your host.

    You basically need 2 successive mounts:

    the first one to mount a directory from your host to your system

    the second to mount the new directory from boot2docker to your container like this:

    • 1) Mount local system on boot2docker

      sudo mount -t vboxsf hostfolder /boot2dockerfolder
      
    • 2) Mount boot2docker file on linux container

      docker run -v /boot2dockerfolder:/root/containerfolder -i -t imagename
      

    Then when you ls inside the containerfolder you will see the content of your hostfolder.

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

    I found that any directory laying under system directive like /var, /usr, /etc could not be mount under the container.

    The directive should be at user's space -v switch instructs docker daemon to mount local directory to the container, for example:

    docker run -t -d -v /{local}/{path}:/{container}/{path} --name {container_name} {imagename}
    
    0 讨论(0)
  • 2020-11-22 13:23

    You can also do this with Portainer web application for a different visual experience.

    First pull the Portainer image:

    docker pull portainer/portainer
    

    Then create a volume for Portainer:

    docker volume create portainer_data
    

    Also create a Portainer container:

    docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
    

    You will be able to access the web app with your browser at this URL: "http://localhost:9000". At the first login, you will be prompted to set your Portainer admin credentials.

    In the web app, follow these menus and buttons: (Container > Add container > Fill settings > Deploy Container)

    I had trouble to create a "mount" volume with Portainer and I realized I had to click "bind" when creating my container's volume. Below is an illustration of the volume binding settings that worked for my container creation with a mounted volume binded to the host.

    P.S.: I'm using Docker 19.035 and Portainer 1.23.1

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

    The user of this question was using Docker version 0.9.1, build 867b2a9, I will give you an answer for docker version >= 17.06.

    What you want, keep local directory synchronized within container directory, is accomplished by mounting the volume with type bind. This will bind the source (your system) and the target (at the docker container) directories. It's almost the same as mounting a directory on linux.

    According to Docker documentation, the appropriate command to mount is now mount instead of -v. Here's its documentation:

    • --mount: Consists of multiple key-value pairs, separated by commas. Each key/value pair takes the form of a <key>=<value> tuple. The --mount syntax is more verbose than -v or --volume, but the order of the keys is not significant, and the value of the flag is easier to understand.

    • The type of the mount, which can be bind, volume, or tmpfs. (We are going to use bind)

    • The source of the mount. For bind mounts, this is the path to the file or directory on the Docker daemon host. May be specified as source or src.

    • The destination takes as its value the path where the file or directory will be mounted in the container. May be specified as destination, dst, or target.

    So, to mount the the current directory (source) with /test_container (target) we are going to use:

        docker run -it --mount src="$(pwd)",target=/test_container,type=bind k3_s3
    

    If these mount parameters have spaces you must put quotes around them. When I know they don't, I would use `pwd` instead:

        docker run -it --mount src=`pwd`,target=/test_container,type=bind k3_s3
    

    You will also have to deal with file permission, see this article.

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

    I've been having the same issue. My command line looked like this:

    docker run --rm -i --name $NAME -v `pwd`:/sources:z $NAME
    

    The problem was with 'pwd'. So I changed that to $(pwd):

    docker run --rm -i --name $NAME -v $(pwd):/sources:z $NAME
    
    0 讨论(0)
提交回复
热议问题