GitLab CI:如何连接到 .gitlab-ci.yml 脚本中启动的 docker 容器?
GitLab CI: how to connect to the docker container started in .gitlab-ci.yml script?
初始任务
在我的 GitLab CI 构建中,我想:
- 使用本地 AmazonDB 启动一个 docker 容器。标准端口布局:端口
8000
在 docker 中,端口 8000
暴露。当然,一切都在本地工作,我可以连接(Amazon DB 的curl
、awc-cli
、Java 代码,任何你想要的)。
- 将其用于测试,即以
--endpoint-url http://localhost:8000
(或任何其他映射 IP 而不是 localhost
)连接到它。
问题
.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
# $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日志)如下所示:
Docker 容器运行:
$ 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 失败(尝试了所有可能的变体,这只是一个例子):
$ 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
。没有任何效果。
${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
用法。
- 是否有 link 工作示例?
/etc/hosts
中没有 docker
别名,这是个问题吗?
unix:///var/run/docker.sock
是 ${DOCKER_HOST}
的有效值吗?不应该在 .gitlab-ci.yml
的 variables
中设置此变量的值(特别是 tcp://localhost:2375
)吗?
链接
我用谷歌搜索了多个 link。那里的解决方案目前对我没有帮助。
- 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 — 类似的问题,对于
docker-compose
- https://gitlab.com/gitlab-org/gitlab-foss/issues/40774 — 完全相同的问题
实际上没有必要在 -script
部分中使用手动 docker run
/ docker stop
编写如此复杂的解决方案。苗条和简单的方法是使用本地 DynamoDB 作为 service
.
使用此脚本,可以通过 url 从 services
元素的 alias
访问本地 DynamoDB,即本例中的 dynamodb-local
:
services:
- name: amazon/dynamodb-local
alias: dynamodb-local
使用 http://dynamodb-local:8000
端点 url 执行 aws dynamodb
有效:
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
此选项在 this great answer. Many thanks to @madhead 中找到并提供!
初始任务
在我的 GitLab CI 构建中,我想:
- 使用本地 AmazonDB 启动一个 docker 容器。标准端口布局:端口
8000
在 docker 中,端口8000
暴露。当然,一切都在本地工作,我可以连接(Amazon DB 的curl
、awc-cli
、Java 代码,任何你想要的)。 - 将其用于测试,即以
--endpoint-url http://localhost:8000
(或任何其他映射 IP 而不是localhost
)连接到它。
问题
.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
# $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日志)如下所示:
Docker 容器运行:
$ 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 失败(尝试了所有可能的变体,这只是一个例子):
$ 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
。没有任何效果。
${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
用法。 - 是否有 link 工作示例?
/etc/hosts
中没有docker
别名,这是个问题吗?unix:///var/run/docker.sock
是${DOCKER_HOST}
的有效值吗?不应该在.gitlab-ci.yml
的variables
中设置此变量的值(特别是tcp://localhost:2375
)吗?
链接
我用谷歌搜索了多个 link。那里的解决方案目前对我没有帮助。
- 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 — 类似的问题,对于
docker-compose
- https://gitlab.com/gitlab-org/gitlab-foss/issues/40774 — 完全相同的问题
实际上没有必要在 -script
部分中使用手动 docker run
/ docker stop
编写如此复杂的解决方案。苗条和简单的方法是使用本地 DynamoDB 作为 service
.
使用此脚本,可以通过 url 从 services
元素的 alias
访问本地 DynamoDB,即本例中的 dynamodb-local
:
services:
- name: amazon/dynamodb-local
alias: dynamodb-local
使用 http://dynamodb-local:8000
端点 url 执行 aws dynamodb
有效:
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
此选项在 this great answer. Many thanks to @madhead 中找到并提供!