All Products
Search
Document Center

Simple Log Service:How do I obtain the labels and environment variables of a container?

Last Updated:Jun 04, 2024

When you use Logtail to collect logs from containers, you can use labels and environment variables to filter containers. Labels are included in container metadata that is returned by the docker inspect command. Environment variables are runtime environment parameters that are configured to start containers. This topic describes how to obtain the labels and environment variables of a container.

Obtain labels

  1. Log on to the host of the container whose labels you want to obtain. For more information about how to log on to an Elastic Compute Service (ECS) instance, see Connect to a Linux instance by using a password or key.

  2. Run the following command to list all running containers:

    docker ps

    f******a in the command output indicates the ID of the container.

    Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
    CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS        PORTS                 NAMES
    f******a      docker.io/library/nginx:latest  nginx -g daemon o...  6 seconds ago  Up 7 seconds  0.0.0.0:8080->80/tcp  my-nginx
  3. Run the following command to obtain the labels of the container:

    docker inspect ${Container ID}

    The Labels field in the command output indicates the labels of the container.

Obtain environment variables

  1. Log on to the host of the container whose labels you want to obtain. For more information about how to log on to an Elastic Compute Service (ECS) instance, see Connect to a Linux instance by using a password or key.

  2. Run the following command to list all running containers:

    docker ps

    f******a in the command output indicates the ID of the container.

    Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
    CONTAINER ID  IMAGE                           COMMAND               CREATED        STATUS        PORTS                 NAMES
    f******a      docker.io/library/nginx:latest  nginx -g daemon o...  6 seconds ago  Up 7 seconds  0.0.0.0:8080->80/tcp  my-nginx
  3. Run the following command to obtain the environment variables of the container:

    docker exec ${Container ID} env