99

On *nix systems, it is possible to bind-mount the docker socket from the host machine to the VM by doing something like this:

docker run -v /var/run/docker.sock:/var/run/docker.sock ...

Is there an equivalent way to do this when running docker on a windows host?


I tried various combinations like:

docker run -v tcp://127.0.0.1:2376:/var/run/docker.sock ...

docker run -v "tcp://127.0.0.1:2376":/var/run/docker.sock ...

docker run -v localhost:2376:/var/run/docker.sock ...

none of these have worked.

Theolodus
  • 2,004
  • 1
  • 23
  • 30

11 Answers11

161

For Docker for Windows following seems to be working:

-v //var/run/docker.sock:/var/run/docker.sock

Alexander Chichenin
  • 1,642
  • 1
  • 8
  • 6
  • 1
    This really works!! I have been trying to get this working forever. Thanks a lot! – potibas Jan 11 '17 at 01:53
  • 23
    Some of the above answers seem like they aren't referring to running Windows containers on Docker for Windows. When I try to run the standard command for doing this: `docker run -v /var/run/docker.sock:/var/run/docker.sock` I get the following error: `docker: Error response from daemon: invalid bind mount spec "//var/run/docker.sock:/var/run/docker.sock": invalid volume specification: '\\var\run\docker.sock:\var\run\docker.sock'` The error makes sense because as I'm on Windows, trying to run a Windows container the paths don't make sense. – Josh Wittner May 04 '17 at 20:45
  • 2
    @JoshWittner: have you found a solution to make the Docker Socket available inside Windows Containers with Windows Hosts? – Martin May 17 '17 at 11:24
  • 2
    This works indeed, do you now the reason? Is there any official documentation for this? – Kevin Wittek Dec 10 '17 at 13:52
  • 4
    After 2 days of googling, I finally found the answer! – Utwo Mar 26 '18 at 10:58
  • 1
    It really works when you use `docker run`, but it wont work when you use it in docker-compose file – Igor S May 08 '18 at 10:42
  • @Kevin It's probably a bit late, but Docker on Windows actually runs in a virtual machine, so this command is likely mapping to that VM, rather than a real host machine disk. – ProgrammingLlama Jul 04 '18 at 01:26
  • @john of course you are right about the VM (and I knew this), I was wondering about the leading `/`. So you mean the leading `/` is a hack for referencing the VM? – Kevin Wittek Jul 04 '18 at 18:41
  • 1
    @Kevin I believe so. If you use `-v //:/testfolder` and inspect /testfolder in the docker container, you can see things like `/usr`, `/bin`, `/mnt`, etc. – ProgrammingLlama Jul 05 '18 at 01:44
  • 6
    I believe it's not magic its just path expansion in git bash on windows. Using / in front of the parameter disables path expansion. See here: https://stackoverflow.com/questions/34647591/passing-windows-slash-based-parameters-to-a-program-from-bash-script Took me a while to figure this out but thanks to this answer I saved a lot of time. Thanks everyone!! – wholenewstrain Aug 27 '18 at 06:41
  • 7
    Anyone know is there's a "2018" solution for this (Since Docker on Windows no longer uses a VM to run Windows containers?) – Mike Christensen Sep 19 '18 at 17:58
  • 1
    @MikeChristensen, i have tested using the hyper-v enabled docker and it works just fine, but to do the same with docker-compose, one must set the environment `COMPOSE_CONVERT_WINDOWS_PATHS=1` either in CLI or a `.env` file at the same location as the project's docker-compose.yml, [source](https://github.com/docker/for-win/issues/1829#issuecomment-417376091) – Sean W Oct 18 '18 at 03:01
  • @alexander-chichenin Here's a big hug :) – dovidweisz Mar 26 '19 at 18:11
  • What's gonna happen if I `docker attach` to myself LOL – dovidweisz Mar 26 '19 at 18:14
  • 1
    It is not working for me :( I'm running: docker run -d -p 49001:8080 -u 0 -v D:/jenkins_home:/var/jenkins_home:z -v //var/run/docker.sock:/var/run/docker.sock -t jenkins/jenkins – GabrielBB Sep 03 '19 at 20:31
  • Thanks!! Been trying to do the Jenkins tutorial and this is what got it going! – trpt4him Dec 20 '19 at 20:32
21

As the Docker documentation states:

If you are using Docker Machine on Mac or Windows, your Engine daemon has only limited access to your OS X or Windows filesystem. Docker Machine tries to auto-share your /Users (OS X) or C:\Users (Windows) directory. So, you can mount files or directories on OS X using:

docker run -v /Users/<path>:/<container path> ...

On Windows, mount directories using:

docker run -v /c/Users/<path>:/<container path> ...

All other paths come from your virtual machine’s filesystem, so if you want to make some other host folder available for sharing, you need to do additional work. In the case of VirtualBox you need to make the host folder available as a shared folder in VirtualBox. Then, you can mount it using the Docker -v flag.

With all that being said, you can still use the:

docker run -v /var/run/docker.sock:/var/run/docker.sock ...

The first /var/run/docker.sock refers to the same path in your boot2docker virtual machine.

For example, when I run my own Jenkins image using the following command in a Windows machine:

$ docker run -dP -v /var/run/docker.sock:/var/run/docker.sock alidehghanig/jenkins

I can still talk to the Docker Daemon in the host machine using the typical docker commands. For example, when I run docker ps in the Jenkins container, I can see running containers in the host machine:

CONTAINER ID   IMAGE  COMMAND   CREATED  STATUS  PORTS        NAMES
65311731f446   jen... "/bi.."   10...    Up 10.. 0.0.0.0:..  jenkins
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
9

Just to top it off on the answers provided earlier

When using docker-compose, one must set the COMPOSE_CONVERT_WINDOWS_PATHS=1 by either:

1) create a .env file at the same location as the project's docker-compose.yml file

2) in the CLI set COMPOSE_CONVERT_WINDOWS_PATHS=1

before running the docker-compose up command.

source

Sean W
  • 527
  • 6
  • 15
7

This never worked for me on Windows 10 even if it is a linux container:

-v /var/run/docker.sock:/var/run/docker.sock

But this did:

-v /usr/local/bin/docker:/usr/bin/docker

Solution taken from this issue i opened: https://github.com/docker/for-win/issues/4642

GabrielBB
  • 2,479
  • 1
  • 35
  • 49
7

To bind to a Windows container you need to use pipes.

-v \\.\pipe\docker_engine:\\.\pipe\docker_engine
Josh Close
  • 22,935
  • 13
  • 92
  • 140
5

What it was suitable for me in Windows 10 was:

 -v "\\.\pipe\docker_engine:\\.\pipe\docker_engine"

Have in mind that I was trying to access to portainer that I do recommend a lot it's a great app. For that I use this command:

docker run -d -p 9000:9000 -v "\\.\pipe\docker_engine:\\.\pipe\docker_engine" portainer/portainer 

And then just go to:

http://localhost:9000/
paulofer85
  • 555
  • 11
  • 15
4

Some containers (eg. portainer) work fine with -v /var/run/docker.sock:/var/run/docker.sock The jenkins container required --user root permissions on the docker run command to successfully access the Docker UNIX socket (using Docker-Desktop on Windows).

By default, a unix domain socket (or IPC socket) is created at /var/run/docker.sock, requiring either root permission, or docker group membership.

Source: https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-socket-option

--group-add docker had no effect using Docker-Desktop on Windows.

naaman
  • 900
  • 10
  • 15
1

This worked for me on Windows 11;

-v //./pipe/docker_engine://./pipe/docker_engine

Let me explain the reason I'm posting this is, I've tried the back-slash version which is written in the answers above in this post, and that is not working. I've tried the followings;

-v \\.\pipe\docker_engine:\\.\pipe\docker_engine

neither wrapped with quotes as follows worked;

-v "\\.\pipe\docker_engine:\\.\pipe\docker_engine"

Popped out this error;

docker: Error response from daemon: .\pipe\docker_engine%!(EXTRA string=is not a valid Windows path).

Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106
0

I never made it worked myself, but i know it works on windows container on docker for windows server 2016 using this technique: https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-socket-option

We actually have at the shop vsts-agents on windows containers that uses the host docker like that:

# listen using the default unix socket, and on 2 specific IP addresses on this host.    
$ sudo dockerd -H unix:///var/run/docker.sock -H tcp://192.168.59.106 -H tcp://10.10.10.2

# then you can execute remote docker commands (from container to host for example)
$ docker -H tcp://0.0.0.0:2375 ps
jackstrapp
  • 195
  • 10
0

This is what actually made it work for me

docker run -p 8080:8080 -p 50000:50000 -v D:\docker-data\jenkins:/var/jenkins_home -v /usr/local/bin/docker:/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -u root jenkins/jenkins:lts
Stefan
  • 369
  • 2
  • 4
0

it works well :

docker run -it -v //var/run/docker.sock:/var/run/docker.sock -v /usr/local/bin/docker:/usr/bin/docker ubuntu
zizhen zhan
  • 200
  • 3
  • 6