【问题标题】:mac docker compose, port can not be accessed from localhostmac docker compose,无法从localhost访问端口
【发布时间】:2021-12-20 08:45:39
【问题描述】:

环境:

Docker Mac 桌面:4.2.0

MacOS:11.6.1

预期行为:

我构建了一个 java 服务器映像并使用docker-compose -f docker/docker-compose.yml up 运行一个 java 服务器,并尝试从我的笔记本电脑的 localhost:9090 访问它。我期待curl -i -X GET http://localhost:9090/heanthz 将返回 200。

实际行为:

curl -i -X GET http://localhost:9090/heanthz 将返回 curl: (52) 来自服务器的空回复。

docker-compose.yml 看起来像这样:

version: '3'
services:
  database:
    image: "postgres" # use latest official postgres version
    ports:
      - "5432:5432"
    env_file:
      - dev-postgres.env # configure postgres
    volumes:
      - ./dev-postgres-data:/var/lib/postgresql/data/ # persist data even if container shuts down

  redis:
    image: "redis:alpine"

  
  clickhouse: 
    image: "clickhouse/clickhouse-server"
    ports: 
      - "8123:8123"
      
  server:
    image: "test-server"
    depends_on:
      - database
      - redis
      - clickhouse
    ports:
      - "9090:9090"
    entrypoint: /bin/sh
    command: -c "java -cp test-server.jar app.core"
    env_file:
      - dev-server.env

我发现的一些行为:

  1. 如果我进入 docker 实例 (docker exec -it docker-server-1 sh),并访问 http://localhost:9090/heanthz,它将返回 200

  2. 在我的笔记本电脑上

$lsof -i:9090
COMMAND     PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
com.docke 44140 joe  238u  IPv6 0x48c9c413c17d3d1b      0t0  TCP *:websm (LISTEN)

【问题讨论】:

    标签: docker docker-compose


    【解决方案1】:

    我认为在您的服务器中,您必须使用 0.0.0.0

    ,而不是使用 ip 作为 localhost 或 127.0.0.1

    【讨论】:

    • 我试过http://0.0.0.0:9090/heanthz,还是不行
    • 我的意思是你的项目主机设置不在 http 请求中
    • 成功了,谢谢
    猜你喜欢
    • 2020-09-30
    • 2019-04-04
    • 2017-01-03
    • 1970-01-01
    • 2018-01-17
    • 2021-07-05
    • 2021-04-16
    • 1970-01-01
    • 2019-04-29
    相关资源
    最近更新 更多