How do I run commands in the remote Docker environment?

Sometimes, you may need to run non-docker commands in your remote docker environment. 

Although only Docker commands are automatically executed in the remote Docker environment, you can programmatically execute any set of commands in the remote Docker environment by SSHing into it and chaining any subsequent commands to your shell command.


To SSH into the remote Docker environment, run ssh remote-docker while SSH'd into any CircleCI job that uses setup_remote_docker or execute it programmatically in a run step. (Make sure the setup_remote_docker step runs first, otherwise, your SSH command will fail!)


Using `ssh remote-docker` in a run step: 

run: |
  ssh remote-docker \ echo "this echo command will be executed in the remote Docker environment \ 
rather than the initial job container itself"

The challenge presented here is that only a singular command can be passed to the SSH shell—for example, if you run ssh remote-docker touch file1 && touch file2file1 will be created in the remote Docker environment, but file2will be created in your job's local container.

So, if you want to run a more complex set of commands in the remote Docker environment, programmatically, then you need to find a way to have them executed as a single command.

Some good approaches are outlined in this stackoverflow.

These suggestions include using a "here document" or saving your commands to a shell script which you then pipe into your ssh command.


If you are using remote-docker commands to update your Docker storage driver, please checkout this helpful thread in our forum.


Was this article helpful?
1 out of 5 found this helpful



Article is closed for comments.