Connect to another container using Docker compose

纵饮孤独 提交于 2019-12-22 04:00:46

问题


I would need to use two containers together: one with Tomcat and another with a Database. I have created the following yaml file which describes the services:

postgredb:
  image: postgres
  expose:
    - 5432
  ports:
    - 5432:5432
  environment:
    - POSTGRES_USER=user
    - POSTGRES_PASSWORD=password
tomcat:
  image:  tomcat
  links:
    - postgredb:db
  ports:
    - 8080:8080

Once started docker-compose I can see that I'm not able to reach the Database from Tomcat, unless I retrieve the IP address of the Database (via docker inspect) and use it when configuring Tomcat Connection Pool to the DB. From my understanding, the two containers should be linked and I'd expect to find the database on localhost at port 5432. Otherwise I see little benefits in linking the containers. Is my understanding correct ? Any guidance will be much appreciated!


回答1:


Use the alias "db" that you have defined in file to refer to the database host name.

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.



来源:https://stackoverflow.com/questions/35749385/connect-to-another-container-using-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!