为什么我无法在 GitLab.com CI 中连接到本地主机?

Any reason why I can't connect to localhost in GitLab.com CI?

我想测试一些 HTTP REST API,为此我需要让我的工作能够监听并向自己发出请求。

为了模拟这样的场景,我做了一个简单的管道,挂载一个netcat服务器,然后尝试连接。

test_backend_job:
  image: debian:latest
  script:
    - apt-get update && apt-get --assume-yes install netcat
    - nohup nc -l localhost 5000 &
    - sleep 5
    - nc -v localhost 5000

如果我在我的 PC 上尝试此操作,则会成功建立连接。但是在 GitLab 中,我在我选择的任何端口上都被拒绝连接。

(...)
Preparing to unpack .../netcat-traditional_1.10-41.1_amd64.deb ...
Unpacking netcat-traditional (1.10-41.1) ...
Selecting previously unselected package netcat.
Preparing to unpack .../netcat_1.10-41.1_all.deb ...
Unpacking netcat (1.10-41.1) ...
Setting up netcat-traditional (1.10-41.1) ...
update-alternatives: using /bin/nc.traditional to provide /bin/nc (nc) in auto mode
Setting up netcat (1.10-41.1) ...
$ nohup nc -l localhost 5000 &
$ sleep 5
$ nc -v localhost 5000
localhost [127.0.0.1] 5000 (?) : Connection refused
ERROR: Job failed: exit code 1

我错过了什么?这应该非常简单。

注:我是运行这个在他们"cloud".

您是否尝试过使用 netcat 映像作为服务然后 运行 脚本?

https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#how-services-are-linked-to-the-job