The correct solution: Only use the docker client in the jenkin but use the docker daemon in the host machine
Dockerfile for parent docker FROM ubuntu:latest # install docker RUN apt-get update RUN apt-get install docker.io -y CMD bash
Build image as normal docker build -t parent .
Run container from image docker run -it --rm parent
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.
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!