dockerkubernetesdockerpykubernetes-python-clientpython-docker

Python Docker SDK Inside Kubernetes


I followed this link - https://docs.docker.com/engine/api/sdk/examples/ and the docker SDK worked fine while I was using Docker containers. Now that I have moved to K8s, when I run the code I get error like "Container Not Found" . Is there a way to use Python Docker SDK inside of Kubernetes PODS? Error:

docker.errors.NotFound: 404 Client Error for http+docker://localhost/v1.41/containers/taskmanager-son/json: Not Found ("No such container: taskmanager-son")

Note: I am not facing this error while running as Plain Docker Containers. This happens only when code runs inside of k8s PODS.

Note: Also docker ps returns nothing since all containers are running inside K8s PODS

import docker
docker_client = docker.from_env()
print(docker_client.containers.list())
# Returns none since since all containers are running inside K8s PODS

You can refer about Python Docker SDK here: https://docker-py.readthedocs.io/en/stable/


Solution

  • You can't use the Docker API from inside Kubernetes; you need to use the Kubernetes API instead. You can do many of the same things you might have done with the Docker API using the Kubernetes API (launch new Jobs, query existing Pods, ...). However, you can't build new images, the set of operations you perform can be limited by the Kubernetes RBAC system, and it is harder to take over the host.

    The Docker API has some significant security considerations, and Kubernetes isn't guaranteed to be running Docker proper. It's also worth noting that the Docker API could only inspect or operate on containers on the same node, where the Kubernetes API can see everything in the cluster.

    (The differences between these two environments is a good reason to avoid needing direct access to either API, if you can avoid it. For example, a job queue system like RabbitMQ plus a long-running worker can run in any environment, and is easier to both scale and control than launching a separate container per job.)