We are possibly getting error
dial tcp 127.0.0.1:5432: connect: connection refused
Container1 | Container2 |
---|---|
- 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
- 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