1
votes

How to access postgres-docker container other docker container without ip address? I want to store data in postgres by using myweb. in jar given host like 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:
1
I tried -link option , able to get only ENV values , not accessing postgres .phanip
Have you tried adding network_mode: host to your docker compose service? In this way, containers can share local ethernet iface.Alejandro Galera
Yes , i tried with network_mode: host , it also not working ...phanip
my app docker not recognise postgres containter, any other solution..?phanip
Have you added network_mode: host to both services or just one?Alejandro Galera

1 Answers

2
votes

Instead of localhost:5432/db.. use postgresdb:5432/db.. connection string.

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. 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.

Note that this can be directly executed by docker-compose up, it dosen't have any other dependencies. Both the images are from 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"