本文介绍了gitlab-ci.yml& docker-in-docker(dind)& curl返回共享运行器上的连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的GitLab CI,在其中我使用docker-compose up旋转一个容器,然后尝试使用curl对其进行访问,最后使用docker-compose down将其拆解. docker-compose up旋转得非常好,我可以使用docker ps -a看到容器,但是当我卷曲时,我会收到连接被拒绝"的信息.

I'm trying to create a simple GitLab CI where I spin up a container using docker-compose up then try to access it using curl and finally tear it down using docker-compose down. docker-compose up spins up perfectly fine and I can see the container up using docker ps -a, however when I curl, I get "connection refused".

这是我的gitlab-ci.yml

here is my 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

Docker Compose:

Docker Compose:

version: '3.4'

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

Docker撰写替代:

Docker compose override:

version: '3.4'

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

推荐答案

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

  1. 在运行curl之前设置sleep 15. 15是您可以正确启动服务的任意时间(以秒为单位).

  1. set sleep 15 before running curl. 15 is an arbitrary period in seconds when your service should start exactly.

接下来,有2个选项:

选项1 :将curl http://localhost:5000/api/values中的localhost替换为docker,例如curl http://docker:5000/api/values

Option 1:Replace localhost in curl http://localhost:5000/api/values with docker like this curl http://docker:5000/api/values

选项2 :

services:
  - name: docker:dind
    alias: localhost

这篇关于gitlab-ci.yml& docker-in-docker(dind)& curl返回共享运行器上的连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:12