skip to Main Content

I am using the docker dind image and creating a pod. When I try to run docker build inside the pod, I get an error.

    apiVersion: v1
    kind: Pod
    metadata:
      name: dockercontainer
      namespace: default
    spec:
      containers:
      - image: docker:24.0.0-rc.1-dind
        name: dockercontainer
        securityContext:
          runAsUser: 0

The pod is getting created, but when I execute a docker build inside the pod, I get the following error:
ERROR: Cannot connect to the docker daemon at unix://var/run/docker.sock

enter image description here

2

Answers


  1. Can you check the OS Distro? Plz run this command cat /etc/os-release. As for the error message, it looks like the docker Daemon isn’t running. Can you try running this command sudo dockerd &?

    You can read more about dockerd here. For ArchLinux, I had posted a similar answer here.

    Login or Signup to reply.
  2. NOTE: this assumes that you’re using a docker runtime in K8s! If not, use kaniko.

    The docker daemon isn’t mounted into the pod – this is the reason for your error. In order to build images, you will need one of these:

    • get the docker daemon mounted into each container of the pod
    • run the containers in the pod in privileged mode
    apiVersion: v1
    kind: Pod
    metadata:
      name: dockercontainer
      namespace: default
    spec:
      containers:
      - image: docker:24.0.0-rc.1-dind
        name: dockercontainer
        securityContext:
          privileged: true # this should do the trick
    

    There are some issues w/ building/running containers w/in a K8s pod, from a security standpoint (more on this here and here). Much safer approach is to use sysbox but that’s perhaps more detailed than we want to get here.

    I’m assuming that it’s not possible for you to be able to build this image outside of K8s.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search