Access Loopback-Bound Docker Admin Services via SSH Tunneling

Goal

Make a containerized admin web interface (such as pgAdmin4) in Docker Compose listen only on the host's loopback interface (127.0.0.1), pin its image, and reach it from an administrator workstation through an encrypted SSH tunnel.

Prerequisites

  • A remote Linux server running Docker Engine 28.3.3 or later (earlier 28.x releases could expose loopback-published ports after a firewalld reload: CVE-2025-54388) and Docker Compose.
  • SSH key-based access to the server (user@your-server) with TCP forwarding allowed in sshd_config (AllowTcpForwarding yes, the OpenSSH default).

Steps

1. Find the Image Version the Server Runs

Pin the version that is already running, so the persistent volume isn't upgraded or downgraded by the change. On the server:

docker inspect --format '{{index .RepoDigests 0}}' \
  "$(docker compose -f docker-compose-prod.yml images -q pgadmin4)"

This prints elestio/pgadmin@sha256:<digest>. Find the release tag with that digest on Docker Hub, or check pgAdmin's Help → About. To get the digest of a given tag instead:

docker buildx imagetools inspect elestio/pgadmin:REL-9_18

2. Bind the Port to 127.0.0.1 and Pin the Image

In docker-compose-prod.yml, prefix the host port with 127.0.0.1: and pin the tag and digest (see Container Image Version Pinning vs Mutable Tags):

services:
  pgadmin4:
    image: elestio/pgadmin:REL-9_18@sha256:<digest>
    restart: unless-stopped
    env_file:
      - ./.env
    environment:
      PGADMIN_LISTEN_PORT: 80
      GUNICORN_TIMEOUT: 300
    ports:
      - "127.0.0.1:4000:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: [ "CMD", "wget", "-O-", "http://localhost:80/misc/ping" ]
      interval: 30s
      timeout: 10s
      retries: 3

Replace REL-9_18 and <digest> with the values from step 1.

3. Apply the Updated Compose Configuration

Recreate the service so Docker replaces the wildcard binding with the loopback one:

docker compose -f docker-compose-prod.yml up -d pgadmin4

4. Open an SSH Local Port-Forwarding Tunnel

From your workstation, forward local port 4000 to 127.0.0.1:4000 on the server. -N opens the tunnel without starting a remote shell:

ssh -N -L 4000:127.0.0.1:4000 user@your-server

Then open http://localhost:4000 in your browser. Traffic between your workstation and the server travels inside the encrypted SSH session. Stop the tunnel with Ctrl+C.

Verify

  1. Loopback-only binding on the server:
    ss -tlnp | grep 4000
    
    The local address column shows 127.0.0.1:4000, and not 0.0.0.0:4000, [::]:4000 or *:4000.
  2. External access blocked: from another machine, without the tunnel, curl http://<server-public-ip>:4000 fails with Connection refused or times out.
  3. Pinned image running: docker compose -f docker-compose-prod.yml images pgadmin4 shows the pinned tag.

Troubleshooting

  • channel 2: open failed: connect failed: Connection refused when browsing http://localhost:4000: Check that pgadmin4 is running and healthy (docker compose -f docker-compose-prod.yml ps) and that Gunicorn has finished starting on port 80.
  • bind [127.0.0.1]:4000: Address already in use on your workstation: Another local process uses port 4000. Pick a free local port: ssh -N -L 14000:127.0.0.1:4000 user@your-server, then browse to http://localhost:14000.
  • manifest unknown on docker compose pull: The tag doesn't exist. Check the exact name on Docker Hub: REL-8_14, for example, was never published.

References