How to execute host's Docker command from container?

Pahlevi Fikri Auliya picture Pahlevi Fikri Auliya · Jul 25, 2015 · Viewed 8.1k times · Source

I want to write Docker containers management script in Python. However, since I use CoreOS, Python is not included as standard command. So, I am thinking of using Python Docker container (https://registry.hub.docker.com/_/python/) to execute my script. However, in that case the script will be executed in container's VM which doesn't have access to the host's Docker CLI.

Is there a way to use Python (or other programming languages not packaged in CoreOS), to manage host environment without installing it on the host machine?

PS, the script will do something like:

docker run/rm/stop <another container>;

Answer

Adrian Mouat picture Adrian Mouat · Jul 25, 2015

You can either mount the docker binary and socket into the container:

$ docker run -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock debian docker --version
Docker version 1.7.0, build 0baf609

Or set up docker to allow remote access (I can't find a good reference for this at the minute).

You should also look at using the Docker API rather than making command line calls. There is a python library available to help you.