Home > Back-end >  mac docker compose, port can not be accessed from localhost
mac docker compose, port can not be accessed from localhost

Time:12-21

Environment:

Docker Mac Desktop: 4.2.0

MacOS: 11.6.1

Expected Behavior:

I build a java server image and use docker-compose -f docker/docker-compose.yml up to run a java server, and try to access it from my laptop's localhost:9090. I am expecting curl -i -X GET http://localhost:9090/heanthz will return 200.

Actual Behavior:

curl -i -X GET http://localhost:9090/heanthz will return curl: (52) Empty reply from server.

docker-compose.yml look like this:

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

Some behavior I found:

  1. if I get into the docker instance (docker exec -it docker-server-1 sh), and access http://localhost:9090/heanthz, it will return 200

  2. on my laptop

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

CodePudding user response:

I think in your server instead of using the ip as localhost or 127.0.0.1 you have to use 0.0.0.0

  • Related