Docker in Docker

Docker in Docker

Jothimani Radhakrishnan's photo
Jothimani Radhakrishnan
·Mar 25, 2019·

2 min read

There was a time I once visualised this scenario, Also have discussed with one of mentor (Allwyn) of this usage. How come it would be if we could use docker inside a Docker container. :p

Sounds buzzy; Yes it is.

There are several scenarios where we may end up using a scenario like this. We will see one such scenario and a simple way to implement them.

The scenario I faced was like when automating the process of building the Docker image. To build a Docker image, we can simply do

for this we need docker needs to be installed in our machine so that it can be automated via Jenkins

Okay, let us assume we have hosted the Jenkins as docker container, then what??

Shall we connect the container and install docker in it? Are you sure?

Yes, we have some cool solutions for this.

https://hub.docker.com/_/docker

This means, if you pull an image inside the container, this image will also be visible on the host system (and vice versa). The above solution is straight forward, like the nested scene. A docker daemon will be installed inside the container.

However there are other optimal solutions for this scenario;

Just mount the docker sock (from local install path) with the docker image while starting up the container.

-v /var/run/docker.sock:/var/run/docker.sock docker run --name myjenkins -p 8080:8080 -p 50000:50000 -v /var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins sudo chown root:{USER} /var/run/docker.sock

docker exec -it jenkins /bin/bash docker ps docker images

full command is as follows; I have started jenkins container by mounting the volumes

make sure the docker.sock file has permissions to the docker user group

Note: replace the user with the user from which you start the docker container: my case it is ubuntu

Now log into the docker image and check you have the access for the same.

Inside the container try running the docker commands

Thank you,

[DISPLAY_ULTIMATE_SOCIAL_ICONS]

Originally published at https://opsinsights.dev on March 25, 2019.

Did you find this article valuable?

Support Jothimani Radhakrishnan by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this