A local development app sometimes needs data from a database running in a remote Docker Compose stack. Opening the database port to the internet is usually unnecessary: publish it on the server’s loopback interface, then carry your database connection through an SSH tunnel.
The detail that often trips people up is that the tunnel’s destination is resolved from the SSH server’s side, not from your laptop. In this setup, 127.0.0.1 appears on both ends, but refers to two different machines.
1. Publish the database on the server’s loopback only
In the Compose file on the remote server, map the database container port to a host port bound specifically to loopback. For PostgreSQL:
services:
db:
image: postgres:16
environment:
POSTGRES_DB: app
POSTGRES_USER: app
POSTGRES_PASSWORD: change-this
ports:
- "127.0.0.1:5432:5432"
The format is HOST_IP:HOST_PORT:CONTAINER_PORT. Here, Docker publishes container port 5432 on server port 5432, but only on the server’s 127.0.0.1 address. A remote machine cannot connect directly to that listener through the server’s public interface.
This differs from "5432:5432", which normally publishes on all host interfaces. Check the actual mapping after applying your Compose changes:
docker compose up -d db
docker compose ps
On the server, you can also inspect listening sockets:
ss -lnt | grep ':5432'
You want to see a listener associated with 127.0.0.1:5432, not 0.0.0.0:5432 or [::]:5432. Docker’s implementation and firewall rules can affect how published ports appear in system tools, so treat this as a useful check rather than the only proof of reachability. For background on tunnel types and forwarding behavior, see SSH tunneling explained.
If the database is already running, confirm that the Compose configuration actually changed and that the container was recreated as needed. Also make sure the database process listens on the container interface Docker uses; the usual official PostgreSQL image does this by default.
2. Create the local forward
On your development machine, run:
ssh -N -L 15432:127.0.0.1:5432 dev@staging
This asks SSH to listen on local port 15432. Connections arriving there are carried through the SSH connection and sent to 127.0.0.1:5432 as seen by the SSH server—in this example, the staging server. Docker’s loopback-bound host port receives the connection and forwards it to the database container.
The two addresses have different roles:
- The first port,
15432, is on your local machine. - The destination
127.0.0.1:5432is reached from the remote SSH server. -
dev@stagingidentifies the SSH login and server; it is not the database address.
-N tells SSH not to run a remote command. Leave this process running while your app needs the tunnel. If you prefer a config alias, use the usual OpenSSH Host settings for the SSH connection; the forwarding destination still refers to the server side. The SSH config examples cover host aliases and options.
By default, OpenSSH’s local forward normally binds to loopback on your own machine. To make that intent explicit, use:
ssh -N -L 127.0.0.1:15432:127.0.0.1:5432 dev@staging
Avoid changing the local bind address to 0.0.0.0 unless you deliberately want other devices to use your tunnel. That would make the forwarded port available beyond your local machine, subject to network and firewall conditions.
3. Point the local app at the local end
Configure your local app as if the database were on your development machine:
host: 127.0.0.1
port: 15432
database: app
username: app
password: change-this
For a PostgreSQL URL, that might be:
postgresql://app:change-this@127.0.0.1:15432/app
Use the database credentials configured for the remote database, not your SSH password or key. Keep secrets out of committed files; use your project’s local environment-file or secret-management convention.
4. Check each leg if the connection fails
First, confirm SSH can reach the server. If SSH itself is refused, diagnose the SSH listener and network path separately; these SSH connection-refused checks are a useful starting point.
Then check the tunnel from a second local terminal while the SSH command remains running:
nc -vz 127.0.0.1 15432
A successful TCP check shows that something accepts connections on the local forwarded port; it does not prove that database authentication or the requested database name is correct. Test with a database client if available, for example:
psql -h 127.0.0.1 -p 15432 -U app -d app
If the local connection is refused, confirm the SSH process is still running and that port 15432 is free. If SSH reports that it cannot bind the local port, choose another unused local port and update the app configuration. If the tunnel accepts TCP connections but the database client cannot connect, check the remote Compose service, container logs, credentials, database name, and the server-side destination port.
Important caveats
The tunnel protects traffic between your development machine and the SSH server. It does not replace database authentication, and it does not necessarily encrypt traffic between the server and the database container. Use strong database credentials and limit who can log in to the server. If your database requires TLS, configure the client accordingly; certificate hostname verification may need care because the client connects to 127.0.0.1 rather than the database’s normal hostname.
Finally, this pattern assumes the SSH server and Docker host are the same machine. If SSH terminates on a separate bastion, the destination 127.0.0.1 means the bastion, not the Docker host. In that case, target an address and port reachable from the bastion, or establish a tunnel to the Docker host through the bastion.
I'm building ShadeTerm — an SSH client that organizes terminals, SFTP, code editing, and server tools into workspaces.
Top comments (0)