GitLab CI:如何连接到以.gitlab-ci.yml脚本启动的Docker容器? [英] GitLab CI: how to connect to the docker container started in .gitlab-ci.yml script?
问题描述
在我的GitLab CI构建中,我想:
In my GitLab CI build, I want to:
- 使用本地AmazonDB启动Docker容器。标准端口布局:泊坞窗中的端口
8000
,端口8000
暴露。当然,一切都在本地运行,我可以连接(curl
,awc-cli
,Amazon DB的Java代码,无论如何 - 将其用于测试,即以
-endpoint-url http:// localhost:8000
的身份连接(或任何其他映射的IP而不是localhost
)。
- Start a docker container with local AmazonDB. Standard port layout: port
8000
in docker, port8000
exposed. Of course, everything works locally, I can connect (curl
,awc-cli
, Java code for Amazon DB, whatever you wish). - Use it for tests, i.e. connect to it as
--endpoint-url http://localhost:8000
(or any other mapped IP instead oflocalhost
).
.gitlab-ci.yml
看起来像这样:
image: docker:stable
build/test:
tags:
- gradle
- eu
stage: test
# doesn't work with or without it
# services:
# - docker:dind
script:
# display local running containers
- echo Displaying all running docker containers with "amazon/dynamodb-local" image...
- docker ps --filter ancestor=amazon/dynamodb-local
# stop all running docker containers with "amazon/dynamodb-local" image
- echo Stopping all Docker containers with "amazon/dynamodb-local" image...
- CONTAINERS=$(docker ps -q --filter ancestor=amazon/dynamodb-local)
- >
if [ "${CONTAINERS}" == "" ]; then
echo No docker containers with "amazon/dynamodb-local" image running. Nothing to stop.
else
docker stop $(docker ps -q --filter ancestor=amazon/dynamodb-local)
echo All Docker containers with "amazon/dynamodb-local" image stopped.
fi
# start DynamoDB local as a docker container with shared database
# - java -Djava.library.path=./dynamodb_local_latest/DynamoDBLocal_lib -jar ./dynamodb_local_latest/DynamoDBLocal.jar -sharedDb
# relative path to causes "Error: Unable to access jarfile" for both windows and linux
# run Docker in detached mode to not hang on the opened console
- cd ./dynamodb_local_latest
- docker run --detach -p 8000:8000 amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb
- cd ./..
# display local running containers
- echo Displaying all running docker containers with "amazon/dynamodb-local" image...
- docker ps --filter ancestor=amazon/dynamodb-local
# see https://stackoverflow.com/questions/45389116/unable-to-access-docker-compose-containers-created-inside-docker
# $DOCKER_HOST is unix:///var/run/docker.sock
# http://localhost:8080 fails
# http://docker:8000 fails
# http://unix:///var/run/docker.sock:8000 fails
- echo docker host is ${DOCKER_HOST}
- cat /etc/hosts
# - curl docker:80 | true
# - curl docker:8000 | true
# - curl http://docker:8000 | true
# - curl http://docker:8000 | true
# - curl ${DOCKER_HOST} | true
# - curl ${DOCKER_HOST}:8000 | true
- curl localhost:8000 | true
- curl http://localhost:8000 | true
# stop all running docker containers with "amazon/dynamodb-local" image
- echo Stopping all Docker containers with "amazon/dynamodb-local" image...
- CONTAINERS=$(docker ps -q --filter ancestor=amazon/dynamodb-local)
- >
if [ "${CONTAINERS}" == "" ]; then
echo No docker containers with "amazon/dynamodb-local" image running. Nothing to stop.
else
docker stop $(docker ps -q --filter ancestor=amazon/dynamodb-local)
echo All Docker containers with "amazon/dynamodb-local" image stopped.
fi
# display local running containers
- echo Displaying all running docker containers with "amazon/dynamodb-local" image...
- docker ps --filter ancestor=amazon/dynamodb-local
关键执行点(Gitlab-CI日志)如下所示:
Critical execution points (Gitlab-CI log) look like this:
Docker容器运行:
Docker container runs:
$ docker run --detach -p 8000:8000 amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb
c823489c22fffa603c1ae1b91d898cb7de4964774d54a08c9fdf0b891c2243b4
$ echo Displaying all running docker containers with "amazon/dynamodb-local" image...
Displaying all running docker containers with amazon/dynamodb-local image...
$ docker ps --filter ancestor=amazon/dynamodb-local
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c823489c22ff amazon/dynamodb-local "java -jar DynamoDBL…" 1 second ago Up Less than a second 0.0.0.0:8000->8000/tcp peaceful_beaver
卷曲失败(尝试所有可能的变体,这只是一个例子):
curl fails (tried all possible variations, this is just an example):
$ curl localhost:8000 | true
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (7) Failed to connect to localhost port 8000: Connection refused
Authenticating with credentials from $DOCKER_AUTH_CONFIG
ERROR: Job failed: exit code 7
我尝试使用和不使用
services:
- docker:dind
尝试使用主机名 localhost
或 docker
或 tcp:// localhost
,其中 http://
前缀或不带前缀,端口为 80
, 8000
或 2375
。
Tried with host names localhost
or docker
or tcp://localhost
, with http://
prefix or without it, with ports 80
, 8000
or 2375
. Nothing works.
$ {DOCKER_HOST}
的值是 unix:/// var /run/docker.sock
:
$ echo docker host is ${DOCKER_HOST}
docker host is unix:///var/run/docker.sock
/ etc / hosts
不包含 docker
$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 runner-H3HL9s9t-project-913-concurrent-0
问题
- 如何解决这个典型的任务?
- 使用
docker run
还是复杂的docker-compose $,可以解决吗? c $ c>需要使用。
- 是否存在指向有效示例的链接?
- 是否有问题,没有<$ c
/ etc / hosts
中的$ c> docker 别名? -
unix:///var/run/docker.sock
的有效值是$ {DOCKER_HOST}
?不应在的 变量
中设置此变量的值(尤其是tcp:// localhost:2375
)? - How to solve this typical task?
- Is it solvable with
docker run
, or complexdocker-compose
usage required. - Is there a link to a working example?
- Is it a problem, that there is no
docker
alias in/etc/hosts
? - Is
unix:///var/run/docker.sock
a valid value of${DOCKER_HOST}
? Shouldn't the value for this variable be set invariables
of.gitlab-ci.yml
(particularly totcp://localhost:2375
)? - https://gitlab.com/gitlab-com/support-forum/issues/748
- < a href = https://gitlab.com/gitlab-org/charts/gitlab/issues/478 rel = nofollow noreferrer> https://gitlab.com/gitlab-org/charts/gitlab/issues/478
- > https://blog.lwolf.org/post/how-to-build-and-test-docker-images-in-gitlab-ci/
- https://gitlab.com/gitlab-org / gitlab-foss / issues / 26566 —类似的问题,用于
docker-compose
- https://gitlab.com/gitlab-org/gitlab-foss/issues/40774 —我完全一样ssue
- Gitlab CI运行程序无法公开嵌套的Docker容器的端口
- 无法访问在docker内部创建的docker-compose容器
- https://gitlab.com/gitlab-com/support-forum/issues/748
- https://gitlab.com/gitlab-org/charts/gitlab/issues/478
- https://blog.lwolf.org/post/how-to-build-and-test-docker-images-in-gitlab-ci/
- https://gitlab.com/gitlab-org/gitlab-foss/issues/26566 — similar issue, for
docker-compose
- https://gitlab.com/gitlab-org/gitlab-foss/issues/40774 — exactly the same issue
- Gitlab CI runner not able to expose ports of nested Docker containers
- Unable to access docker-compose containers created inside docker
Questions
我已经搜索了多个链接。
There are multiple links that I've googled. Solutions from there haven't helped me for now.
推荐答案
实际上不需要使用手动 docker run
/ -script
部分中> docker stop 。苗条和简单的方法是将本地DynamoDB用作服务
。
There is actually no need in writing such a complex solution with manual docker run
/ docker stop
inside the -script
section. The slim and simple way is to use the local DynamoDB as service
.
使用此脚本,本地DynamoDB将可以通过服务
元素的别名
的URL访问,即 dynamodb-local $ c对于此示例$ c>:
With this script, local DynamoDB will be accessible via url from alias
of the services
element, i.e. dynamodb-local
for this example:
services:
- name: amazon/dynamodb-local
alias: dynamodb-local
执行 aws dynamodb
http:// dynamodb-local:8000
端点url起作用:
Executing aws dynamodb
with http://dynamodb-local:8000
endpoint url works:
script:
- DYNAMODB_LOCAL_URL=http://dynamodb-local:8000
- apk add --no-cache curl jq python py-pip
- pip install awscli
- aws dynamodb list-tables --endpoint-url ${DYNAMODB_LOCAL_URL} --region eu-central-1
在此很好的答案中找到了此选项。非常感谢 @madhead 提供了它!
This option was found in this great answer. Many thanks to @madhead for providing it!
这篇关于GitLab CI:如何连接到以.gitlab-ci.yml脚本启动的Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!