gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:02:14 27 4
gpt4 key购买 nike

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

这是我的 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 组合:
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文件:

  • 套装sleep 15运行前 curl . 15 是您的服务应该准确启动的任意时间段(以秒为单位)。
  • 接下来,有两个选项:

  • 选项 1 :
    替换 localhostcurl http://localhost:5000/api/valuesdocker像这样 curl http://docker:5000/api/values
    选项 2 :
    services:
    - name: docker:dind
    alias: localhost

    关于docker - gitlab-ci.yml & docker-in-docker (dind) & curl 在共享运行器上返回连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53570796/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com