How to run docker-compose on remote host?
After the release of Docker 18.09.0 and the (as of now) upcoming docker-compose v1.23.1
release this will get a whole lot easier. This mentioned Docker release added support for the ssh
protocol to the DOCKER_HOST
environment variable and the -H
argument to docker ...
commands respectively. The next docker-compose
release will incorporate this feature as well.
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 your docker-compose commands.
docker-compose pull
docker-compose down
docker-compose up
# All docker-compose 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-compose -H "ssh://my-user@remote-host" up
One great thing about this is that all your local environment variables that you might use in your docker-compose.yml
file for configuration are available without having to transfer them over to remote-host
in some way.
You can now use docker contexts for this:
docker context create dev ‐‐docker “host=ssh://user@remotemachine”
docker-compose ‐‐context dev up -d
More info here: https://www.docker.com/blog/how-to-deploy-on-remote-docker-hosts-with-docker-compose/
From the compose documentation
Compose CLI environment variables
DOCKER_HOST
Sets the URL of the docker daemon. As with the Docker client, defaults to unix:///var/run/docker.sock.
so that we can do
export DOCKER_HOST=tcp://192.168.1.2:2375
docker-compose up
If you don't need to run docker container on your local machine, but still on the same remote machine, you can change this in your docker setting.
On the local machine: You can control remote host with -H parameter
docker -H tcp://remote:2375 pull ubuntu
To use it with docker-compose, you should add this parameter in /etc/default/docker
On the remote machine
You should change listen from external adress and not only unix socket.
See Bind Docker to another host/port or a Unix socket for more details.
If you need to run container on multiple remote hoste, you should configure Docker Swarm