HUB
Sign inSign up

donuk/sshportforwarder

By donuk

•Updated about 10 hours ago

Forwards local ports over ssh via a remote ssh server, e.g. for connecting to firewalled services

Image
0

508

donuk/sshportforwarder repository overview

⁠sshportforwarder

Re-serve remote TCP services as local ports on this container using an SSH tunnel. No DNS, iptables or second container required.

docker run --rm \
  -p 8080:8080 \
  -e SSH_HOST=bastion.example.com \
  -e SSH_USER=tunnel \
  -e SSH_KEY="$(cat ~/.ssh/id_rsa)" \
  -e PORT_8080=example.com:80 \
  donuk/sshportforwarder:latest

This serves example.com:80 (as seen from the SSH server) on port 8080 of the container. PORT_<local-port>=<remote-host>[:<remote-port>] creates one SSH local forward per variable. If the remote port is omitted it defaults to the local port, so PORT_5432=database.internal forwards local 5432 to remote 5432.

Listeners bind to 0.0.0.0 by default, so they are reachable from other containers on the same network (and via Docker's published ports:).

Docker Compose example:

services:
  ssh-forward:
    image: donuk/sshportforwarder:latest
    restart: unless-stopped
    environment:
      SSH_HOST: bastion.example.com
      SSH_USER: tunnel
      SSH_KEY: ${SSH_PRIVATE_KEY}
      PORT_8080: example.com:80
    networks:
      - backend

  app:
    image: my-app:latest
    depends_on:
      - ssh-forward
    networks:
      - backend

networks:
  backend:

The app container can then connect to ssh-forward:8080.

⁠Environment variables

VariableDefaultDescription
PORT_<local-port>(required)<remote-host> or <remote-host>:<remote-port> to forward. Repeat with different local ports.
SSH_HOST(required)SSH server to tunnel through.
SSH_USER(required)SSH user on that server.
SSH_KEY(required)Private key contents, as a multi-line value (or with literal \n separators). Never baked into the image.
SSH_PORT22SSH server port.
SSH_KNOWN_HOSTSunsetKnown hosts contents. When supplied, host keys are strictly checked.
SSH_STRICT_HOST_KEY_CHECKINGaccept-new (yes with SSH_KNOWN_HOSTS)Override OpenSSH host key checking.
FORWARD_BIND_ADDRESS0.0.0.0Address local listeners bind to. Use 127.0.0.1 to restrict listeners to the container itself.
HEALTHCHECK_HOSTexample.comHost used for the extra healthcheck-only forward.
HEALTHCHECK_PORT80Remote port for the healthcheck forward.
HEALTHCHECK_LOCAL_PORT1080Loopback port the healthcheck forward listens on. Must not clash with a PORT_ mapping.

An additional SSH forward listens on 127.0.0.1:$HEALTHCHECK_LOCAL_PORT and points at HEALTHCHECK_HOST:HEALTHCHECK_PORT. Docker's healthcheck probes that local port with nc -z, so the container is marked unhealthy if the SSH tunnel dies. This checks the local listener rather than the remote service itself. The healthcheck listener is never bound to the container's external interface.

The tunnel runs silently in the foreground. If the SSH connection drops, the container exits so a restart policy (e.g. restart: unless-stopped) can reconnect.

Tag summary

Content type

Image

Digest

sha256:f9a4b136a…

Size

5.2 MB

Last updated

about 10 hours ago

docker pull donuk/sshportforwarder