简体   繁体   English

Windows10 主页上的 Docker - Docker 容器内部连接到 Docker 引擎

[英]Docker on Windows10 home - inside docker container connect to the docker engine

When creating a Jenkins Docker container, it is very useful to able to connect to the Docker daemon.在创建 Jenkins Docker 容器时,能够连接到 Docker 守护进程非常有用。 In that way, I can start docker commands inside the Jenkins container.这样,我就可以在 Jenkins 容器内启动 docker 命令。

For example, after starting the Jenkins Docker container, I would like to 'docker exec -it container-id bash' and start 'docker ps'.例如,在启动 Jenkins Docker 容器后,我想“docker exec -it container-id bash”并启动“docker ps”。

On Linux you can use bind-mounts on /var/run/docker.sock.在 Linux 上,您可以在 /var/run/docker.sock 上使用绑定挂载。 On Windows this seems not possible.在 Windows 上,这似乎是不可能的。 The solution is by using 'named pipes'.解决方案是使用“命名管道”。 So, in my docker-compose.yml file I tried to create a named pipe.因此,在我的 docker-compose.yml 文件中,我尝试创建一个命名管道。

version: '2'
services:
  jenkins:
    image: jenkins-docker
    build:
      context: ./
      dockerfile: Dockerfile_docker
    ports:
      - "8080:8080"
      - "50000:50000"
    networks:
      - jenkins
    volumes:
      - jenkins_home:/var/jenkins_home
      - \\.\pipe\docker_engine:\\.\pipe\docker_engine
      # - /var/run/docker.sock:/var/run/docker.sock
      # - /path/to/postgresql/data:/var/run/postgresql/data
      # - etc.

Starting docker-compose with this file, I get the following error:使用此文件启动 docker-compose 时,出现以下错误:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock.无法连接到 unix:///var/run/docker.sock 上的 Docker 守护进程。 Is the docker daemon running? docker 守护进程是否正在运行?

How can I setup the docker-compose file so that I can use the docker.sock (or Docker) inside the started container?如何设置 docker-compose 文件,以便我可以在启动的容器内使用 docker.sock(或 Docker)?

On Linux you can use something like volumes: /var/run/docker.sock:/var/run/docker.sock.在 Linux 上,您可以使用类似卷的内容:/var/run/docker.sock:/var/run/docker.sock。 This does not work in a Windows environment.这在 Windows 环境中不起作用。 When you add this folder (/var) to Oracle VM Virtualbox, it won't get any IP forever.当您将此文件夹 (/var) 添加到 Oracle VM Virtualbox 时,它将永远不会获得任何 IP。 And on many posts并且在很多帖子上

You can expose the daemon on tcp://localhost:2375 without TLS in the settings.您可以在tcp://localhost:2375上公开守护程序,而无需在设置中使用 TLS。 This way you can configure Jenkins to use the Docker API instead of the socket.通过这种方式,您可以将 Jenkins 配置为使用 Docker API 而不是套接字。 I encourage you to read this article by Nick Janetakis about "Understanding how the Docker Daemon and the Docker CLI work together" .我鼓励您阅读Nick Janetakis 撰写的关于“了解 Docker 守护程序和 Docker CLI 如何协同工作”的文章

And then there are several Docker plugins for Jenkins that allows this connection:然后有几个 Jenkins 的 Docker 插件允许这种连接:

在此处输入图片说明

Also, you can find additional information in the Docker plugin documentation on wiki.jenkins.io :此外,您可以在 wiki.jenkins.io 上Docker 插件文档中找到其他信息:

def dockerCloudParameters = [
  connectTimeout:   3,
  containerCapStr:  '4',
  credentialsId:    '',
  dockerHostname:   '',
  name:             'docker.local',
  readTimeout:      60,
  serverUrl:        'unix:///var/run/docker.sock', // <-- Replace here by the tcp address
  version:          ''
]

EDIT 1:编辑 1:

I don't know if it is useful, but the Docker Daemon on Windows is located to C:\\ProgramData\\docker according to the Docker Daemon configuration doc .我不知道它是否有用,但是根据Docker Daemon configuration doc ,Windows 上的 Docker Daemon 位于C:\\ProgramData\\docker

EDIT 2:编辑2:

You need to say explicitly the container to use the host network because you want to expose both Jenkins and Docker API.您需要明确说明使用host网络的容器,因为您想同时公开 Jenkins 和 Docker API。 Following this documentation , you only have to add --network=host (or network_mode: 'host' in docker-compose) to your container/service.按照本文档,您只需将--network=host (或--network=host -compose 中的network_mode: 'host' )添加到您的容器/服务中。 For further information, you can read this article to understand what is the purpose of this network mode.有关更多信息,您可以阅读本文以了解这种网络模式的目的是什么。

First try was to start a Docker environment using "Docker Quickstart terminal".第一次尝试是使用“Docker Quickstart 终端”启动 Docker 环境。 This is a good solution when running Docker commands within that environment .在该环境中运行 Docker 命令时,这是一个很好的解决方案。

When installing a complete CI/CD Jenkins environment via Docker means that WITHIN the Jenkins Docker container you need to access the Docker daemon.当通过 Docker 安装完整的 CI/CD Jenkins 环境时,意味着在 Jenkins Docker 容器内您需要访问 Docker 守护进程。 After trying many solutions, reading many posts, this did not work.在尝试了许多解决方案,阅读了许多帖子后,这不起作用。 @Paul Rey, thank you very much for trying all kinds of routes. @Paul Rey,非常感谢您尝试各种路线。

A good solution is to get an Ubuntu Virtual Machine and install it via the Oracle VM Virtualbox.一个好的解决方案是获取一个 Ubuntu 虚拟机并通过 Oracle VM Virtualbox 安装它。 It is then VERY IMPORTANT to install Docker via this official description .然后通过此官方说明安装 Docker非常重要

Before installing Docker, of course you need to install Curl, Git, etc.在安装 Docker 之前,当然需要先安装 Curl、Git 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM