Docker exec vs kubectl exec: Inspecting Containers and Pods

𝐘𝐨𝐮𝐫 𝐂𝐨𝐧𝐭𝐚𝐢𝐧𝐞𝐫 𝐢𝐬 𝐁𝐫𝐨𝐤𝐞𝐧. 𝐇𝐨𝐰 𝐃𝐨 𝐘𝐨𝐮 𝐋𝐨𝐨𝐤 𝐈𝐧𝐬𝐢𝐝𝐞? [Docker Deep Dive — Day 5/5] It is a very simple statement that is used to inspect inside a running container. But this is an interviewer's favourite question: Read once, do it once. You will never forget it. Your container is misbehaving in production. Logs tell you nothing. You need to board the ship and inspect it yourself. This is exactly what exec is for. A running container is a ship at sea — isolated, self-contained, fully operational. From the shore you can only watch it. But with docker exec -it, you drop a ladder through the hatch and step inside — a live shell, inside the running environment, while it keeps sailing. -i keeps the connection open. -t gives you a proper terminal. Together they hand you the wheel room of a live vessel. bash # Docker docker exec -it <container_id> /bin/bash # Kubernetes kubectl exec -it <pod_name> -- /bin/bash # Check logs inside cat /app/logs/error.log # Check running processes ps aux 𝐅𝐀𝐐: Q: What is the difference between docker exec and kubectl exec? Same idea, different fleet. docker exec boards a standalone container. kubectl exec boards a pod inside a Kubernetes cluster. Both drop you into a live shell on a moving ship. Q: Does exec change the container permanently? No. Any changes you make inside vanish when the container stops. You are boarding the ship, not rebuilding it. For permanent changes, update the Dockerfile and rebuild the image. Q: When would you NOT use exec in production? When your containers are ephemeral and immutable by design. Best practice is to fix the Dockerfile, redeploy, and read external logs — not board a live ship mid-voyage. Exec is a debugging tool, not a deployment strategy. Q: What if bash is not available inside the container? Minimal images like Alpine do not ship bash. Use /bin/sh instead — a lighter shell that is almost always present. Next series: Kubernetes Architecture — the control plane, data plane, and why the API server is the heartbeat of your entire cluster. #DevOps #Docker #Containers #kubectl #DevOpsInterview #CloudEngineering #DockerDeepDive #Kubernetes

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories