DEV Community

Nate
Nate

Posted on

Use SSH to Reach a Remote Docker Socket Without Opening a TCP Port

A remote Docker daemon does not need a TCP listener for your local Docker CLI to use it. If the daemon already listens on a Unix-domain socket, OpenSSH can forward that socket over your SSH connection and expose it as another Unix socket on your workstation.

This keeps Docker’s API off the server’s network interfaces. It does not make Docker access unprivileged: anyone who can use the forwarded socket can exercise the permissions granted to the SSH account on the remote Docker socket.

Check access on the server first

The usual Linux Docker socket is /var/run/docker.sock. Log in as the account you plan to use and check that it can access the socket:

ssh dev@server
ls -l /var/run/docker.sock
Enter fullscreen mode Exit fullscreen mode

The account needs permission to connect to the socket. On many installations, that means membership in the docker group. Treat that access as highly privileged: controlling the Docker daemon can often provide effective root-level control of the host. Don’t add an account to the group casually just to make forwarding work.

If you need to troubleshoot SSH authentication before setting up the tunnel, see how to fix SSH Permission denied (publickey).

Forward the Unix socket

On your local Linux or macOS machine, start a foreground SSH session:

ssh -N \
  -o ExitOnForwardFailure=yes \
  -L /tmp/remote-docker.sock:/var/run/docker.sock \
  dev@server
Enter fullscreen mode Exit fullscreen mode

Here, the two paths after -L are local and remote Unix-domain sockets, respectively. OpenSSH creates /tmp/remote-docker.sock locally and relays connections through SSH to /var/run/docker.sock on the server. -N means not to run a remote command; the connection stays open to carry the forwarding. ExitOnForwardFailure makes SSH exit if it cannot establish the requested forward, rather than leaving you with a session that appears useful but has no working socket.

This is OpenSSH’s Unix-socket forwarding form of -L, not the more familiar TCP form such as -L 2375:localhost:2375. No Docker TCP listener or firewall port is needed. For background on the distinction between forwarding types, see SSH tunneling explained.

Keep this SSH process running. In a second local terminal, point the Docker CLI at the forwarded socket:

DOCKER_HOST=unix:///tmp/remote-docker.sock docker ps
Enter fullscreen mode Exit fullscreen mode

The unix:// prefix tells Docker to use a Unix socket instead of its normal connection setting. To use the remote daemon for several commands in that terminal, export the variable:

export DOCKER_HOST=unix:///tmp/remote-docker.sock
docker info
docker ps
Enter fullscreen mode Exit fullscreen mode

Close that terminal or unset the variable when you want subsequent Docker commands to use their usual endpoint:

unset DOCKER_HOST
Enter fullscreen mode Exit fullscreen mode

You can also put the SSH destination and options in an SSH config host entry, then use that alias in the forwarding command. See practical SSH config examples for the relevant configuration patterns.

Troubleshoot the common failures

  • SSH says the local socket already exists. The path may be in use by another tunnel, or it may be left over from an earlier run. First check that no active SSH process owns the forwarding. If the old socket is stale, remove it and retry:
  rm -f /tmp/remote-docker.sock
Enter fullscreen mode Exit fullscreen mode

Only remove the socket after confirming that no live tunnel is using it.

  • The forward fails or Docker reports a connection error. Confirm that the remote socket exists and that the SSH account can access it. The SSH server must also permit Unix-domain socket forwarding; server-side AllowStreamLocalForwarding settings can disable it. Ask the server administrator to check the effective sshd configuration if the client reports that forwarding is prohibited.

  • The tunnel is up, but Docker gets permission denied. Check socket access as the remote SSH user, not just as root. Also check that the local user can use the forwarded socket. Avoid running Docker with sudo as a first workaround: sudo may discard DOCKER_HOST, and granting broad privileges to solve a local socket permission issue is risky.

  • Commands still use the wrong Docker endpoint. Inspect the current environment with printf '%s\n' "$DOCKER_HOST". Docker contexts and environment variables can affect which endpoint the CLI selects; use the explicit DOCKER_HOST command above to verify the intended socket.

Stop and clean up

When finished, stop the foreground SSH process with Ctrl+C. Then remove the local socket if it remains:

rm -f /tmp/remote-docker.sock
Enter fullscreen mode Exit fullscreen mode

The remote daemon continues running; stopping the tunnel only ends your local route to it. Keep the socket path in a private, user-controlled location, and do not share access to the forwarded socket with untrusted local users. This workflow is useful for occasional development access without publishing Docker’s API on a TCP port, while leaving SSH authentication and the remote socket’s permissions as the access controls.

I'm building ShadeTerm — an SSH client that organizes terminals, SFTP, code editing, and server tools into workspaces.

Top comments (0)