GItLab CI使curl:(7)无法连接到本地主机端口8090:连接被拒绝

时间:2019-07-16 14:39:58

标签: shell docker curl gitlab gitlab-ci

问题是我遇到了curl: (7) Failed to connect to localhost port 8090: Connection refused GItLab CI错误,但是在我获取网页源HTML的笔记本电脑上却没有发生。下面的.gitlab-ci.yml是问题的简单再现。我花了很多时间试图弄清楚这一点-我敢肯定其他人也有。

GitLab Docker Runner Verbose

此外:这不是一个类似的问题-因为他们没有提供解决方案。

GitLab存储库:https://gitlab.com/mudassir-ahmed/wordpress-testing-with-gitlab-ci/tree/another-approach,但其中包含的唯一文件是下面显示的.gitlab-ci.yml ...

image: docker:stable

variables:
  # When using dind service we need to instruct docker, to talk with the
  # daemon started inside of the service. The daemon is available with
  # a network connection instead of the default /var/run/docker.sock socket.
  #
  # The 'docker' hostname is the alias of the service container as described at
  # https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services
  #
  # Note that if you're using the Kubernetes executor, the variable should be set to
  # tcp://localhost:2375/ because of how the Kubernetes executor connects services
  # to the job container
  # DOCKER_HOST: tcp://localhost:2375/
  #
  # For non-Kubernetes executors, we use tcp://docker:2375/
  DOCKER_HOST: tcp://docker:2375/
  # When using dind, it's wise to use the overlayfs driver for
  # improved performance.
  DOCKER_DRIVER: overlay2

services:
  - docker:dind

before_script:
  - docker info

build:
  stage: build
  script:
    - apk update
    - apk add curl
    #- hostname -i
    - docker container ls
    - docker run -d -p 8090:80 --name nginx-server kitematic/hello-world-nginx
    - curl localhost:8090 # This works on my laptop but not on a GitLab runner.

2 个答案:

答案 0 :(得分:0)

 docker run -d -p 8090:80 --name nginx-server kitematic/hello-world-nginx
 curl localhost:8090 # This works on my laptop but not on a GitLab runner.

假设这是您的代码,我认为您应该以某种方式在docker run和curl之间添加一些超时。

前段时间,我在gitlab跑步机上启动docker容器后遇到类似问题,但我无法访问我的网址。当我添加了检查容器是否正在运行“大约一分钟”的命令时,它解决了我的问题。

“ docker inspect -f {{.State.Running}}” + containerName“,但是要执行此检查,您应该添加一些其他脚本

答案 1 :(得分:0)

请参阅此处的答案:gitlab-ci.yml & docker-in-docker (dind) & curl returns connection refused on shared runner

有两种方法可以解决此问题:

选项1:将localhost中的curl localhost:8090替换为curl docker:8090的docker

选项2:

services:
  - name: docker:dind
    alias: localhost