'Cannot connect django container to postgres on a linux host machine
After going through all of the Stackoverflow threads on this topic, I still could not solve the problem. This happens only on my Fedora laptop, on my Macbook, the docker containers work perfectly fine. My docker-compose.yml file is very standard:
version: "3.8"
services:
app:
container_name: data-management
restart: always
build: ./
ports:
- '3000:3000'
links:
- 'postgresdb'
volumes:
- ./:/usr/local/share/src/app
env_file:
- .dev.env
postgresdb:
container_name: be-postgres
restart: always
image: postgres:12.7-alpine
ports:
- '5432:5432'
volumes:
- postgres-db:/var/lib/postgresql/data
env_file:
- .dev.env
volumes:
postgres-db:
As you can see the environment variables are read in both containers from a file:
POSTGRES_PASSWORD=devpassword123
POSTGRES_USER=devuser
POSTGRES_SERVICE=postgresdb
POSTGRES_PORT=5432
These env variables are also used in the settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.environ['POSTGRES_SERVICE'],
'NAME': os.environ['POSTGRES_USER'],
'PORT': os.environ['POSTGRES_PORT'],
'USER': os.environ['POSTGRES_USER'],
'PASSWORD': os.environ['POSTGRES_PASSWORD'],
}
}
The problem that I am having is that the django application cannot connect to the postgresDB:
django.db.utils.OperationalError: could not connect to server: Host is unreachable Is the server running on host "postgresdb" (172.xx.x.x) and accepting TCP/IP connections on port 5432?
I also checked if the port in the postgres container is opened and it is:
/ # netstat -tuplen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.11:37009 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN -
tcp 0 0 :::5432 :::* LISTEN -
udp 0 0 127.0.0.11:59651 0.0.0.0:* -
Last, but not least, I pinged the postgres container from the django one and it seems like every thing is ok.
This works fine on my macbook machine, therefor, I assume something is wrong on my Fedora laptop. Something else I noticed, which I also find strange is that the django container builds fine, but when I try to install a package from the container I get an error:
/usr/local/share/src/app # apk add gcc
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.13/main: temporary error (try again later)
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.13/main: No such file or directory
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.13/community: temporary error (try again later)
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.13/community: No such file or directory
My Linux, docker as well as networking skills are definitely not enough to figure out, why my Fedora laptop is struggling to run this simple setup.
Solution 1:[1]
It turned out Fedora switched to nftables, which does not go well with Docker. The easiest solution was to switch back to iptables - sudo vim /etc/firewalld/firewalld.conf and change
# FirewallBackend
# Selects the firewall backend implementation.
# Choices are:
# - nftables (default)
# - iptables (iptables, ip6tables, ebtables and ipset)
FirewallBackend=iptables
See the accepted answer here for more information
Solution 2:[2]
Try and build using flag "--network host". Here is the Link
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | filtfilt |
| Solution 2 | senarijit1618 |
