gitlab-ci.yml&docker-in-docker(dind)&curl 在共享运行器上返回连接被拒绝

2024-04-16

我正在尝试创建一个简单的 GitLab CI,在其中使用 docker-compose up 启动一个容器,然后尝试使用curl 访问它,最后使用 docker-compose down 将其拆除。 docker-compose up 旋转得很好,我可以使用 docker ps -a 看到容器,但是当我卷曲时,我得到“连接被拒绝”。

这是我的 gitlab-ci.yml

image: docker

services:
 - docker:dind

before_script:
 - apk add --update python py-pip python-dev && pip install docker-compose
 - apk add --update curl && rm -rf /var/cache/apk/*

stages:
 - test

test:
 stage: test
 script:
  - docker-compose up -d
  - docker ps -a
  - curl http://localhost:5000/api/values
  - docker-compose down

这是跑步者的日志

Image for service testwebapp was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating test-container ... 

Creating test-container ... done
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                    NAMES
3423adfb1f3b        mytestwebapp        "dotnet TestWebApp.d…"   1 second ago        Up Less than a second   0.0.0.0:5000->5000/tcp   test-container
$ curl http://localhost:5000/api/values
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to localhost port 5000: Connection refused
ERROR: Job failed: exit code 7

码头工人组成:

version: '3.4'

services:
  testwebapp:
    image: mytestwebapp
    build:
      context: .
      dockerfile: TestWebApp/Dockerfile
    container_name: test-container

Docker 撰写覆盖:

version: '3.4'

services:
  testwebapp:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://0.0.0.0:5000
    ports:
      - "5000:5000"

更新你的gitlab-ci.yml file:

  1. set sleep 15跑步前curl。 15 是您的服务应准确启动的任意时间段(以秒为单位)。

  2. 接下来,有2个选项:https://docs.gitlab.com/ee/ci/services/#how-services-are-linked-to-the-job https://docs.gitlab.com/ee/ci/services/#how-services-are-linked-to-the-job:

Option 1: 代替localhost in curl http://localhost:5000/api/values with docker像这样curl http://docker:5000/api/values

Option 2:

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

gitlab-ci.yml&docker-in-docker(dind)&curl 在共享运行器上返回连接被拒绝 的相关文章

随机推荐