GitLab CI:如何连接到 .gitlab-ci.yml 脚本中启动的 docker 容器?

2024-03-12

初始任务

在我的 GitLab CI 构建中,我想要:

  • 使用本地 AmazonDB 启动 Docker 容器。标准端口布局:端口8000在码头工人,港口8000裸露。当然,一切都在本地运行,我可以连接(curl, awc-cli、Amazon DB 的 Java 代码,无论您想要什么)。
  • 使用它进行测试,即连接到它--endpoint-url http://localhost:8000(或任何其他映射的 IP,而不是localhost).

Problem

.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 日志)如下所示:

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 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 or docker or tcp://localhost, with http://前缀或不带前缀,带端口80, 8000 or 2375。什么都不起作用。

的价值${DOCKER_HOST} is 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需要使用。
  • 有工作示例的链接吗?
  • 有问题吗,没有docker别名在/etc/hosts?
  • Is unix:///var/run/docker.sock有效值为${DOCKER_HOST}?这个变量的值不应该设置在variables of .gitlab-ci.yml(特别是对于tcp://localhost:2375)?

Links

我用谷歌搜索了多个链接。那里的解决方案目前对我没有帮助。

  • https://gitlab.com/gitlab-com/support-forum/issues/748 https://gitlab.com/gitlab-com/support-forum/issues/748
  • https://gitlab.com/gitlab-org/charts/gitlab/issues/478 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://blog.lwolf.org/post/how-to-build-and-test-docker-images-in-gitlab-ci/
  • https://gitlab.com/gitlab-org/gitlab-foss/issues/26566 https://gitlab.com/gitlab-org/gitlab-foss/issues/26566- 类似的问题,对于docker-compose
  • https://gitlab.com/gitlab-org/gitlab-foss/issues/40774 https://gitlab.com/gitlab-org/gitlab-foss/issues/40774— 完全相同的问题
  • Gitlab CI 运行程序无法公开嵌套 Docker 容器的端口 https://stackoverflow.com/questions/41559660/gitlab-ci-runner-not-able-to-expose-ports-of-nested-docker-containers/
  • 无法访问在 docker 内创建的 docker-compose 容器 https://stackoverflow.com/questions/45389116/unable-to-access-docker-compose-containers-created-inside-docker/

实际上没有必要手动编写这么复杂的解决方案docker run / docker stop在 - 的里面-script部分。最简单的方法是使用本地 DynamoDB 作为service.

使用此脚本,可以通过 url 访问本地 DynamoDBalias of the services元素,即dynamodb-local对于这个例子:

  services:
    - name: amazon/dynamodb-local
      alias: dynamodb-local

执行中aws dynamodb with http://dynamodb-local:8000端点 url 有效:

  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 https://stackoverflow.com/a/53583725/8534088很好的答案。非常感谢@madhead https://stackoverflow.com/users/750510/madhead因为提供它!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

GitLab CI:如何连接到 .gitlab-ci.yml 脚本中启动的 docker 容器? 的相关文章

随机推荐