2

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:
7
  • I tried -link option , able to get only ENV values , not accessing postgres . Commented Jun 19, 2018 at 11:49
  • Have you tried adding network_mode: host to your docker compose service? In this way, containers can share local ethernet iface. Commented Jun 19, 2018 at 11:56
  • Yes , i tried with network_mode: host , it also not working ... Commented Jun 19, 2018 at 13:03
  • my app docker not recognise postgres containter, any other solution..? Commented Jun 19, 2018 at 13:11
  • Have you added network_mode: host to both services or just one? Commented Jun 19, 2018 at 13:13

2 Answers 2

3

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"      
Sign up to request clarification or add additional context in comments.

8 Comments

I tried postgresdb:5432/db.. it also not working, my app container not recognise postgres container.
@phanip What is the output of docker exec app ping postgresdb ? Did you try restarting the whole docker daemon?
Its pinging with each other , postgredb not accessing in app docker ,even i enabled localhost=* and ,0.0.0.0 md5 respective files
@phanip When ping is ok then there is no problem with docker setup. Your problem seems to be in the config of your database. The docker network is ok...
@phanip I added a minimal working example to my answer. Hope it helps you.
|
0

That is the Docker Networking problem. 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. I think that my solution will help you so.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.