<p>How can I run a docker image in a kubernetes pod?</p>

<p>A teammate of mine has defined a new docker image and pushed it to my team's private registry. I own some code running in our kubernetes cluster and we need to get my code to effectively <code>docker run gcr.io/our-project/teammates-image:latest</code>. </p>

<p>I've been looking at enabling docker-in-docker on our kubernetes pods but I'm having little luck. I'm currently experimenting with <code>minikube</code> and whenever I try to <code>docker run</code> I get: <code>docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.</code>. </p>

<p>I've added
<code>
securityContext:
  privileged: true
</code></p>

<p>To my container spec. I suspect I need more configuration but haven't figured out what.</p>

<p>I'm also trying to evaluate whether I can <code>kubectl run --image=gcr.io/our-project/teammates-image:lastest</code> from within the kubernetes pod. </p>

<p>Is there a "correct" way to run docker images from within a kubernetes pod? How can I run image with docker-in-docker? </p>

<p>Here's the solution I ended up using for anyone who may stumble upon this in the future.</p>

<p>Install <code>kubectl</code> in the main docker image which needs to run the other docker image. I did this by following <a href="https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl" rel="noreferrer">these docs</a>. Once you have <code>kubectl</code> installed, it should automatically be able to interact with the cluster it's running in and doesn't need extra authentication.</p>

<p>Next, to run the <code>teammtes-image:latest</code> docker image you can do something like the following in python:</p>

<pre>
import subprocess
subprocess.run(["kubectl", "run", "teammates-image", "--image", "gcr.io/our-project/teammates-image:lastest", "--attach", "--restart", "Never", "--rm"])
</pre>

<p>This should:</p>

<ol>
<li>create a pod</li>
<li>run the image</li>
<li>return the return code of the container</li>
<li>delete the pod after completion</li>
</ol>
