Running Docker in MicroK8s Kubernetes container

I have MicroK8s cluster node with Gitlab Runner container. I can delegate Gitlab pipeline steps to this container. This works fine but I also want to build and run Docker images in this container. When I try for example command like below:

docker run --rm -v $(pwd)/test/:/test --ipc=host --user pwuser registry.gitlab.com/c2platform/docker/robot:latest bash -c "robot --outputdir /tmp/output /test"`

This fails with message

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

So the log shows that the Gitlab Runner pod is using Kubernetes executor.

Is it possible to use Docker inside a Kubernetes pod?

With Docker in Docker I noticed that sometimes a mount is used

docker run --name whater  -it --rm -v /var/run/docker.sock:/var/run/docker.sock ubuntu:latest bash

Inside the container I can then run

apt update && apt install docker.io
docker ps
docker run --name whatever2  -it --rm ubuntu:latest bash  # works

So in order to enable this, I could install docker.io on MicroK8s worker nodes? Does this make sense?

A Gitlab runner that supports doing the work as in-cluster jobs is going to be better. This document seems to be talking about something like that: Kubernetes executor | GitLab

As a comparison, drone.io has a Kubernetes provider that does this and I wish the forked OSS project Woodpecker-CI had it.

If having this legacy runner architecture work is absolutely required, you can build a pod that uses the docker:dind. You can expose the socket between containers in the pod. The same thing can be done with podman as well, it can expose a docker compatible socker iirc.

I managed to create two working examples:

  1. Example gitlab-docker-build creates builds and releases images.
  2. Example gitlab-robot runs Robot Framework tests using a Robot container. So this is also a “dind” approach.

In addition to documentation linked by @protosam I can recommend Docker-in-Docker with TLS enabled in Kubernetes