Answers: <p>If you simply need to build a docker image, I recommend using <a href="https://github.com/GoogleContainerTools/kaniko" rel="nofollow noreferrer">Kaniko</a>. 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:</p>
  <pre><code>apiVersion: v1
  kind: Pod
  metadata:
    name: kaniko
  spec:
    containers:
    - name: kaniko
      image: gcr.io/kaniko-project/executor:latest
      args:
      - &quot;--dockerfile=&lt;path to Dockerfile within the build context&gt;&quot;
    restartPolicy: Never
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-04-05T18:24:19.217Z
Answers: 2021-04-05T18:24:19.217Z
Answers: Preston Martin
Answers: <p>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:</p>
  <pre><code>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
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-04-04T13:46:00.197Z
Answers: 2021-04-04T13:46:00.197Z
Answers: taleodor
Body: <p>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 <code>docker build . -t image/name:sometag</code>.</p>
  <p>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.</p>
  
Tags: <kubernetes><builds><images>
Title: Build image from inside a pod or container
UserDisplayName: Nabin
ThreadText: # Build image from inside a pod or container
  
  (Asked by: username_0 on 2021-04-04)
  
  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.
  
  **Tags:** kubernetes, builds, images
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2021-04-05. Score: 1
  
  If you simply need to build a docker image, I recommend using [Kaniko](https://github.com/GoogleContainerTools/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
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2021-04-04. Score: 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
  
  ```
  
  
  ---
  
  