Connect to another container using Docker compose

2019-04-27 08:10发布

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条回答
欢心
2楼-- · 2019-04-27 08:48

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.

查看更多
登录 后发表回答