Docker 容器在浏览器中可用但在 curl 中不可用

Docker 容器在浏览器中可用但在 curl 中不可用

我有一个正在运行的 gitlab 容器。运行命令:

sudo docker run --detach \
  --hostname gitlab.domain.com \
  --publish 8888:80 --publish 2222:22 \
  --name gitlab \
  --restart always \
  --volume /home/apps/gitlab/config:/etc/gitlab \
  --volume /home/apps/gitlab/logs:/var/log/gitlab \
  --volume /home/apps/gitlab/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest

gitlab 应用程序运行良好,可以通过在本地网络上的浏览器中输入以下命令进行访问http://192.168.1.141:8888.(docker run 中定义的服务器内部IP和端口)

PORTS 部分docker ps -a

443/tcp, 0.0.0.0:2222->22/tcp, 0.0.0.0:8888->80/tcp

在服务器上打开端口:

~$ netstat -l | grep 8888
tcp6     0     0     [::]:8888     [::]:*     LISTEN

我在我的计算机上添加了192.168.1.141 gitlab.domain.com/etc/hosts想在服务器端口 80 上运行的 apache 中设置一个代理。我的想法是仅通过输入 即可查看 gitlab 网站gitlab.domain.com

我没有成功,所以我尝试从主机服务器 curl 容器。以下两种方法都不起作用: curl http://localhost:8888
curl http://192.168.1.141:8888

结果:

~$ curl http://localhost:8888 -v
*   Trying 127.0.0.1:8888...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8888 (#0)
> GET / HTTP/1.1
> Host: localhost:8888
> User-Agent: curl/7.66.0
> Accept: */*
> 

知道为什么会发生这种情况吗?如何从主机服务器连接到 gitlab 页面?

如果我curl 192.168.1.141:8888从我的电脑上执行此操作,它会起作用:

$ curl 192.168.1.141:8888 -v
* Rebuilt URL to: 192.168.1.141:8888/
*   Trying 192.168.1.141...
* TCP_NODELAY set
* Connected to 192.168.1.141 (192.168.1.141) port 8888 (#0)
> GET / HTTP/1.1
> Host: 192.168.1.141:8888
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 302 Found
< Server: nginx
< Date: Sun, 03 Nov 2019 16:27:53 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 105
< Connection: keep-alive
< Cache-Control: no-cache
< Location: http://192.168.1.141:8888/users/sign_in
< Referrer-Policy: strict-origin-when-cross-origin
< Set-Cookie: experimentation_subject_id=ImQyMThkOWI4LWM2MjQtNGM5ZS1hOTZiLTc4YTFhN2U3YjhmNSI%3D--a9d45810de1608210d337df71cbad8c0ac10de16; path=/; expires=Thu, 03 Nov 2039 16:27:53 -0000
< X-Content-Type-Options: nosniff
< X-Download-Options: noopen
< X-Frame-Options: DENY
< X-Permitted-Cross-Domain-Policies: none
< X-Request-Id: LnCSv7rcW6a
< X-Runtime: 0.010182
< X-Ua-Compatible: IE=edge
< X-Xss-Protection: 1; mode=block
< Strict-Transport-Security: max-age=31536000
< Referrer-Policy: strict-origin-when-cross-origin
< 
* Connection #0 to host 192.168.1.141 left intact
<html><body>You are being <a href="http://192.168.1.141:8888/users/sign_in">redirected</a>.</body></html>

答案1

是防火墙问题。关闭防火墙后,curl 工作正常。

相关内容