Search⌘ K

Running the Pod after mounting hostPath

Explore how to create and run a Kubernetes pod with hostPath volume mounting to enable communication between the container and the host's Docker daemon. Learn to build Docker images inside the pod, understand the practical use of hostPath for accessing host resources, and manage pod lifecycle within a Kubernetes cluster.

Creating and testing the Pod

Let’s create the Pod and check whether, this time, we can execute Docker commands from inside the container it’ll create.

Shell
kubectl create -f docker.yml

Since the image is already pulled, starting the Pod should be almost instant.

Let’s see whether we can retrieve the list of Docker images.

Shell
kubectl exec -it docker \
-- docker image ls \
--format "{{.Repository}}"

We executed docker image ls command and shortened the output by limiting its formatting only to Repository. The output is as follows. The output of the file may vary because of add-ons enabled on the cluster.

Shell
rancher/k3d-tools
rancher/k3d-proxy
rancher/k3s

Even though we executed the docker command inside a container, the output clearly shows the images from the host. We proved that mounting the Docker socket (/var/run/docker.sock) as a volume allows communication between Docker client inside the container, and Docker ...