I have a simple compose file at ~/tmp/
:
version: '3.9'
networks:
network1:
name: pg-net
attachable: false
ipam:
driver: default
config:
- subnet: 172.20.0.0/16
ip_range: 172.20.2.0/24
gateway: 172.20.0.1
services:
postgres:
image: postgres:14.1-bullseye
hostname: postgres
environment:
POSTGRES_PASSWORD: ****
ports:
- "5433:5432"
#networks: # for the moment, keep the default network that will be build during the start up of the container
# - network1
when connecting to my container and trying to connect to a dummy database on the host machine, the psql
command times out:
$ dc down -v && dc up -d && dc exec postgres bash
root@postgres:/# psql -d postgres://postgres@172.17.0.1:5432/dummydb
psql: error: connection to server at "172.17.0.1", port 5432 failed: Connection timed out
Is the server running on that host and accepting TCP/IP connections?
but if I uncomment the two last lines of the compose file, it works:
root@postgres:/# psql -d postgres://postgres@172.20.0.1:5432/dummydb
Password for user postgres: *********************
psql (14.0 (Debian 14.0-1.pgdg110+1), server 12.4 (Ubuntu 12.4-1.pgdg18.04+1))
Type "help" for help.
dummydb=#
but the strangest is that if I use the standard docker0
IP address, which is the default docker Gateway 172.17.0.1
it also works (Iām really surprised there !!):
root@postgres:/# psql -d postgres://postgres@172.17.0.1:5432/dummydb
Password for user postgres: *********************
psql (14.0 (Debian 14.0-1.pgdg110+1), server 12.4 (Ubuntu 12.4-1.pgdg18.04+1))
Type "help" for help.
dummydb=#
Here is my custom network:
$ docker network inspect -f '{{json .}}' pg-net | jq .
{
"Name": "pg-net",
"Id": "4ad4a93eef77fff6a2e3972b3dde4b2a990cf96dda0df00a11fac62de86c1507",
"Created": "2022-01-15T21:39:00.681972675+01:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.20.0.0/16",
"IPRange": "172.20.2.0/24",
"Gateway": "172.20.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"302408154a2b84b75eda328e76c6de3676aq4c042038aa83fb7e3aa6fc640e10": {
"Name": "tmp_postgres_1",
"EndpointID": "f05c7d0faf9e415d8adde05de953d41beea5dd12605ebcf42d49f973bb03f71a",
"MacAddress": "02:42:ac:ad:44:00",
"IPv4Address": "172.20.2.0/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "pg-net",
"com.docker.compose.project": "tmp",
"com.docker.compose.version": "1.27.4"
}
}
(( please also note that even Iāve set attachable: false
is has been switched to true
, I donāt know whyā¦ ))
and hereās the network when the two last lines of the compose file were actually commented, so when the network was auto-magically build when spinning up the container:
$ docker network inspect -f '{{json .}}' tmp_default | jq .
{
"Name": "tmp_default",
"Id": "04b249a2367007baba5187130587823d7e027f49d7424a8423e7a6f69e754601",
"Created": "2022-01-15T21:45:28.507326828+01:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "192.168.62.0/20",
"Gateway": "192.168.62.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"1157fca611cb189e85f0d878a6a6bdd042d5d625e7486584feda2df77de10229": {
"Name": "tmp_postgres_1",
"EndpointID": "6f87632562208aca2d3193156faee985a3d06a1ca829af6e56f814a8a66b0382",
"MacAddress": "02:42:c0:a9:23:02",
"IPv4Address": "192.168.62.2/20",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "default",
"com.docker.compose.project": "tmp",
"com.docker.compose.version": "1.27.4"
}
}
Iām wondering why does the connection using 172.17.0.1
suddenly (!!) work when configuring manually a totally different network?!
Info:
$ docker-compose --version
docker-compose version 1.27.4, build 40524192
$ docker --version
Docker version 20.10.12, build e91ed57
$ uname -mor
5.4.0-91-generic x86_64 GNU/Linux
OS: Ubuntu 18.04.6 LTS