DEV Community

Nate
Nate

Posted on

Reach a Remote Docker Socket Over SSH Without Exposing the Docker API

A remote Docker Engine does not need a TCP listener for you to use it from your workstation. If the engine is available through a Unix-domain socket on the server, OpenSSH can forward a local TCP port to that socket. Your Docker CLI connects to the local port; SSH carries the connection to the remote socket.

This keeps the Docker API off the server’s network interfaces. It does not make Docker access harmless: anyone who can reach the forwarded port while the tunnel is open may be able to control the remote engine.

Check the remote socket and SSH access

On the remote host, the usual rootful Docker socket is /var/run/docker.sock. The SSH account must be able to connect to it. Check its existence and permissions over SSH:

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

The socket is commonly owned by root and a group such as docker. If dev cannot access it, Docker commands will fail even though SSH connects successfully. Adding a user to the Docker group is not a minor permission change: Docker access can effectively grant root-level control of the host. Use an account and access policy appropriate to that risk; don’t loosen socket permissions with chmod 666.

Rootless Docker installations may use a different socket path, often under /run/user/<uid>/. Find the actual endpoint for that setup and substitute it below.

Forward a loopback port to the Unix socket

Run this on your workstation:

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

Here, -L creates local forwarding, 127.0.0.1:23750 is the local listening address and port, and /var/run/docker.sock is the socket path on the SSH server. In this form, OpenSSH forwards connections to a remote Unix-domain socket rather than to a remote TCP host and port. The -N option tells SSH not to run a remote command; -T disables pseudo-terminal allocation. ExitOnForwardFailure makes SSH exit if it cannot establish the local forwarding listener, though it does not prove that the remote socket will accept a Docker request.

Leave this terminal open while using Docker. In another local terminal, point the CLI at the forwarded port:

DOCKER_HOST=tcp://127.0.0.1:23750 docker ps
Enter fullscreen mode Exit fullscreen mode

You can also check the connection and engine information with:

DOCKER_HOST=tcp://127.0.0.1:23750 docker version
Enter fullscreen mode Exit fullscreen mode

The client should show both client and server details if it can reach the engine. If DOCKER_HOST is set only for one command as above, it won’t change the endpoint for later Docker commands. To apply it to the current shell session instead:

export DOCKER_HOST=tcp://127.0.0.1:23750
docker ps
Enter fullscreen mode Exit fullscreen mode

Unset it when you’re finished with the tunnel:

unset DOCKER_HOST
Enter fullscreen mode Exit fullscreen mode

The Docker CLI may also use a selected context to determine its endpoint. If a command appears to ignore DOCKER_HOST, check docker context show and the CLI’s environment and context settings. For a one-off explicit endpoint, use docker --host tcp://127.0.0.1:23750 ps.

Understand the security boundary

Binding to 127.0.0.1 matters: it limits the listener to the local machine’s IPv4 loopback interface, rather than exposing it on the workstation’s LAN-facing interfaces. Do not replace it with 0.0.0.0 unless you deliberately intend other machines to reach the port and have designed the additional protections. The Docker API here is not protected by TLS; SSH secures the connection between the workstation and server, while local processes that can connect to the loopback port can use the forwarded API.

Treat an active tunnel like temporary access to the remote Docker socket. Keep it open only as long as needed, and close it with Ctrl-C. Use normal SSH host-key verification and an account whose socket access is intentional. For a broader explanation of local forwarding modes and their addressing, see OpenSSH local, reverse, and dynamic tunneling.

Troubleshoot by layer

If SSH exits immediately with an address-in-use error, another process may already be listening on port 23750. Choose a different unused local port and update DOCKER_HOST to match. To inspect listeners on Linux, for example:

ss -ltn | grep ':23750'
Enter fullscreen mode Exit fullscreen mode

If SSH stays open but Docker reports a connection error, first check that the local port is listening and that the SSH session is still alive. Then verify the remote socket path and permissions. A successful SSH login alone does not establish that the Docker socket exists or is accessible to the SSH user.

If Docker reports a permission error, fix access through the server’s intended account and group policy rather than making the socket world-writable. If the connection drops during longer work, the tunnel stops working too; reconnect SSH and rerun the Docker command. Keepalive settings can help detect or manage idle SSH connections, but they do not keep a disconnected tunnel available. See the SSH keepalive guide for the distinction.

For repeated use, an SSH host alias can keep the server name and identity settings in one place. The OpenSSH config examples explain host aliases and per-host options; keep the actual forwarding command explicit so it’s clear when the Docker socket is being exposed locally.

This approach avoids enabling a remote Docker TCP listener, but it still delegates powerful engine access to the SSH account and to local clients that can reach the forwarded port. Keep the bind address on loopback, protect SSH access, and close the tunnel when you’re done.

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

Top comments (0)