Call Docker in Container

Thu Aug 05 2021

Theory

There is no way to actually run a docker daemon in a container, but we can pass docker cli and docker daemon through volume mirroring.

Example

Let's run a container along with those we need mirrored.

bash
|
docker run -it \ -v /usr/bin/docker:/usr/bin/docker \ ## mirror docker cli -v /var/run/docker.sock:/var/run/docker.sock \ ## mirror docker.sock ubuntu /bin/bash

Inside this container we can actually call the docker cli.

bash
|
root@72f21d4335b99:/# docker info Client: Context: default Debug Mode: false ## other info ...

Use Case

The reason for me to do this is that I want to call docker cli in a gitlab-runner/docker executor during pipeline. The executor itself is a docker container.

Thus, I configured the runner this way:

toml
|
[[runners]] [runners.docker] image = "ubuntu" volumes = [ "/usr/bin/docker:/usr/bin/docker", "/var/run/docker.sock:/var/run/docker.sock", "/usr/local/bin/docker-compose:/usr/local/bin/docker-compose", "/cache" ]