Accessing a Docker container in Kubernetes

There are some occasions when a developer or system administrator might want to enter a Docker container and obtain a Terminal session. For example, an administrator might want to view a configuration or properties file that is inside a Docker container.

About this task

Procedure

  1. As root, use a Terminal shell to log in to the Kubernetes master node.
  2. Run the following command:
    kubectl get pods

    Output is similar to the following. NAME is the name of the pod and READY indicates the number of Docker containers running inside the pod. For example, 1/1 indicates that there is one Docker container in that pod, 2/2 indicates two containers, and so on.

    [root@kubem helm]# kubectl get pods
    NAME                                                 READY   STATUS      RESTARTS   AGE
    fci-case-manager-fci-messaging-568b87878d-srw8j      8/8     Running     1          7d7h
    fci-case-manager-fci-solution-686fcdbd4-vkc47        3/3     Running     0          7d7h
    fci-cedm-integration-78dff5c885-xknfm                3/3     Running     0          7d7h
    fci-common-ui-iui-77c84d9dd6-vlvkp                   1/1     Running     0          7d7h
    fci-common-ui-iui-config-service-78974b677b-7zvnd    1/1     Running     0          7d7h
    fci-common-ui-nginx-6d77699bc-rqn7n                  1/1     Running     0          7d7h
    fci-common-ui-nodejs-778d6bc594-4z7d4                1/1     Running     0          7d7h
    fci-crypto-utils-57fbcf589b-qmp89                    1/1     Running     0          7d7h
    fci-db2-datastore-0                                  2/2     Running     0          7d7h
    fci-elasticsearch-76d4f95df5-fcmvx                   1/1     Running     0          7d7h
    fci-grafana-7f69cf4596-bntj6                         1/1     Running     0          7d7h
    fci-kafka-0                                          1/1     Running     0          7d7h
    fci-kafka-zk-0                                       1/1     Running     0          7d7h
    fci-logging-curator-1565658000-hrdb6                 0/1     Completed   0          20h
    fci-logging-elastalert-56fc6df85b-pthw9              1/1     Running     0          7d7h
    fci-logging-fb-5hsf5                                 1/1     Running     0          7d7h
    fci-logging-kb-6968b67bb9-dhrxr                      1/1     Running     0          7d7h
    fci-logging-ls-c4fd6dbf5-2q7hg                       2/2     Running     0          7d7h
    fci-mongodb-primary-0                                1/1     Running     0          7d7h
    fci-odm-76b44c4d48-vksrt                             3/3     Running     0          7d7h
    fci-prometheus-alertmanager-6d857c5f99-lcw85         2/2     Running     0          7d7h
    fci-prometheus-kube-state-metrics-54586854cf-64kzs   1/1     Running     0          7d7h
    fci-prometheus-node-exporter-mkcqh                   1/1     Running     0          7d7h
    fci-prometheus-server-5cb6c66979-wjwz5               2/2     Running     0          7d7h
    fci-search-liberty-6c5f9fbfdf-ddq7s                  3/3     Running     0          7d7h
    fci-security-audit-app-78558d7498-5qpk2              3/3     Running     0          7d7h
    fci-security-auth-nodejs-6f765bd584-lw4xt            1/1     Running     0          7d7h
    fci-security-auth-nodejs-6f765bd584-npwtb            1/1     Running     0          7d7h
    fci-security-auth-redis-77b5cdb4b7-75jbp             2/2     Running     0          7d7h
    fci-wca-fd984f5c9-rn7vw                              3/3     Running     0          3h58m
    fcii-fci-insurance-liberty-65dc5c4c99-sr78t          3/3     Running     3          21d
  3. As root, run one of the following commands to access a container:
    Important: Some containers, such as the fci-common-ui-nodejs and fci-common-ui-nginx, do not have a bash shell. You must enter the container using a Bourne shell (sh) instead. For example:
    kubectl exec -it fci-common-ui-nodejs-778d6bc594-4z7d4 sh
    • To access a container in a pod that includes a single container, enter the following command:
      kubectl exec -it container_name bash
      Where container_name is the name of the container. For example:
      kubectl exec -it fci-common-ui-iui-77c84d9dd6-vlvkp bash
    • To access a container in a pod that includes multiple containers:
      1. Run the following command using the pod name of the container that you want to access:
        kubectl describe pods pod_name
        For example:
        kubectl describe pods fci-case-manager-fci-solution-7f884497d-c2wkb
        Notice that the output includes a Containers section. The first line in this section is the first container name (in this example, fci-solution). After the container's mounts are listed, the second container name is displayed (in the same column as the first container), and so on.
      2. To access one of the containers in the pod, enter the following command:
        kubectl exec -it pod_name -c container_name bash
        Where pod_name is the pod name of the container_name that you want to access. For example:
        kubectl exec -it fci-case-manager-fci-solution-686fcdbd4-vkc47 -c fci-solution bash