简体   繁体   English

如何访问没有ip地址的postgres-docker容器other docker容器

[英]How to access postgres-docker container other docker container without ip address

How to access postgres-docker container other docker container without ip address?如何在没有ip地址的情况下访问其他docker容器的postgres-docker容器? I want to store data in postgres by using myweb.我想使用 myweb 将数据存储在 postgres 中。 in jar given host like localhost:5432/db..在 jar 中给定主机,如 localhost:5432/db..

Here my compose file:这是我的撰写文件:

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:

Instead of localhost:5432/db.. use postgresdb:5432/db.. connection string. 代替localhost:5432/db..使用postgresdb:5432/db..连接字符串。

By default the container has the same hostname as the service name. 默认情况下,容器的主机名与服务名称相同。


Here is my minimal working example, which is connecting a java client (boxfuse/flyway) with postgres server. 这是我的最小工作示例,该示例将Java客户端(boxfuse / flyway)与postgres服务器连接。 The most important part is the heath check , which is delaying the start of the myweb container to the time when postgres is ready to accept connections. 最重要的部分是健康检查 ,这myweb容器的启动延迟到postgres准备接受连接的时间。

Note that this can be directly executed by docker-compose up , it dosen't have any other dependencies. 请注意, 这可以docker-compose up 直接执行 ,它没有任何其他依赖关系。 Both the images are from docker hub. 这两个图像均来自docker hub。

version: '2.1'

services:
   myweb:
    image: boxfuse/flyway
    command: -url=jdbc:postgresql://postgresdb/postgres -user=postgres -password=123 info
    depends_on:
      postgresdb:
        condition: service_healthy

   postgresdb:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=123
    healthcheck:
      test: "pg_isready -q -U postgres"      

That is the Docker Networking problem.那就是Docker Networking问题。 The solution is to use postgresdb:5432/db in place of localhost:5432/db because the two service is in the same.network named back-tier and docker deamon will use name service like a DNS name to make communication between the two container.解决方案是使用postgresdb:5432/db代替localhost:5432/db ,因为这两个服务在同一个网络中。名为 back-tier 的网络和 docker 守护进程将使用类似 DNS 名称的名称服务在两个容器之间进行通信. I think that my solution will help you so.我认为我的解决方案会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM