Since the release of Docker 18.09.0 this has got a whole lot easier. This release added support for the ssh
protocol to the DOCKER_HOST
environment variable and the -H
argument to docker ...
commands respectively.
First of all, you’ll need SSH access to the target machine (which you’ll probably need with any approach).
Then, either:
# Re-direct to remote environment.
export DOCKER_HOST="ssh://my-user@remote-host"
# Run a container. To prove that we are on remote-host, this will print its hostname.
docker run --rm --net host busybox hostname -f
# All docker commands here will be run on remote-host.
# Switch back to your local environment.
unset DOCKER_HOST
Or, if you prefer, all in one go for one command only:
docker -H "ssh://my-user@remote-host" run --rm --net host busybox hostname -f
Note that this is not yet supported in docker-compose
v.1.23.1 (latest version as of writing) and below. However, it will be part of the next release.