SHINCHVEN'S HEXO

Docker in Docker

Count: 423Reading time: 2 min
2018/05/28 Share

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.

1
concurrent = 1
2
check_interval = 0
3
4
[[runners]]
5
  name = "my-runner"
6
  url = "MY_GITLAB_URL"
7
  token = "MY_TOKEN"
8
  executor = "docker"
9
  [runners.docker]
10
    tls_verify = false
11
    image = "docker:latest"
12
    privileged = true
13
    disable_cache = false
14
    # mirror /var/run/docker.sock in your host that runs docker engine to your executor container
15
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
16
    cache_dir = "cache"
17
    shm_size = 0
18
  [runners.cache]
  • 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 docker run or docker-compose.

To build a customed image with docker client is simple, you just do the COPY like what I do in the Dockerfile given below.

1
FROM YOUR_IMAGE
2
3
# please download the docker client before you build the image at https://download.docker.com/linux/static/stable/x86_64/ .
4
COPY docker-ce.tgz /root/
5
RUN cd /root/ \
6
&& tar xzvf docker-ce.tgz \
7
&& cp docker/* /usr/bin/ \
8
&& docker --version \
9
&& rm -rf /root/docker /root/docker-ce.tgz
10
11
# please download the docker-compose from https://github.com/docker/compose/releases if you also want to use it.
12
COPY docker-compose /usr/local/bin/
13
RUN chmod +x /usr/local/bin/docker-compose
CATALOG