How to manage Docker over SSH: contexts, logs, Compose and cleanup

To manage Docker over SSH, create a context with docker context create prod --docker host=ssh://user@server and switch to it with docker context use prod: every docker and docker compose command on your machine then runs on the server. For one command, set DOCKER_HOST=ssh://user@server instead. The remote user needs access to Docker, through the docker group or sudo.

Three ways to manage Docker over SSH

You do not need to expose the Docker API to work with containers on a server. SSH is already there, and it is enough.

  • Run commands on the server: ssh user@server docker ps. Nothing to set up.
  • A Docker context: your local docker command drives the server's Docker through SSH. Best for daily work from a laptop.
  • A containers panel in your SSH client: the list, logs and actions with a tap, also from a phone.

How do you run Docker commands over SSH directly?

The simplest way works with any server you can reach:

ssh deploy@203.0.113.10 docker ps
ssh deploy@203.0.113.10 'docker logs --tail 50 web'
ssh -t deploy@203.0.113.10 docker exec -it web sh

Use quotes when the command has options or pipes, and -t when you need an interactive terminal. Our guide to running commands over SSH explains quoting and exit codes.

How do you manage Docker over SSH with a context?

A context tells your local Docker which daemon to talk to. Create one for the server:

docker context create prod --docker "host=ssh://deploy@203.0.113.10"
docker context use prod
docker ps                  # runs on the server

Everything now runs remotely: docker images, docker stats, docker compose. Switch back with docker context use default. To run one command on another context without switching, add --context:

docker --context prod ps
docker context ls

The server needs nothing new: no open port, no TLS certificates. It only needs Docker and SSH. Your local machine needs the Docker CLI, which you get with Docker Desktop or the docker package.

One-off: DOCKER_HOST

For a script or a single command, set the variable instead:

DOCKER_HOST=ssh://deploy@203.0.113.10 docker ps

Make it faster

Each Docker command opens a new SSH connection. Reuse one with multiplexing in ~/.ssh/config:

Host 203.0.113.10
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlPersist 10m

Our guide to SSH multiplexing explains each line.

How do you use docker compose on a remote server?

With the context active, Compose runs on the server:

docker context use prod
docker compose up -d
docker compose ps
docker compose logs -f --tail 100

Two things surprise people. Build contexts in the compose file are read on your machine and sent to the server. Bind mounts, such as ./data:/data, refer to folders on the server. If your compose file and data live on the server, it is often simpler to SSH in and run Compose there.

The Docker commands you use most over SSH

TaskCommand
List running containersdocker ps
Include stopped onesdocker ps -a
Follow logsdocker logs -f --tail 100 web
Shell insidedocker exec -it web sh
Restart onedocker restart web
CPU and memorydocker stats --no-stream
Disk used by Dockerdocker system df
Update a Compose projectdocker compose pull && docker compose up -d

For more on shells in containers, see how to SSH into a Docker container.

Why does Docker say permission denied over SSH?

The message permission denied while trying to connect to the Docker daemon socket means the SSH user cannot use Docker. Two fixes:

  1. Add the user to the docker group, then sign out and back in:
    sudo usermod -aG docker $USER
  2. Or keep Docker behind sudo and run sudo docker ... on the server.

Know what the first one means: a member of the docker group can start a container that mounts the whole disk, which is root in all but name. Give it only to users who would have sudo anyway.

How do you clean up Docker disk space safely?

A server that builds or pulls images fills its disk over time. Look first, then delete:

docker system df                 # what uses space
docker image prune               # dangling images only
docker image prune -a            # every image no container uses
docker builder prune             # build cache

Be careful with docker system prune --volumes: it deletes unused volumes, and a volume holds data, such as a database you stopped for a minute. When in doubt, list volumes with docker volume ls -f dangling=true and delete them one by one.

How do you manage Docker over SSH from an iPhone or iPad?

Typing docker logs -f on a phone keyboard works, but it is not much fun at 2 a.m. Wardterm has a Containers panel for every SSH server, on iPhone, iPad, Android and Mac. It uses the server's own docker or podman command, so there is nothing to install.

  • Containers grouped by Compose project, with state, image and ports, and stopped ones folded away.
  • Logs that follow live, Shell in a new session, Restart, Stop and Start.
  • For a Compose project: logs of all its containers, the compose file in the editor, and Update images, which pulls and recreates only what changed.
  • CPU and memory bars per container, and environment variables hidden until you ask.
  • When the user needs sudo for Docker, the app asks for the password once.

The panel also sits in the side panel of a terminal, next to the live server stats. See Docker containers for every action.

Questions

How do I connect Docker to a remote host over SSH?

Run docker context create myserver --docker host=ssh://user@server, then docker context use myserver. Docker on your machine talks to the Docker daemon on the server through SSH, with no port to open.

Do I need to open port 2375 or 2376 to manage Docker remotely?

No. With an SSH context, Docker goes through your normal SSH connection. Opening the Docker API port without TLS gives anyone who reaches it root on the server.

Why do I get permission denied on /var/run/docker.sock over SSH?

The remote user cannot use Docker. Add it to the docker group with sudo usermod -aG docker $USER and sign in again, or run the commands with sudo on the server. Membership of the docker group is the same as root access.

Does docker compose work with a remote context?

Yes. With the context active, docker compose up -d runs on the server. Paths in the compose file for builds are read on your machine, and bind mounts refer to folders on the server.

How do I see the logs of a container on a remote server?

Use docker logs -f --tail 100 name with the remote context, or ssh user@server docker logs -f name. For a Compose project, docker compose logs -f.

How do I get a shell inside a container on a remote server?

Run docker exec -it name sh with the remote context, or ssh -t user@server docker exec -it name sh. The -t gives you a real terminal.

Can I manage Docker from my iPhone?

Yes. Wardterm has a Containers panel for any SSH server: Compose projects, live logs, a shell, restart and cleanup, on iPhone, iPad, Android and Mac.

Does this work with Podman?

Podman has its own remote connections with podman system connection add. Wardterm uses the docker or podman command of the server, so its Containers panel works with both.

Sources

Each page was read on the date shown. Prices and features change, so check the maker's page before you decide.

  1. Docker docs, Docker contexts, read on
  2. Docker docs, protect the Docker daemon socket, read on
  3. Docker docs, Linux post-installation steps, read on
  4. Docker docs, docker system prune, read on
  5. Podman docs, podman system connection add, read on
  6. Wardterm docs, Docker containers, read on

All posts