Docker usually offers the first introduction to containers by a developer. Kubernetes is an orchestration platform that solves challenges of turning containers into production. Here’s how Docker commands map to their Kubernetes counterparts.
You can use the docker
CLI for interacting with containers running in Kubernetes. Kubernetes offers its own command line interface, kubectl
, to help you manage your cluster. Read our guide to get started kubectl
if you are not familiar with the tool.
None of the docker
commands have the same name in it kubectl
Kubernetes exposes functionality in its own way. Workloads themselves are fundamentally different ̵
The first point to appreciate is the shift in terminology. Docker refers to ‘containers’ while Kubernetes uses ‘pods’. A pod can run a single container or multiple replicas managed as a single unit. Apart from this detail, seeing “container” in Docker makes you think of a Kubernetes “pod”. The terms are used interchangeably throughout the rest of this article.
Request details of your containers
In Docker you use docker ps -a
to see all containers on your machine.
The closest Kubernetes equivalent is kubectl get pods
The output of the two commands is quite different. Docker shows more information about the workload that the container is running.
Kubernetes provides details about the image and command when using the describe pod
order. You must provide the pod name. This provides much more comprehensive information, using a list instead of a table.
Run commands in containers
Docker allows you to run a command in a running container with docker exec
The Kubernetes equivalent is also referred to exec
Use the Kubernetes pod name instead of the Docker container name. The command is specified slightly differently – it must be separated from the pod name by a --
order.
You can use the -it
flags to gain interactive access in the same way as Docker. This is short for --stdin --tty
and must be used when you want to start a shell within a pod. Specify the shell name, such as bash
, if the command.
Kubectl supports the attach
command if you want to associate with a process in a container that is already running. It works the same way as docker attach
but you have to use the -it
flags if you need interactive access.
View container logs
To view a container’s logs with Docker, use the docker logs
order. Adding the -f
switch will “track” the logs so that they are continuously streamed to your terminal.
Kubectl’s logs
command has the same syntax. Enter a pod name in the same way that Docker accepts a container name.
Both Docker and Kubernetes collect logs of standard output and standard error (stdout
stderr
) flows of running containers. Kubernetes treats container reboots differently from Docker. While in Docker, a restarted container adds its logs to the existing one, Kubernetes creates a new log for each run. You can get the logs of a replaced container by the extension --previous
flag to the logs
order.
Making containers
Docker containers are created with the run
order. Here’s how to get a nginx
server with Docker:
docker run -d --name nginx --restart=always -p 80:80 nginx
This creates a container using the nginx
base image and set it to restart automatically. The server is bound by the standard HTTP port 80.
Kubernetes requires you to think about higher-level abstractions when adding containers to your cluster. Instead of running a container, you create a effort to view your workload:
kubectl create deployment --image=nginx nginx
This will make a nginx
effort. A pod starts automatically; inside the pod there will be a container running the web server.
Creating a deployment does not bind the containers to ports. The newly created server is not yet accessible. Gates must be exposed via a maintenancePods are ephemeral and can contain multiple replicated containers. Services define a logical collection of pods and let you assign network resources to it, such as an IP address and port.
Uncovering the nginx
implementation on port 80 will allow access to the server:
kubectl expose deployment nginx --port=80 --name nginx-http
If you try to access port 80 on the cluster’s default IP address, you will now be directed to the nginx
server.
Kubectl does not directly support others docker run
options such as creating volumes and mounting links. For containers that require permanent storage, volumes must be manually configured via kubectl
commands or a volume manifest.
Remove containers
Docker containers are removed using the docker rm
command with the container ID.
You cannot delete containers directly with Kubernetes. Instead, you work with the effort who created the pod. Use the kubectl delete deployment
command, passing the name of the implementation.
Docker enables you stop a container instead of removing it. Kubernetes has removed support for this promotion. The recommended way to temporarily suspend a deployment is to scale the number of replicas to 0. If no pods are running, the workload is effectively stopped.
kubectl scale --replicas=0 deployment/my-deployment
When you are ready to resume deployment, run the scale
command again. Set the number of new replicas to 1
or higher. Using more replicas can increase the availability of your workload.
Conclusion
There are no direct parallels between the Docker CLI and kubectl
Most Kubernetes commands have a different syntax from their Docker counterparts. You must learn new terms and options before you can port Docker-based workflows to Kubernetes.
In many cases there is none kubectl
alternative to a Docker CLI capability. Docker’s functionality focuses on the container concept. Kubernetes takes that and puts it at the center of a vastly expanded resource ecosystem.
Containers are rarely handled in isolation. Instead, you need to work with resources such as deployments, services, and replica sets. This is why learning Kubernetes can seem challenging when approached from a Docker user’s perspective.
If you’re familiar with Docker basics, the transition to Kubernetes should still be relatively easy. The main difference is that what Docker sees as a container is usually accessed as an aggregated “pod” in Kubernetes. Pods are created by “deployments” that represent the workloads in your cluster. When in doubt, consult it kubectl
docs to find a suitable match for a Docker command.
Source link