Container is a lightweight, standalone, and executable package of software that includes everything needed to run a piece of software, including the code, a runtime, system tools, libraries, and settings. containers are built from images, which are essentially snapshots of a container at a specific point in time. Docker is most widely used container solution in the market. Docker supports Windows, Linux, Mac Operating systems. This article will walk you through how to access the container from the host.
At the end of the article, we will see how to access the pods from Kubernetes/Openshift.
On Standalone Docker host:
To login to a running Docker container, use the command “docker exec -it <container_name_or_id> /bin/bash”. This will open a Bash shell inside the container, allowing you to run commands and interact with the container’s file system. Replace <container_name_or_id> with the name or ID of the container you want to login to.
1.List the running containers on your host.
uxpro-$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1e93388a0311 nginx_ua:v1 "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp nginx_ua uxpro-$
If you do not have any running container, you can start one using the following command.
uxpro-$ docker run -it -d --name nginx_ua nginx /bin/bash 83c6a194fe554d2e1f6089c687c42e6ce71b4c338049e4c44990423794a6436a
2. Use the following command to login to “nginx_ua”.
uxpro-$ docker exec -it nginx_ua /bin/bash root@83c6a194fe55:/# hostname 83c6a194fe55 root@83c6a194fe55:/#
3. If you do not want to login, but need to execute the command inside the container, then use the following command to get the command output.
uxpro-$ docker exec -it nginx_ua df -h Filesystem Size Used Avail Use% Mounted on overlay 59G 7.9G 48G 15% / tmpfs 64M 0 64M 0% /dev shm 64M 0 64M 0% /dev/shm /dev/vda1 59G 7.9G 48G 15% /etc/hosts tmpfs 3.9G 0 3.9G 0% /sys/firmware uxpro-$ docker exec -it nginx_ua uname -a Linux 83c6a194fe55 5.10.104-linuxkit #1 SMP PREEMPT Thu Mar 17 17:05:54 UTC 2022 aarch64 GNU/Linux uxpro-$
Logging in to Pod on Kubernetes Cluster (AKS/EKS/GKE):
A pod can contain one or more containers, and these containers are typically deployed together on the same host and share the same network namespace. This means that they can communicate with each other using localhost, and they share the same IP address and port space.
1. Login to kubernetes cluster.
2. List the running pods in the current namespace. In the READY column, “1/1” indicates that pod is hosting only one container. Always use “-n” switch to explicitly specify the namespace.
uxpro-$ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-6799fc88d8-8r9nt 1/1 Running 0 110s uxpro-$
3. Login to pod using the following command. (ex: kubectl exec -it POD_NAME -n namespace — bash)
uxpro-$ kubectl exec -it nginx-6799fc88d8-8r9nt bash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead. root@nginx-6799fc88d8-8r9nt:/# hostname nginx-6799fc88d8-8r9nt root@nginx-6799fc88d8-8r9nt:/#
You can also the following command to avoid the “DEPRECATED” warning.
uxpro-$ kubectl exec -it nginx-6799fc88d8-8r9nt -- bash root@nginx-6799fc88d8-8r9nt:/#
You can also explicitly mention the namespace of the pod. (Use “-n” flag to specify the namespace)
uxpro-$ kubectl exec -it nginx-6799fc88d8-8r9nt -n default -- bash root@nginx-6799fc88d8-8r9nt:/#
How to login to specific container when you have more than one container running on pod ?
To login to a specific container within a pod in Kubernetes, you can use the command “kubectl exec -it <pod_name> -c <container_name> — /bin/bash”. This will open a Bash shell inside the specified container, allowing you to run commands and interact with the container’s file system. Replace <pod_name> with the name of the pod and <container_name> with the name of the container you want to login to.
In the following example, we have two containers running on pod.
uxpro-$ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-7fcd4fd975-hjtbr 2/2 Running 0 12m uxpro-$
To list the containers name on the specific pod, [ kubectl get pods <pod_name> -o jsonpath='{.spec.containers[*].name}’ ]
uxpro-$ kubectl get pods nginx-7fcd4fd975-hjtbr -o jsonpath='{.spec.containers[*].name}' nginx sidecar-container% uxpro-$
To login to container “sidecar-container”, use the following command.
uxpro-$ kubectl exec -it nginx-7fcd4fd975-hjtbr -c sidecar-container -- sh / #
To login to container “nginx”, use the following command.
uxpro-$ kubectl exec -it nginx-7fcd4fd975-hjtbr -c nginx -- sh #
Logging in to Pod on Openshift Cluster:
Accessing the pods on Openshift kubernetes variant is simple unlike the other kubernetes variant. One can simply use “oc rsh pod_name” to access the pod.
uxpro-$ oc get pods NAME READY STATUS RESTARTS AGE nginx-6799fc88d8-8r9nt 1/1 Running 0 2d22h uxpro-$ oc rsh nginx-6799fc88d8-8r9nt root@nginx-6799fc88d8-8r9nt:/#
Conclusion:
This article walk you through how to login to container on Standalone docker host, Kubernetes environments like AKS, EKS, GKE or Opensource environments for both single container pods and multiple containers pod. At the end, we have seen how to access pod on Openshift environment. Hope this article is informative to you.
Leave a Reply