Can access docker service from curl but not from p

2019-05-07 06:01发布

问题:

I'm doing the docker getting started guide: https://docs.docker.com/get-started/part3/#recap-and-cheat-sheet-optional

docker-compose.yml:

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repo:tag
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:

I deployed my app by running docker stack deploy -c docker-compose.yml getstartedlab. Then Accessing my service from curl which working fine curl -4 http://localhost

<h3>Hello World!</h3><b>Hostname:</b> 1532cae6e06f<br/>....

But I can't access it from chrome or postman by going to http://localhost:80 (it loads forever). Why and how can I fix it?


Update 19/10/17:

I can access my service in the browser from: http://192.168.1.68:80 . It is the address of the leader node (which is the ip of my real machine also..).

But why can't I do it from localhost also?

回答1:

Seeing the curl -4 ... makes me suspect this is an ipv6 issue. If your local machine isn't configured for ipv6 and localhost has a reference to the ipv6 address in the hosts file, then calls to localhost will hang.

The workaround is rather simple, go to 127.0.0.1 instead of localhost in your urls.



回答2:

Try to use bridge network. The bridge network will be share recource network with host. Example if you set in web conatiner with port 80:80, you can access with localhost:80. so add driver: bridge in your docker-compose file, like this

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repo:tag
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:
   driver: bridge


标签: curl docker