无法连接到在VSTS中运行的Docker容器

问题描述 投票:0回答:1

我有一个测试,它启动一个Docker容器,执行验证(与Docker容器中的Apache httpd对话),然后停止Docker容器。

当我在本地运行此测试时,此测试运行正常。但是当它在托管的VSTS上运行时,因此它是托管的构建代理,它无法连接到Docker容器中的Apache httpd。

这是.vsts-ci.yml文件:

queue: Hosted Linux Preview

steps:
- script: |
    ./test.sh

这是重现问题的test.sh shell脚本:

#!/bin/bash
set -e
set -o pipefail

function tearDown {
    docker stop test-apache
    docker rm test-apache
}
trap tearDown EXIT

docker run -d --name test-apache -p 8083:80 httpd
sleep 10

curl -D - http://localhost:8083/

当我在本地运行此测试时,我得到的输出是:

$ ./test.sh 
469d50447ebc01775d94e8bed65b8310f4d9c7689ad41b2da8111fd57f27cb38
HTTP/1.1 200 OK
Date: Tue, 04 Sep 2018 12:00:17 GMT
Server: Apache/2.4.34 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html

<html><body><h1>It works!</h1></body></html>
test-apache
test-apache

此输出完全符合我的预期。

但是当我在VSTS上运行这个测试时,我得到的输出是(用替换不相关的部分)。

2018-09-04T12:01:23.7909911Z ##[section]Starting: CmdLine
2018-09-04T12:01:23.8044456Z ==============================================================================
2018-09-04T12:01:23.8061703Z Task         : Command Line
2018-09-04T12:01:23.8077837Z Description  : Run a command line script using cmd.exe on Windows and bash on macOS and Linux.
2018-09-04T12:01:23.8095370Z Version      : 2.136.0
2018-09-04T12:01:23.8111699Z Author       : Microsoft Corporation
2018-09-04T12:01:23.8128664Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613735)
2018-09-04T12:01:23.8146694Z ==============================================================================
2018-09-04T12:01:26.3345330Z Generating script.
2018-09-04T12:01:26.3392080Z Script contents:
2018-09-04T12:01:26.3409635Z ./test.sh
2018-09-04T12:01:26.3574923Z [command]/bin/bash --noprofile --norc /home/vsts/work/_temp/02476800-8a7e-4e22-8715-c3f706e3679f.sh
2018-09-04T12:01:27.7054918Z Unable to find image 'httpd:latest' locally
2018-09-04T12:01:30.5555851Z latest: Pulling from library/httpd
2018-09-04T12:01:31.4312351Z d660b1f15b9b: Pulling fs layer
[…]
2018-09-04T12:01:49.1468474Z e86a7f31d4e7506d34e3b854c2a55646eaa4dcc731edc711af2cc934c44da2f9
2018-09-04T12:02:00.2563446Z   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
2018-09-04T12:02:00.2583211Z                                  Dload  Upload   Total   Spent    Left  Speed
2018-09-04T12:02:00.2595905Z 
2018-09-04T12:02:00.2613320Z   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to localhost port 8083: Connection refused
2018-09-04T12:02:00.7027822Z test-apache
2018-09-04T12:02:00.7642313Z test-apache
2018-09-04T12:02:00.7826541Z ##[error]Bash exited with code '7'.
2018-09-04T12:02:00.7989841Z ##[section]Finishing: CmdLine

关键是这个:

curl: (7) Failed to connect to localhost port 8083: Connection refused

10秒应该足以让apache启动。为什么curl不能在其端口8083上与Apache通信?

P.S:

我知道像这样的硬编码端口是垃圾,我应该使用短暂的端口代替。我想让它首先运行一个硬编码端口,因为这比使用短暂端口更简单,然后在硬编码端口工作时立即切换到短暂端口。如果硬编码端口不起作用,因为端口不可用,则错误应该看起来不同,在这种情况下,docker run应该失败,因为端口无法分配。

更新:

为了确定,我用sleep 100而不是sleep 10重新进行了测试。结果没有变化,curl无法连接到localhost端口8083

更新2:

在扩展脚本以执行docker logs时,docker logs显示Apache正在按预期运行。在扩展脚本以执行docker ps时,它显示以下输出:

2018-09-05T00:02:24.1310783Z CONTAINER ID        IMAGE                                                          COMMAND                  CREATED              STATUS              PORTS                  NAMES
2018-09-05T00:02:24.1336263Z 3f59aa014216        httpd                                                          "httpd-foreground"       About a minute ago   Up About a minute   0.0.0.0:8083->80/tcp   test-apache
2018-09-05T00:02:24.1357782Z 850bda64f847        microsoft/vsts-agent:ubuntu-16.04-docker-17.12.0-ce-standard   "/home/vsts/agents/2…"   2 minutes ago        Up 2 minutes                               musing_booth
docker azure-devops azure-pipelines
1个回答
0
投票

问题是VSTS构建代理程序在Docker容器中运行。当Apache的Docker容器启动时,它运行在与VSTS构建代理Docker容器相同的级别上,而不是嵌套在VSTS构建代理Docker容器中。

有两种可能的解决方案:

  • 用docker主机的ip地址替换localhost,保留端口号8083
  • 用docker容器的ip地址替换localhost,将主机端口号8083更改为容器端口号80

Access via the Docker Host

在这种情况下,解决方案是用docker主机的ip地址替换localhost。以下shell片段可以做到这一点:

host=localhost
if grep '^1:name=systemd:/docker/' /proc/1/cgroup
then
    apt-get update
    apt-get install net-tools
    host=$(route -n | grep '^0.0.0.0' | sed -e 's/^0.0.0.0\s*//' -e 's/ .*//')
fi
curl -D - http://$host:8083/

if grep '^1:name=systemd:/docker/' /proc/1/cgroup检查脚本是否在Docker容器内运行。如果是这样,它会安装net-tools以访问route命令,然后从route命令解析默认gw以获取主机的IP地址。请注意,这仅在容器的网络默认gw实际上是主机时才有效。

Direct Access to the Docker Container

启动docker容器后,可以使用以下命令获取其ip地址:

docker container inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' <container-id>

<container-id>替换为您的容器ID或名称。

所以,在这种情况下,它会(假设第一个IP地址没问题):

ips=($(docker container inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' nuance-apache))
host=${ips[0]}
curl http://$host/
© www.soinside.com 2019 - 2024. All rights reserved.