使用 Docker compose 连接到另一个容器
Connect to another container using Docker compose
我需要同时使用两个容器:一个与 Tomcat 一起使用,另一个与数据库一起使用。
我创建了以下描述服务的 yaml 文件:
postgredb:
image: postgres
expose:
- 5432
ports:
- 5432:5432
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
tomcat:
image: tomcat
links:
- postgredb:db
ports:
- 8080:8080
启动 docker-compose 后,我可以看到我无法从 Tomcat 访问数据库,除非我检索数据库的 IP 地址(通过 docker检查)并在配置 Tomcat 数据库连接池时使用它。
根据我的理解,这两个容器应该链接起来,我希望在本地主机上的端口 5432 上找到数据库,否则我看不到链接容器的好处。
我的理解正确吗?
使用您在文件中定义的别名"db" 来引用数据库主机名。
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://docs.docker.com/compose/compose-file/compose-file-v2/#links
我需要同时使用两个容器:一个与 Tomcat 一起使用,另一个与数据库一起使用。
我创建了以下描述服务的 yaml 文件:
postgredb:
image: postgres
expose:
- 5432
ports:
- 5432:5432
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
tomcat:
image: tomcat
links:
- postgredb:db
ports:
- 8080:8080
启动 docker-compose 后,我可以看到我无法从 Tomcat 访问数据库,除非我检索数据库的 IP 地址(通过 docker检查)并在配置 Tomcat 数据库连接池时使用它。
根据我的理解,这两个容器应该链接起来,我希望在本地主机上的端口 5432 上找到数据库,否则我看不到链接容器的好处。
我的理解正确吗?
使用您在文件中定义的别名"db" 来引用数据库主机名。
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://docs.docker.com/compose/compose-file/compose-file-v2/#links