Access Docker API from within container
Nowadays I’m contributing to pool project, which provides the simple http proxy launches docker container on access with commit-id of git repository, to be called even a “Just In Time Container”.
At first prototyping, pool provided the proxy service on vagrant VM. The proxy service kick-starts docker container by accessing Docker API, but it’s running on VM directly. So next I tried move the proxy service into docker container to separate it from docker host layer.
In this situation, How access docker api from within container? Finally I got this answer. It’s very simple solution(but may be dirty hack), that is sharing docker socket between container and host by running container with
For example, running container with
-v option to share socket.
docker run -v /var/run/docker.sock:/var/run/dockerhost/docker.sock --name inception -i -t centos /bin/bash`
Now, we can access docker api of host from within docker.
-H options enables to switch socket file.
bash-4.2# docker -H unix:///var/run/dockerhost/docker.sock ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 567a67857236 centos:latest bin/bash About a minute ago Up About a minute inception