Resources · Security guides · Security guide · port 5432
Bind databases, queues and admin panels to localhost
A database that listens on 0.0.0.0 is an internet service. Bind it to 127.0.0.1, publish Docker ports on loopback, and reach admin panels through an SSH tunnel.
Check command
sudo ss -tlnp | grep -vE '127\.0\.0\.1|\[::1\]'
Copied.
Internet-wide scanners find an open Postgres, Redis or MongoDB port within hours of it appearing. Most of the databases found that way were never meant to be public: the service listened on every interface by default, or Docker published the port and bypassed the firewall. The fix is the same for all of them. The service listens on 127.0.0.1, and anything that needs it from outside goes through an SSH tunnel or a private network.
Find what is listening on a public address
sudo ss -tlnp | grep -vE '127\.0\.0\.1|\[::1\]'
docker ps --format '{{.Names}}\t{{.Ports}}'
Read the local address column. 0.0.0.0:5432 and [::]:5432 mean every IPv4 or IPv6 interface. 127.0.0.1:5432 means only processes on this host. In the Docker output, 0.0.0.0:6379->6379/tcp is published to the world even if UFW denies 6379, because Docker's iptables rules run before UFW's.
Bind native services to loopback
Change the setting, restart the service, then run the ss check again.
| Service | File | Setting |
|---|---|---|
| PostgreSQL | postgresql.conf |
listen_addresses = 'localhost' |
| MySQL / MariaDB | /etc/mysql/mysql.conf.d/mysqld.cnf or 50-server.cnf |
bind-address = 127.0.0.1 |
| Redis | /etc/redis/redis.conf |
bind 127.0.0.1 -::1 and protected-mode yes |
| MongoDB | /etc/mongod.conf |
net.bindIp: 127.0.0.1 |
| Elasticsearch | elasticsearch.yml |
network.host: 127.0.0.1 |
| Memcached | /etc/memcached.conf |
-l 127.0.0.1 and -U 0 |
Loopback is the first control, not the only one. Keep passwords on, and in PostgreSQL keep pg_hba.conf limited to host ... 127.0.0.1/32 scram-sha-256 for TCP.
Publish Docker ports on loopback, or not at all
Containers in the same Compose project reach each other by service name on the project network. They do not need a host port.
services:
app:
build: .
ports:
- "127.0.0.1:8080:8080" # nginx on the host proxies to this
db:
image: postgres:17
# no ports: the app reaches it as db:5432
redis:
image: redis:7
ports:
- "127.0.0.1:6379:6379" # only if a host process needs it
Recreate the containers with docker compose up -d and check docker ps again. Do not put a database on network_mode: host: it ignores the publish address and listens wherever the service is configured to.
Reach admin panels through an SSH tunnel
Adminer, phpMyAdmin, pgAdmin, Grafana and queue dashboards should listen on loopback too. From your laptop:
ssh -N -L 8081:127.0.0.1:8081 [email protected]
Then open http://127.0.0.1:8081 locally. Nothing is exposed, there is no extra login page to brute-force, and the tunnel closes when you stop the command.
Check from outside
From a second machine, every port you closed should time out or refuse:
nc -vz -w 3 your.server 5432
nc -vz -w 3 your.server 6379
Run the check again after a reboot and after docker compose up, because a changed Compose file can publish a port again. If you must keep a published port, filter it in the DOCKER-USER chain as the Docker and UFW guide shows.