如何访问postgres-docker容器其他没有ip地址的docker容器?我想使用myweb在postgres中存储数据 . 在 jar 里给主机像localhost:5432 / db ..
这是我的撰写文件:
version: "3"
services:
myweb:
build: ./myweb
container_name: app
ports:
- "8080:8080"
- "9090:9090"
networks:
- front-tier
- back-tier
depends_on:
- "postgresdb"
postgresdb:
build: ./mydb
image: ppk:postgres9.5
volumes:
- dbdata:/var/lib/postgresql
ports:
- "5432:5432"
networks:
- back-tier
volumes:
dbdata: {}
networks:
front-tier:
back-tier:
1 回答
而不是
localhost:5432/db..
使用postgresdb:5432/db..
连接字符串 .默认情况下,容器具有与服务名称相同的主机名 .
这是我的最小工作示例,它将java客户端(boxfuse / flyway)与postgres服务器连接起来 . 最重要的部分是 heath check ,它将
myweb
容器的开头延迟到postgres准备接受连接的时间 .注意 this can be directly executed 由
docker-compose up
,它没有任何其他依赖项 . 这两张图片都来自码头中心 .