P E N C I L
Login
How to run docker inside docker?
  1. Why?
    1. The typical case: Your jenkins is running in a docker container. The jenkin container itself needs to launch a new docker container.
  2. Take a way solution
    run -it -v /var/run/docker.sock:/var/run/docker.sock .....
  3. More explanation
    1. The most straightforward idea is child docker. Just install docker daemon insdie the docker and launch a child container.
      You will soon discover that it does not work !
      Even if you can try to manage to make it work but it's not a recommended way.
      For more detail, you can read in https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/
    2. The correct solution:
      Only use the docker client in the jenkin but use the docker daemon in the host machine
  4. Full recipe
    1. Dockerfile for parent docker
      FROM ubuntu:latest
      # install docker
      RUN apt-get update
      RUN apt-get install docker.io -y
      CMD bash

    2. Build image as normal
      docker build -t parent .
    3. Run container from image
      docker run -it --rm parent   -v /var/run/docker.sock:/var/run/docker.sock
    4. Inside container, now you can run docker
      docker images
      > It list all docker images in the host machine.
      > The docker client sends commands (basically json strings) to docker daemon via unix socket  /var/run/docker.sock to get the list of images.
      Because this socket is bind to socket of host machine, it's the docker daemon on the host machine that execute the command.
    5. Quizz: What will happen if you run docker build to build an image
      > In exactly the same way, the command is sent to docker daemon on host machine. This daemon creates the image and store it in the host machine.
      > In this way, all docker container even children docker container can reuse all images in the host machine. 
      > That's great!
  5. Reference
    1. https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/
    2. https://www.jujens.eu/posts/en/2017/Feb/15/docker-unix-socket/