How to run docker-compose on remote host?

前端 未结 6 1255
刺人心
刺人心 2021-01-31 16:53

I have compose file locally. How to run bundle of containers on remote host like docker-compose up -d with DOCKER_HOST=?

6条回答
  •  灰色年华
    2021-01-31 17:08

    Given that you are able to log in on the remote machine, another approach to running docker-compose commands on that machine is to use SSH.

    Copy your docker-compose.yml file over to the remote host via scp, run the docker-compose commands over SSH, finally clean up by removing the file again. This could look as follows:

    scp ./docker-compose.yml SomeUser@RemoteHost:/tmp/docker-compose.yml
    ssh SomeUser@RemoteHost "docker-compose -f /tmp/docker-compose.yml up"
    ssh SomeUser@RemoteHost "rm -f /tmp/docker-compose.yml"
    

    You could even make it shorter and omit the sending and removing of the docker-compose.yml file by using the -f - option to docker-compose which will expect the docker-compose.yml file to be piped from stdin. Just pipe its content to the SSH command:

    cat docker-compose.yml | ssh SomeUser@RemoteHost "docker-compose -f - up"
    

    If you use environment variable substitution in your docker-compose.yml file, the above-mentioned command will not replace them with your local values on the remote host and your commands might fail due to the variables being unset. To overcome this, the envsubst utility can be used to replace the variables with your local values in memory before piping the content to the SSH command:

    envsubst < docker-compose.yml | ssh SomeUser@RemoteHost "docker-compose up"
    

提交回复
热议问题