Getting a shell to a running container can be very useful to debug and manage container. kubectl exec lets you get shell to container inside the pod.
This article will show you how to get a shell into a running container inside a pod.
Reasons to get shell to a running container
Here are some reasons to get shell to a running container.
- To debug your application running inside the container.
- To run commands inside container.
- To install any packages inside container.
- To investigate the internal state of the running container.
kubectl exec
You can use following command to get the shell to a running container.
kubectl exec -it <pod-name> -- /bin/sh
Example
Let's learn by example. We will be using the following YAML file to create our pod.
apiVersion: v1
kind: Pod
metadata:
name: nginx-app-pod
spec:
containers:
- name: nginx
image: nginx
To start running pod, you can use following command:
kubectl create -f <filename>.yaml
You can view the running pods by using following command:
kubectl get pods
You should see output something like this:
You can use following command to get shell in the running container inside the pod:
kubectl exec -it nginx-app-pod -- bash
You should see output something like this:
This is all for this article. Enjoy your root access in the container. ;)
Top comments (0)