在VSTS中测试Docker容器内的应用程序 [英] Testing an application inside docker container in VSTS

查看:104
本文介绍了在VSTS中测试Docker容器内的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试ASP. VSTS中的NET Core 2 dockerized应用程序.它是通过docker-compose在docker容器内设置的.测试通过存储在config中的地址(或从环境变量(如果已设置)中获取)发出请求.

I'm trying to test an ASP. NET Core 2 dockerized application in VSTS. It is set up inside the docker container via docker-compose. The tests make requests via addresses stored in config (or taken from environment variables, if set).

现在,构建的设置如下:

Right now, the build is set up like this:

  1. 运行compose命令以恢复和发布应用.
  2. 运行compose创建和运行docker容器.
  3. 运行bash脚本(如下所述).
  4. 运行测试.

首先,我发现我无法在VSTS中使用 http://localhost:port .在我的本地计算机上它可以正常工作,但是在服务器上不起作用.

First of all, I found out that I can't use http://localhost:port inside VSTS. It works fine on my local machine, but it does not work on the server.

我发现了此文章指出需要使用容器的真实IP来访问它.我已尝试参考

I've found this article that points out the need to use container's real IP to access it. I've tried 2 of the methods described in the referenced question, but none of them worked.

  1. 使用docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id时得到Template parsing error: template: :1:24: executing "" at <.NetworkSettings.Net...>: map has no entry for key "NetworkSettings"(问题出在命令本身)
  2. 当使用docker inspect $(sudo docker ps | grep wiremocktest_microservice.gateway | head -c 12) | grep -e \"IPAddress\"\:[[:space:]]\"[0-2] | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'时,我实际上获得了IP并将其传递给测试,但是随后发生了一些奇怪的事情.即,它们开始超时.我试图在本地复制它,但确实如此.我对此IP提出的每个请求都会超时(可以在浏览器中轻松检查).
  1. When using docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id, I get Template parsing error: template: :1:24: executing "" at <.NetworkSettings.Net...>: map has no entry for key "NetworkSettings" (the problem is with the command itself)
  2. And when using docker inspect $(sudo docker ps | grep wiremocktest_microservice.gateway | head -c 12) | grep -e \"IPAddress\"\:[[:space:]]\"[0-2] | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}', I actually get the IP and can pass it to tests, but then something strange happens. Namely, they start to time out. I tried to replicate this locally, and it does. Every request that I make to this IP times out (easily checked in browser).

访问VSTS中的容器需要使用什么地址,为什么我不能使用localhost?

What address do I need to use to access the containers in VSTS, and why can't I use localhost?

推荐答案

在用于单元测试的容器中运行Azure存储服务时,我遇到了类似的问题(Gradle& Kotlin项目).在本地,一切正常,并且可以使用localhost:10000连接到容器(端口在run命令中发布到主机).但这在VSTS构建管道上不起作用,在尝试与容器的IP连接时也不起作用.

I've run into similar problem with having a Azure Storage service running in a container for unit tests (Gradle & Kotlin project). Locally everything's working and it's possible to connect to the container by using localhost:10000 (the port is published to the host machine in run command). But this doesn't work on VSTS build pipeline and neither does when trying to connect with the IP of the container.

我找到了至少在这种情况下有效的解决方案:我创建了一个自定义容器网络,并将Azure存储容器和VSTS代理容器连接到该网络.之后,可以通过使用容器名称和内部端口号从测试连接到我的自定义容器,例如我的存储容器:10000.

I've found a solution that works at least in this case: I created a custom container network and connected my Azure Storage container and the VSTS agent container to that network. After that it's possible to connect to my custom container from the tests by using the container name and internal port number e.g. my-storage-container:10000.

因此,我创建了一个脚本,该脚本创建了容器网络,在该网络中启动了我的容器,然后通过从进程列表中复制容器ID来连接VSTS代理.是这样的:

So I created a script that creates the container network, starts my container in that network and then connects also the VSTS agent by grepping the container ID from process list. Its' something like this:

docker network create my-custom-network
docker run --net=my-custom-network -d --name azure-storage-container -t -p 10000:10000 -v ${SCRIPT_DIR}/azurite:/opt/azurite/folder arafato/azurite
CONTAINER_ID=`docker ps -a | awk '{ print $1,$2 }' | grep microsoft/vsts-agent | awk '{print $1 }'`
docker network connect my-custom-network ${CONTAINER_ID}

之后,我的测试可以使用 http://azure-storage-container:10000连接到Azure存储容器没有问题.

After that my tests can connect to the Azure storage container with http://azure-storage-container:10000 with no problems.

这篇关于在VSTS中测试Docker容器内的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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