Skip to content

Latest commit

 

History

History
53 lines (36 loc) · 1.8 KB

fix_communication_bw_conatiners.md

File metadata and controls

53 lines (36 loc) · 1.8 KB

Communication b/w 2 different containers

We are possibly getting error

dial tcp 127.0.0.1:5432: connect: connection refused

possible cause - containers are running in 2 differnt IP' addresses

Container1 Container2
conatiner1 conatiner2

Possible Solutions

  1. Using container IP:
# check n/w config of a conatiner
# IP_OF_POSTGRES_CONSTAINER can be found using below command as shown in above images
docker container inspect <<conatiner_name/id>

# To fix connection error b/w conatiners(This is not ideal as IP could chnage over time)
docker run --name simplebank -p 8080:8080 -e GIN_MODE=release -e DB_SOURCE="postgresql://root:secret@<IP_OF_POSTGRES_CONSTAINER>:5432/simple_bank?sslmode=disable" simplebank:latest
  1. Using bridge network
  • put both conatiners in same n/w, then they ca communicate.
# List all n/w used by docker
docker network ls

# get detail about a specific n/w
docker network inspect <networkName>

# create a new network
docker network create bank-network

# connect a conatiner to a network
docker network connect bank-network postgres12

# run both conatiner in same network
docker run --name simplebank --network bank-network -p 8080:8080 -e GIN_MODE=release  -e DB_SOURCE="postgresql://root:secret@postgres12:5432/simple_bank?sslmode=disable" simplebank:latest
  • since both runnig on same n/w we can connect to postgres using conatiner name instead of IP address both runnig on same n/w

  • continer running on bridge n/w conatiner1