如何在Windows上安装Docker套接字? [英] How do you mount the docker socket on Windows?

查看:67
本文介绍了如何在Windows上安装Docker套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使仅在Unices上开发的Windows上的应用程序可以工作.所有这些都经过docker化,并使用traefik负载均衡器.用于运行traefik的docker的卷如下所示:

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

第一个卷在Mac或Linux上可以正常运行,但是在Windows上可以吗?应用程序失败(负载均衡器正在a 404),并且可能与该数量有关.当我启动图像时,套接字看起来像一个套接字:

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 ../

按照Marc ABOUCHACRA的回答,我尝试了:

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 ../

我也尝试过:

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]

整个 docker-compose.yml 部分如下所示:

  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

我的问题特别是关于在Windows主机上运行此Docker映像(这是Linux)并运行Docker for Windows.我知道可以通过在另一台计算机上安装Linux或在Windows计算机上安装VM来在Linux主机上运行它.如果有一种方法可以将套接字从Windows公开到Windows,那么我也不希望运行Windows guest.

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.

推荐答案

如果您既不能也不希望使用网络套接字,则可以使用命名管道.语法取决于您运行的是Linux容器还是Windows容器以及所使用的Shell.

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.

如果您在Windows机器上运行Linux容器,则似乎可以使用Powershell或bash来工作:

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

请注意在/var/run/docker.sock 前面的额外的/,用于源卷和目标卷.

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

如果您在Windows机器上运行Windows容器,则似乎可以使用Powershell或bash进行工作:

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

请注意,这仅在Powershell中有效:

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 /.

如果您使用docker-compose.yaml文件,则此文件适用于Windows容器.

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

对于Linux容器,您可以使用缩写形式:

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

额外-Kubernetes

如果您正在Kubernetes的Windows节点上运行Windows容器,那么这似乎可行:

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

在这种情况下,除了使用 \ 外,请注意dockersock卷的定义中的 type:null :如果不设置,它将不起作用.

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.

所有内容都在docker 19.03和Kubernetes 1.18上进行了测试.

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

这篇关于如何在Windows上安装Docker套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆