Execute docker commands inside of a docker container

Aktualisiert: 20. Dez 2019

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


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

You can find my complete Docker and Kubernetes tutorial on Youtube for free:


Nicole Hiller  |  Nana Janashia

Mail: info@nnsoftware.at

©2018 by nnSoftware