如何看到一个链接的容器为本地主机?

我有这两个容器:

api: image: social-learning ports: - "3000:3000" command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - ../api:/app expose: - "3000" web: image: social-learning-frontend ports: - "4200:4200" - "9000:9000" command: ember serve -p 4200 volumes: - .:/app links: - api expose: - "3000" 

当我做 :

 docker exec `docker ps -a | grep 'frontend_web_1' | awk '{print $1 }'` curl http://localhost:3000 

我总是有一个连接拒绝。 我可以通过IP访问另一个容器,但我更喜欢使用它,因为它是本地的。

可能吗 ?

这里似乎有些混乱。

它看起来像你正在尝试login到Web容器,并访问使用本地主机地址的API容器? 这是不行的。

最好的解决scheme就是使用你所声明的链接。 在Web容器中,你应该可以执行curl http://api:3000 。 这是可行的,因为Docker已经为你添加了api容器的/etc/hosts条目。

通过localhost访问只能在发布容器端口的Docker主机上工作。 我想你可以通过Docker桥(如curl 172.17.42.1:3000 )从容器内部访问Docker主机,但是这似乎毫无意义地复杂和脆弱。