Basically, you can’t run a docker engine in a docker container, but in some use cases, you might still want to call docker client to build image and run container. In my use case is that I use docker executor in GitLab runner, and my apps are all deployed in docker containers, so I have to call call docker within the container at the deploy sequence.
The good thing is, docker client and docker engine can be seprated, and you can mirror the /var/run/docker.sock in container with the one your host machine which a docker engine runs within through docker volume.
In order to do the volume mirror, I edited the gitlab-runer config after it is configured as below.
concurrent = 1
check_interval = 0
name = "my-runner"
url = "MY_GITLAB_URL"
token = "MY_TOKEN"
executor = "docker"
tls_verify = false
image = "docker:latest"
privileged = true
disable_cache = false
# mirror /var/run/docker.sock in your host that runs docker engine to your executor container
volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
cache_dir = "cache"
shm_size = 0
- I used the official docker image which contains a docker client, of course you can build customed image with docker client installed within, that is easy.
- I mirrored the host’s /var/run/docker.sock to gitlab-runner’s executor container so that the docker client in container can be used with a running docker engine. You’re welcome to do the config this volume in any other way you use docker like
To build a customed image with docker client is simple, you just do the COPY like what I do in the Dockerfile given below.
# please download the docker client before you build the image at https://download.docker.com/linux/static/stable/x86_64/ .
COPY docker-ce.tgz /root/
RUN cd /root/ \
&& tar xzvf docker-ce.tgz \
&& cp docker/* /usr/bin/ \
&& docker --version \
&& rm -rf /root/docker /root/docker-ce.tgz
# please download the docker-compose from https://github.com/docker/compose/releases if you also want to use it.
COPY docker-compose /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-compose