Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you mount the docker socket on Windows?

I'm trying to make an application work on Windows that's been developed only on Unices. It's all dockerized and it uses the traefik load balancer. The volumes for the docker for running traefik looks like this:

volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro,delegated
- ${PWD}/load_balancer/traefik.toml:/etc/traefik/traefik.toml:ro,delegated

The first volume works fine on Mac or Linux, but does it on Windows? The application is failing (the load balancer is giving a 404) and it might be related to that volume. When I start the image, the socket looks like a socket:

/ # ls -laF /var/run/docker.sock
srw-rw----    1 root     root             0 Sep  2 11:04 /var/run/docker.sock=

Is this working? Any way to test it? What's the correct way of doing this?

Trying to figure this out, I tried replacing it with this:

volumes:
- //./pipe/docker_engine:/var/run/docker.sock

based on various articles and bug reports I found online. The docker image starts but it fails in the same way and now in the docker container it looks like a directory:

/ # ls -laF /var/run/docker.sock
total 4
drwxr-xr-x    2 root     root            40 Sep  3 14:52 ./
drwxr-xr-x    1 root     root          4096 Sep  3 14:57 ../

Following Marc ABOUCHACRA's answers, I tried:

volumes:
- type: npipe
  source: ////./pipe/docker_engine
  target: /var/run/docker.sock
  consistency: delegated

but that also looks like a directory:

/ # ls -laF /var/run/docker.sock
total 4
drwxr-xr-x    2 root     root            40 Sep  3 14:52 ./
drwxr-xr-x    1 root     root          4096 Sep  3 14:57 ../

I also tried this:

volumes:
- npipe:////./pipe/docker_engine:/var/run/docker.sock:ro,delegated

but that fails with this error:

ERROR: Volume npipe:////./pipe/docker_engine:/var/run/docker.sock:ro,delegated has incorrect format, should be external:internal[:mode]

The whole docker-compose.yml section looks like this:

  lb:
    image: load-balancer
    build: ${WORKSPACE}/go-home/load_balancer
    ports:
    - 80:80
    - 443:443
    links:
    - wifi-ui-dev
    - wifi-ui-prod
    - portal
    - wifi-api
    env_file:
    - .env
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock:ro,delegated
    - ${PWD}/load_balancer/traefik.toml:/etc/traefik/traefik.toml:ro,delegated

My question is specifically about running this docker image, which is a Linux, on a Windows host, running Docker for Windows. I understand that I can run it on a Linux host by installing Linux on another machine or a VM on the Windows machine, it's equivalent. Running Windows guests is not what I'm after either in case there's a way of exposing sockets from Windows to Windows only.

like image 949
pupeno Avatar asked Aug 12 '19 18:08

pupeno


People also ask

Where is Docker socket on Windows?

The Docker daemon listens to a socket at /var/run/docker. sock, responding to calls to the Docker API. If we want to be able to issue Docker commands from a container, we'll need to communicate with this socket.

How do you secure a Docker socket?

Use TLS (HTTPS) to protect the Docker daemon socket. If you need Docker to be reachable through HTTP rather than SSH in a safe manner, you can enable TLS (HTTPS) by specifying the tlsverify flag and pointing Docker's tlscacert flag to a trusted CA certificate.

Can I use Docker toolbox on Windows 10?

Operating System If you do not run a 64-bit version of Windows Windows 10 Pro, Enterprise, or Education; 1511 November update, Build 10586 or later, you cannot run Docker for Windows. You can install Docker Toolbox if you have a 64-bit version of Windows 7 or later.


3 Answers

If you cannot nor want use network sockets, then you can use named pipes. The syntax depends whether you run Linux or Windows containers and on the shell you use.

Linux containers

If you run Linux containers on a Windows machine, this seems to work using Powershell or bash:

docker run --rm -it -v "//var/run/docker.sock://var/run/docker.sock" image_with_docker docker version

Please note the extra / in front of /var/run/docker.sock, both for the source and destination volumes.

Windows containers

If you run Windows containers on a Windows machine, this seems to work using Powershell or bash:

docker run -v "//./pipe/docker_engine://./pipe/docker_engine" --rm -it image-with-docker docker version

Note that this works only in Powershell:

docker run -v "\\.\pipe\docker_engine:\\.\pipe\docker_engine" --rm -it image-with-docker docker version

Therefore, it's better to use the version with /.

Extra - docker-compose.yml

If you use a docker-compose.yaml file, this works with Windows containers.

version: '3.7'

services:
  docker:
    image: image-with-docker
    command:
      - docker
      - version
    volumes:
      - type: npipe
        source: \\.\pipe\docker_engine
        target: \\.\pipe\docker_engine

With Linux containers, you can use the shortened form:

  docker:
    image: image-with-docker
    command:
      - docker
      - version
    volumes:
      - //var/run/docker.sock://var/run/docker.sock

Extra - Kubernetes

If you are running Windows containers on a Windows node in Kubernetes, this seems to work:

apiVersion: v1
kind: Pod
spec:
  containers:
    - name: docker
      image: image-with-docker
      command:
        - powershell
      args:
        - Start-Sleep
        - "999999"
      volumeMounts:
        - mountPath: \\.\pipe\docker_engine
          name: dockersock
  volumes:
    - name: dockersock
      hostPath:
        path: \\.\pipe\docker_engine
        type: null
  nodeSelector:
    kubernetes.io/os: windows

In this case, beside using the \, please note the type: null in the definition of the dockersock volume: if you don't set it, it will not work.

Notes

Everything was tested on docker 19.03 and on Kubernetes 1.18.

Client:
 Version:           19.03.3
 API version:       1.40
 Go version:        go1.12.10
 Git commit:        2355349d-
 Built:             10/14/2019 16:41:26
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.24)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:37:20 2020
  OS/Arch:          windows/amd64
  Experimental:     false
like image 155
Fabio Avatar answered Oct 22 '22 05:10

Fabio


If you encounter the following error on windows:

cannot create container for service portainer: Unrecognised volume spec: file '\.\pipe\docker_engine' cannot be mapped. Only directories can be mapped on this platform ERROR: Encountered errors while bringing up the project.

Try adding an extra slash to it, resulting in following volumes section:

volumes:
  - source: \\.\pipe\docker_engine\
    target: \\.\pipe\docker_engine\
    type: npipe

Tested with compose 3.7 and docker CE 19.03.12

like image 5
Sven Hakvoort Avatar answered Oct 22 '22 03:10

Sven Hakvoort


Using short syntax with the type of the bind mount is not possible : npipe:////./pipe/docker_engine:/var/run/docker.sock:ro,delegated

You need to use the long syntax in your compose file :

volumes:
  - type: npipe
    source: ////./pipe/docker_engine
    target: /var/run/docker.sock
    consistency: delegated

You can find some documentation about the long syntax in the official documentation. This syntaxe is from v3.2

Also keep in mind what @lucas-ramage said about using windows container only when using npipe.

like image 1
Marc ABOUCHACRA Avatar answered Oct 22 '22 04:10

Marc ABOUCHACRA