All Products
Search
Document Center

Elastic Compute Service:Run commands in containers

Last Updated:May 15, 2026

Cloud Assistant lets you run commands directly in Kubernetes containers on Linux ECS instances without logging on to instances or using jump servers. Specify ContainerId or ContainerName when calling RunCommand or InvokeCommand.

Prerequisites

  • The instances are in the Running state.

  • Cloud Assistant Agent 2.2.3.344 or later is installed on the instances.

    See Install Cloud Assistant Agent.

Limitations

  • Cloud Assistant supports only CRI-managed Kubernetes containers running on Docker, containerd, or CRI-O.

  • The CRI imposes the following limits when you run commands in containers.

    Limitation

    Workaround

    Only the default container user can run commands.

    • When ContainerId or ContainerName is specified, the Username parameter is ignored. Commands run as the default container user.

    • To switch users, run sudo in the container. Ensure the container image supports user switching.

    Commands run only in the default working directory.

    • When ContainerId or ContainerName is specified, the WorkingDir parameter is ignored.

    • To change the working directory, add a cd command at the beginning of the script.

    Only shell scripts are supported. Shebang lines such as #!/usr/bin/python are not recognized.

    • When ContainerId or ContainerName is specified, scripts run with /bin/sh in the container. The #! shebang directive is ignored.

    • To run a non-shell script, invoke the interpreter directly. For example, add /usr/bin/python -c 'import sys; print(sys.version_info)' to call the Python interpreter.

Procedure

  1. Obtain the ID and name of a Kubernetes container.

    Method 1: Use kubectl to obtain the container ID and name

    Run kubectl --namespace <Specified namespace> describe pod <Specified pod> to view the ContainerId of a container, or query the Kubernetes apiserver for the container ID and name.

    1. Connect to the instance.

      See Connect to a Linux instance by using a password or key.

    2. Query the container ID:

      kubectl --namespace <Specified namespace> describe pod <Specified pod>

      In this example, the namespace is kube-system and the pod is kube-proxy-h4slq. Sample output:

      [test@localhost ~]# kubectl --namespace kube-system describe pod kube-proxy-h4slq
      Name:                 kube-proxy-h4slq
      Namespace:            kube-system
      Priority:             2000001000
      Priority Class Name:  system-node-critical
      Node:                 master1/192.168.1.11
      Start Time:           Thu, 11 Aug 2022 16:33:04 +0800
      Labels:               controller-revision-hash=9c5d7****
                            k8s-app=kube-proxy
                            pod-template-generation=1
      Annotations:          <none>
      Status:               Running
      IP:                   192.168.1.11
      IPs:
        IP:           192.168.1.11
      Controlled By:  DaemonSet/kube-proxy
      Containers:
        kube-proxy:
          Container ID:  docker://fe17ab0409739e63f526aed7c79e87989e90d19f0429********************
          Image:         k8s.gcr.io/kube-proxy:v1.23.9
          Image ID:      docker://sha256:9e6a540eeeb62a64450dad488760cc3769b23d19fc21********************
          Port:          <none>
          Host Port:     <none>
          Command:
            /usr/local/bin/kube-proxy
            --config=/var/lib/kube-proxy/config.conf
            --hostname-override=$(NODE_NAME)
          State:          Running
            Started:      Thu, 11 Aug 2022 16:33:06 +0800
          Ready:          True
          Restart Count:  0
          Environment:
            NODE_NAME:   (v1:spec.nodeName)
          Mounts:
            /lib/modules from lib-modules (ro)
            /run/xtables.lock from xtables-lock (rw)
            /var/lib/kube-proxy from kube-proxy (rw)
            /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-69g97 (ro)

      In the Containers section, the Container ID of the kube-proxy container is docker://fe17ab0409739e63f526aed7c79e87989e90d19f0429********************. This indicates a Docker runtime with container ID fe17ab0409739e63f526aed7c79e87989e90d19f0429********************. The CRI-managed container name is kube-proxy.

    3. (Optional) View container runtime types and versions on each node:

      kubectl get nodes -o wide

      Sample output:

      NAME    STATUS     ROLES                  AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                         KERNEL-VERSION                 CONTAINER-RUNTIME
      node1   Ready      <none>                 17h   v1.23.6   192.168.1.101   <none>        Ubuntu 20.04.4 LTS               5.4.0-123-generic              containerd://1.6.7
      node2   Ready      <none>                 17h   v1.23.6   192.168.1.102   <none>        openSUSE Leap 15.4               5.14.21-150400.24.11-default   cri-o://1.22.0
      master1 Ready      control-plane,master   18h   v1.23.6   192.168.1.11    <none>        CentOS Linux 7 (Core)            3.10.0-1160.71.1.el7.x86_64    docker://20.10.17
      node3   Ready      <none>                 17h   v1.23.6   192.168.1.103   <none>        Debian GNU/Linux 11 (bullseye)   5.10.0-16-amd64                docker://20.10.17

      The CONTAINER-RUNTIME column shows the container runtime on each node.

    Method 2: Use Cloud Assistant Agent to obtain the container ID and name

    Cloud Assistant Agent on Linux instances can list CRI-managed Kubernetes containers running on Docker, containerd, or CRI-O without requiring kubectl.

    The list-containers subcommand lists containers on instances. Examples:

    • Run aliyun-service list-containers --source cri.

      The --source cri parameter lists only CRI-managed running containers. Sample output:

      [test@localhost ~]# aliyun-service list-containers --source cri
      Container Id                                                      Container Name           Pod Name                                         Runtime  State    Data Source
      4f14883f30580007b2b386be16c743048d7b7b6a6522********************  etcd                     etcd-izbp199sm5j54**********                     docker   RUNNING  CRI
      60775315aa50765de7332764322f7697ded2783e4860********************  kube-apiserver           kube-apiserver-izbp199sm5j54**********           docker   RUNNING  CRI
      8d8dd01f09f451109285a0094eef0c144bdfdef6913e********************  coredns                  coredns-64897**********                          docker   RUNNING  CRI
      aee068814a7b10419186bb944832e65df2a8b3cab32b********************  kube-scheduler           kube-scheduler-izbp199sm5j54**********           docker   RUNNING  CRI
      b06609d4246be717c324b366d4e6c392fcf226ecbc4b********************  kube-flannel             kube-flanne**********                            docker   RUNNING  CRI
      c0992c3401ad52b3fce105ce5188026f28db7d5fe202********************  kube-controller-manager  kube-controller-manager-izbp199sm5j54**********  docker   RUNNING  CRI
      d1add169bb596f53d31030f84d69e494e7b23135acd2********************  coredns                  coredns-64897**********                          docker   RUNNING  CRI
      fe17ab0409739e63f526aed7c79e87989e90d19f0429********************  kube-proxy               kube-proxy-*****                                 docker   RUNNING  CRI

      Use the Container Id and Container Name values for the ContainerId and ContainerName parameters. The Pod Name column shows the corresponding Kubernetes pods.

    • Run aliyun-service list-containers --source cri --all.

      The --all parameter includes containers in all states (including exited). Sample output:

      [test@localhost ~]# aliyun-service list-containers --source cri --all
      Container Id                                                      Container Name           Pod Name                                         Runtime  State    Data Source
      240246ecbb7b4bfc3e0fd1bad51a76d43603c9cdee6a********************  install-cni              kube-flannel-ds-*****                            docker   EXITED   CRI
      4f14883f30580007b2b386be16c743048d7b7b6a6522********************  etcd                     etcd-izbp199sm5j************                     docker   RUNNING  CRI
      60775315aa50765de7332764322f7697ded2783e4860********************  kube-apiserver           kube-apiserver-izbp199sm5j************           docker   RUNNING  CRI
      8d8dd01f09f451109285a0094eef0c144bdfdef6913e********************  coredns                  coredns-64897985d-8qz25                          docker   RUNNING  CRI
      aee068814a7b10419186bb944832e65df2a8b3cab32b********************  kube-scheduler           kube-scheduler-izbp199sm5j************           docker   RUNNING  CRI
      b06609d4246be717c324b366d4e6c392fcf226ecbc4b********************  kube-flannel             kube-flannel-ds-*****                            docker   RUNNING  CRI
      bdf9a2aff47a6858897d5c734c481535f7bb70321c8b********************  install-cni-plugin       kube-flannel-ds-*****                            docker   EXITED   CRI
      c0992c3401ad52b3fce105ce5188026f28db7d5fe202********************  kube-controller-manager  kube-controller-manager-izbp199sm53************  docker   RUNNING  CRI
      d1add169bb596f53d31030f84d69e494e7b23135acd2********************  coredns                  coredns-***************                          docker   RUNNING  CRI
      fe17ab0409739e63f526aed7c79e87989e90d19f0429********************  kube-proxy               kube-proxy-*****                                 docker   RUNNING  CRI
    • Run aliyun-service list-containers.

      Without --source, the command lists both CRI-managed containers and non-CRI Docker containers. Sample output:

      Important

      Commands can only be run in CRI-managed containers. Non-CRI Docker containers are not supported.

      [test@localhost ~]# aliyun-service list-containers
      Container Id                                                      Container Name                                                                                          Pod Name                                         Runtime  State    Data Source
      4f14883f30580007b2b386be16c743048d7b7b6a6522********************  etcd                                                                                                    etcd-izbp199sm5j************                     docker   RUNNING  CRI
      60775315aa50765de7332764322f7697ded2783e4860********************  kube-apiserver                                                                                          kube-apiserver-izbp199sm5j************           docker   RUNNING  CRI
      8d8dd01f09f451109285a0094eef0c144bdfdef6913e********************  coredns                                                                                                 coredns-648************                          docker   RUNNING  CRI
      aee068814a7b10419186bb944832e65df2a8b3cab32b********************  kube-scheduler                                                                                          kube-scheduler-izbp199sm5j************           docker   RUNNING  CRI
      b06609d4246be717c324b366d4e6c392fcf226ecbc4b********************  kube-flannel                                                                                            kube-flannel-********                            docker   RUNNING  CRI
      c0992c3401ad52b3fce105ce5188026f28db7d5fe202********************  kube-controller-manager                                                                                 kube-controller-manager-izbp199sm5j************  docker   RUNNING  CRI
      d1add169bb596f53d31030f84d69e494e7b23135acd2********************  coredns                                                                                                 coredns-648************                          docker   RUNNING  CRI
      fe17ab0409739e63f526aed7c79e87989e90d19f0429********************  kube-proxy                                                                                              kube-proxy-*****                                 docker   RUNNING  CRI
      b6864279148b3cef6e72c983f7ffa041dfe7ab5e2c57********************  k8s_POD_coredns-64897985d-8qz25_kube-system_5422418e******************************                                                                       docker   RUNNING  docker
      fa99eaa067927e47f4e30795ad45065383efc8669687********************  k8s_POD_coredns-64897985d-cxvdq_kube-system_f59766f0******************************                                                                       docker   RUNNING  docker
      3236c0f21b4a8f15fadeaaf813afb21d59889163056c********************  k8s_POD_kube-flannel-ds-qwf9p_kube-flannel_66c0205*******************************                                                                        docker   RUNNING  docker
      016bd6794042da8d4eb3b8f5f90594bad104b360670d********************  k8s_POD_kube-proxy-h4slq_kube-system_e2ce593*******************************                                                                              docker   RUNNING  docker
      ae30393668288624472d1a594be2cb45996798d6b750********************  k8s_POD_kube-scheduler-izbp199sm5j54yl36as4h9z_kube-system_f77f******************************                                                            docker   RUNNING  docker
      c2ced280d972f54aab32fda8de1f74f8799237cc51e2********************  k8s_POD_kube-controller-manager-izbp199sm5j54yl36as4h9z_kube-system_5c6*******************************                                                   docker   RUNNING  docker
      c92bce6b84d133b807d0310d215423870101d730ede2********************  k8s_POD_kube-apiserver-izbp199sm5j54yl36as4h9z_kube-system_7f6f******************************                                                            docker   RUNNING  docker
      4a5fe93977ad8eb945889cffe29f9d99d1540e870e69********************  k8s_POD_etcd-izbp199sm5j54yl36as4h9z_kube-system_4a85******************************                                                                      docker   RUNNING  docker
      a87c87c7dc162e99749cb78f8f270765e90f3df921d2********************  modest_bose                                                                                                                                              docker   RUNNING  docker

      Data Source column values:

      • CRI: CRI-managed Kubernetes containers.

      • docker: Non-CRI Docker containers.

    • Run aliyun-service list-containers --source cri --json.

      The --json parameter outputs container information in JSON format. You can use Cloud Assistant to query and parse container data for O&M automation.

      [test@localhost ~]# aliyun-service list-containers --source cri --json
      [{"id":"4f14883f30580007b2b386be16c743048d7b7b6a6522********************","name":"etcd","podId":"4a5fe93977ad8eb945889cffe29f9d99d1540e870e6*********************","podName":"etcd-izbp199sm5j54**********","runtimeName":"docker","state":"RUNNING","dataSource":"CRI"},{"id":"60775315aa50765de7332764322f7697ded2783e4860********************","name":"kube-apiserver","podId":"c92bce6b84d133b807d0310d215423870101d730ede2********************","podName":"kube-apiserver-izbp199sm5j54**********","runtimeName":"docker","state":"RUNNING","dataSource":"CRI"},{"id":"8d8dd01f09f451109285a0094eef0c144bdfdef6913e********************","name":"coredns","podId":"b6864279148b3cef6e72c983f7ffa041dfe7ab5e2c57********************","podName":"coredns-64897**********","runtimeName":"docker","state":"RUNNING","dataSource":"CRI"},{"id":"aee068814a7b10419186bb944832e65df2a8b3cab32b********************","name":"kube-scheduler","podId":"ae30393668288624472d1a594be2cb45996798d6b750********************","podName":"kube-scheduler-izbp199sm5j54**********","runtimeName":"docker","state":"RUNNING","dataSource":"CRI"},{"id":"b06609d4246be717c324b366d4e6c392fcf226ecbc4b********************","name":"kube-flannel","podId":"3236c0f21b4a8f15fadeaaf813afb21d59889163056c********************","podName":"kube-flannel-ds-*****","runtimeName":"docker","state":"RUNNING","dataSource":"CRI"},{"id":"c0992c3401ad52b3fce105ce5188026f28db7d5fe202********************","name":"kube-controller-manager","podId":"c2ced280d972f54aab32fda8de1f74f8799237cc51e2********************","podName":"kube-controller-manager-izbp199sm5j54**********","runtimeName":"docker","state":"RUNNING","dataSource":"CRI"},{"id":"d1add169bb596f53d31030f84d69e494e7b23135acd2********************","name":"coredns","podId":"fa99eaa067927e47f4e30795ad45065383efc8669687********************","podName":"coredns-64897**********","runtimeName":"docker","state":"RUNNING","dataSource":"CRI"},{"id":"fe17ab0409739e63f526aed7c79e87989e90d19f0429********************","name":"kube-proxy","podId":"016bd6794042da8d4eb3b8f5f90594bad104b360670d********************","podName":"kube-proxy-*****","runtimeName":"docker","state":"RUNNING","dataSource":"CRI"}]
  2. Run commands in containers with Cloud Assistant.

    You can run commands in specific containers only by calling API operations. When calling RunCommand or InvokeCommand, specify ContainerId, ContainerName, or both.

    Note

    For limits on Cloud Assistant commands, see the "Prerequisites" and "Background information" sections in Use the immediate execution feature.

Result

Call DescribeInvocations or DescribeInvocationResults to query execution status or results. The response includes the ContainerId and ContainerName values.