Alex Leko
All content on this blog was fully or partially created using local AI (Apple MLX).

Docker to kubectl cheat sheet

  • docker
  • kubernetes
  • kubectl
  • cheat-sheet

This maps common Docker CLI commands to Kubernetes kubectl equivalents.

docker run

Creates and runs a container in Kubernetes.

Docker command kubectl equivalent Notes
docker run -d --name <name> -e KEY=VAL --image <image> kubectl create deployment <name> --image=<image>
kubectl set env deployment/<name> KEY=VAL
kubectl creates a Deployment with one replica by default. Background execution is the default.
docker run -it --attach <name> --image <image> kubectl run <name> --image=<image> -i --tty --attach Use --attach to attach stdin/stdout/stderr. Detach with Ctrl+P, then Ctrl+Q.
docker run -p 80:80 <image> kubectl expose deployment <name> --port=80 --name=<svc-name> Kubernetes separates container creation from port exposure through a Service.

docker ps

Lists running containers and pods.

Docker command kubectl equivalent Notes
docker ps kubectl get pods Lists current pods and their status.
docker ps -a kubectl get pods Includes completed or failed pods.

docker attach

Attaches to a running container process.

Docker command kubectl equivalent Notes
docker attach <container-id> kubectl attach -it <pod-name> Detach with Ctrl+P, then Ctrl+Q.

docker exec

Runs a command in a running container.

Docker command kubectl equivalent Notes
docker exec <container-id> <cmd> kubectl exec <pod-name> -- <cmd> For example, kubectl exec nginx-pod -- cat /etc/hostname.
docker exec -ti <container-id> /bin/sh kubectl exec -ti <pod-name> -- /bin/sh Interactive shell session.

docker logs

Shows logs from a container.

Docker command kubectl equivalent Notes
docker logs -f <container-id> kubectl logs -f <pod-name> Streams logs.
docker logs <container-id> kubectl logs <pod-name> Shows past logs.
N/A kubectl logs --previous <pod-name> Shows logs from the previous container instance after a restart. Kubernetes keeps logs separate per restart, while Docker concatenates them.

docker stop and docker rm

Stops and removes a container.

Docker command kubectl equivalent Notes
docker stop <id> && docker rm <id> kubectl delete deployment <name> If pods are managed by a Deployment, do not delete the pods directly because the Deployment recreates them. Delete the owning resource, such as a Deployment or ReplicaSet.

docker login

Logs in to a registry.

Docker command kubectl equivalent Notes
docker login <registry> No direct equivalent Use kubectl create secret docker-registry to pull images from private registries. See Using a Private Registry.

docker version

Shows version information.

Docker command kubectl equivalent Notes
docker version kubectl version Shows the kubectl client version and the API server version.

docker info

Shows system information.

Docker command kubectl equivalent Notes
docker info kubectl cluster-info Shows API server endpoints and system component URLs.

Things that differ:

  1. kubectl manages higher-level objects such as Deployments and Services, not only raw pods.
  2. Containers do not expose ports directly. You create a Service to expose them.
  3. Pods managed by Deployments restart automatically when they crash. Logs are separated per restart, so use --previous.
  4. Delete the owner, such as a Deployment, to remove pods managed by it.

Source: Kubernetes docs: Docker CLI to kubectl