0

It seems pretty easy to build image when I just mount the docker.sock in a container. It's same as what I do on hosts machine. Simply docker build . -t image/name:sometag.

But when I need to do the same from a pod/container in K3s installation, I don't find a way of doing it. Can someone please guide me to something? It has to happen without mounting docker.sock.

2 Answers 2

1

If you simply need to build a docker image, I recommend using Kaniko. It allows you to build a docker image without having to expose the docker.sock or running your pods in privileged mode, which introduces potential security vulnerabilities. Per the Kaniko documentation, your spec file would look similar to:

apiVersion: v1
kind: Pod
metadata:
  name: kaniko
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:latest
    args:
    - "--dockerfile=<path to Dockerfile within the build context>"
  restartPolicy: Never
0

You can deploy docker on same host where you have k3s and then use same technique with docker sock via mounting volume. I.e. this is part of the spec we did for Jenkins on k3s:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: jenkins-slave
spec:
  containers:
  - name: dind
    image: docker:19.03.12-dind
    command:
    - cat
    volumeMounts:
    - mountPath: /var/run/docker.sock
      name: dockersock
    tty: true
  volumes:
  - name: dockersock
    hostPath:
      path: /var/run/docker.sock

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.