Docker-compose is used to run multiple containers as a single service. Let’s suppose you had an application that required NGNIX and Django, you could create one yml file which would run both the containers as a service without the need to manage them separately.
In some cases, you may want to deploy your docker-compose on a remote host. For small projects setting up container orchestration tools like Kubernetes, Docker Swarm, etc is time-consuming and hard if you are not familiar with them. Also running orchestration for a single host seems to me using them not in an appropriate way.
You just want to do docker-compose up
on the remote host.
1. Version control based
Earlier I used to practice the git-based approach. Whenever I made some changes committed them to Github. Next ssh-ed to the remote host, made git pull, build, and docker-compose up.
- Pros: intutively easy.
- Cons: many actions, setting up a repository, storing the whole codebase in the remote server, pulling new changes, etc
2. Docker repository based
In this situation, you do not need to set up git for the entire codebase on the remote server. After making changes build images locally or with CI tools such as Travis CI. Push them into the docker repository. For example Docker Hub, Github Packages, GitLab Container Registry, and more.
After pushing you to need to synchronize your docker-compose settings with the remote server. For this, you can use GitHub or SCP command for secure file transfer.
scp docker-compose.yml username@servername:/remote/directory
Then ssh to the remote server and run your docker-compose commands.
- Pros: less work on the remote server
- Cons: storing and serving private images is not free, free version have some limits on storage and bandwith
3. Docker compose on remote server
Recently I found link an easy approach to run docker-compose on the remote host. Docker-compose and docker support -H, -host command line option. With the help of this option, you can run commands on the remote host from the local computer.
Examples:
docker-compose -H "ssh://username@servername" ps -a
docker-compose -H "ssh://username@servername" logs -f web
docker-compose -H "ssh://username@servername" -f docker-compose.prod.yml exec web sh
docker-compose -H "ssh://username@servername" -f docker-compose.prod.yml up --build -d
docker -H "ssh://username@servername" ps -a
- Pros: easy to use, you just need compatible Docker Engine installed and passwordless ssh.
- Cons: not user friendly and hard to manage multiple hosts.
Use Docker Contexts for deploying docker-compose applications on different hosts.
You may receive exceptions such as:
paramiko.ssh_exception.ChannelException: ChannelException(2, 'Connect failed')
This can be solved by setting MaxSessions in /etc/ssh/sshd_config
MaxSessions 500
Then restart
sudo service ssh restart