How to run docker image in Kubernetes pod?

4/23/2018

How can I run a docker image in a kubernetes pod?

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 docker run gcr.io/our-project/teammates-image:latest.

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

I've added securityContext: privileged: true

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

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

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

-- Paymahn Moghadasian
docker
docker-in-docker
kubernetes

1 Answer

4/24/2018

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

Install kubectl in the main docker image which needs to run the other docker image. I did this by following these docs. Once you have kubectl installed, it should automatically be able to interact with the cluster it's running in and doesn't need extra authentication.

Next, to run the teammtes-image:latest docker image you can do something like the following in python:

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

This should:

  1. create a pod
  2. run the image
  3. return the return code of the container
  4. delete the pod after completion
-- Paymahn Moghadasian
Source: StackOverflow