6

我正在尝试创建一个简单的 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"
4

1 回答 1

7

更新您的gitlab-ci.yml文件:

  1. sleep 15运行前设置curl。15 是您的服务应该准确启动的任意时间段(以秒为单位)。

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

选项1:替换localhostcurl http://localhost:5000/api/values这样dockercurl http://docker:5000/api/values

选项 2

services:
  - name: docker:dind
    alias: localhost
于 2019-07-16T08:30:48.007 回答