DEV Community

Cover image for How to execute Docker commands inside of a Docker container? πŸ³πŸ‘©πŸ»β€πŸ’»
TechWorld with Nana
TechWorld with Nana

Posted on

How to execute Docker commands inside of a Docker container? πŸ³πŸ‘©πŸ»β€πŸ’»

I've seen a lot of people ask the question: How to execute docker inside of a docker container? And there can actually be a use case for that.

The most obvious use case for me was a Jenkins container, that needs to execute docker commands in order to build and push the image in docker repo in its build jobs.

There are 3 ways to do it:

  1. get a ready Jenkins image with docker pre-installed

  2. install docker yourself in the Jenkins official image (by adjusting the Dockerfile and rebuilding the image)

  3. use docker installed on the host where the container is running. -> This is the option I have used

In order to get the docker from host into the container, you need to mount the docker installation path on the host into the Jenkins container.

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

  name: jenkins

spec:

  template:

    spec: 

      containers:

      - image: jenkins

        name: jenkins

        ...

        volumeMounts:

        - mountPath: /var/run/docker.sock

          name: docker-sock

        ...

      volumes:

      - name: docker-sock

        hostPath:

          path: /var/run/docker.sock
Enter fullscreen mode Exit fullscreen mode

Now when this Jenkins container starts you will have docker commands available inside.


Complete and FREE Kubernetes & Docker course on Youtube

Top comments (0)