How to actually bind mount a file in Docker for Windows
Asked Answered
T

3

7

I'm attempting to update the sebp/elk Logstash configuration following the documentation here. I'm running into a situation in which the host file that I am attempting to mount is being mounted as a directory in the container.

I found this related question How to mount a single file in a volume but the notion of running with PWD didn't work for me on Windows as I got the following error: C:\Program Files\Docker\Docker\Resources\bin\docker.exe: invalid reference format

I'm running Docker on Windows 10 (Build 16299.192)

λ docker version
Client:
 Version:      17.09.1-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   19e2cf6
 Built:        Thu Dec  7 22:22:26 2017
 OS/Arch:      windows/amd64

Server:
 Version:      17.09.1-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   19e2cf6
 Built:        Thu Dec  7 22:28:28 2017
 OS/Arch:      linux/amd64
 Experimental: true

My Docker run command is:

docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -it -v d:/docker/elk/logstash-snmp.conf:/etc/logstash/conf.d/logstash-snmp.conf --rm --name elk sebp/elk

I've been able to run other containers with persistent storage out of this disk (SQL Server, Redis, Exist-DB), but I'm not sure if I'm missing something on this. How can I tell Docker to actually mount this as a file and not as a directory.

Tenstrike answered 8/1, 2018 at 23:19 Comment(2)
Just make a folder for logstash and mount it at conf.d... Logstash actually expects a folder of all configurationsAdjoining
Also, PWD should work in powershell. #41485717Adjoining
U
5

This works for me:

Note: This approach addresses W10 Home Edition with Docker Toolbox and VirtualBox.

Overview: Create a folder in local-machine, mount this as a shared folder in Docker VM, use this shared folder as a bindmount to Docker container.

  1. Stop docker VM using docker-machine stop default
  2. Open VirtualBox, find default go to Settings > Shared Folder
  3. You will see c/Users is binded to your c:\Users
  4. Add a new shared folder, note the name it is assigned. Let's name this as [local-shared]
  5. Exit Settings
  6. docker-machine start default
  7. Once started, docker-machine ssh default
  8. sudo vi /mnt/sda1/var/lib/boot2docker/profile
  9. Append the following:

      # create a directory in VM
      mkdir /home/docker/[foldername]
      # mount/map shared folder on localmachine to directory
      sudo moun -t vboxsf -o uid=1000,gid=50 [local-shared] /home/docker/[foldername] 
    
  10. Save and exit ssh.

  11. docker-machine stop default
  12. docker-machine start default (or perhaps docker-machine restart default)
  13. now docker container run -d -p 1234:6789 -v /[local-shared]/sub-dir:/[container-dir] dockerImage2Run

And it should work.

Ref: http://support.divio.com/local-development/docker/how-to-use-a-directory-outside-cusers-with-docker-toolbox-on-windowsdocker-for-windows

Urinal answered 28/4, 2018 at 13:33 Comment(1)
Good steps. Step-13 needs correction, Instead of [local-shared], it should be /home/docker/[foldername]Agrarian
F
8

If you are on windows, there is a problem in mapping the paths. I am using Git bash terminal on windows and the command which works for me is in this format.

  1. To copy a file named runtime_config.yml from host to container. The host path is given relative by using PWD as below. And the container path is from root.
docker run -d -p 5000:5000 -v "/${PWD}/etc/runtime_config.yml":/demand_forecast/etc/runtime_config.yml ....
  1. To copy a directory named etc from host to container. The host path is given relative by using PWD as below. And the container path is from root.
docker run -d -p 5000:5000 -v "/${PWD}/etc/":/demand_forecast/etc/ ....

Notice the / at the beginning in host path, that is very important, otherwise you will run into errors.

Fowkes answered 24/11, 2020 at 7:2 Comment(2)
if someone wants to run the same on Powershell docker run -p 9090:9090 -v "$(pwd)/test.yml:/etc/test/test.yml" <image>Medrek
Upvoted for the "Notice the / at the beginning in host path". I'm using Git Bash and I was having strange path errors until I figured out Git Bash modifies the paths in the command line, but "double slash" at the beginning of the path avoids this behavior.Marquet
U
5

This works for me:

Note: This approach addresses W10 Home Edition with Docker Toolbox and VirtualBox.

Overview: Create a folder in local-machine, mount this as a shared folder in Docker VM, use this shared folder as a bindmount to Docker container.

  1. Stop docker VM using docker-machine stop default
  2. Open VirtualBox, find default go to Settings > Shared Folder
  3. You will see c/Users is binded to your c:\Users
  4. Add a new shared folder, note the name it is assigned. Let's name this as [local-shared]
  5. Exit Settings
  6. docker-machine start default
  7. Once started, docker-machine ssh default
  8. sudo vi /mnt/sda1/var/lib/boot2docker/profile
  9. Append the following:

      # create a directory in VM
      mkdir /home/docker/[foldername]
      # mount/map shared folder on localmachine to directory
      sudo moun -t vboxsf -o uid=1000,gid=50 [local-shared] /home/docker/[foldername] 
    
  10. Save and exit ssh.

  11. docker-machine stop default
  12. docker-machine start default (or perhaps docker-machine restart default)
  13. now docker container run -d -p 1234:6789 -v /[local-shared]/sub-dir:/[container-dir] dockerImage2Run

And it should work.

Ref: http://support.divio.com/local-development/docker/how-to-use-a-directory-outside-cusers-with-docker-toolbox-on-windowsdocker-for-windows

Urinal answered 28/4, 2018 at 13:33 Comment(1)
Good steps. Step-13 needs correction, Instead of [local-shared], it should be /home/docker/[foldername]Agrarian
W
0

I have found the easiest way to Bind Mount on Windows. Use this format:

docker container run -d -p 8080:80 -v /{Drive}/{Folder(Optional)}:/{Container Path}/{Target} --name your-wish your-image.

Example:

docker container run -d -p 8080:80 -v /d/Container:/usr/share/nginx/html --name my-website nginx

Note: I already created a 'Container' Folder in my D drive

Wendywendye answered 5/10 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.