Collect standard output from Docker containers (legacy)

更新时间:
复制 MD 格式

Docker generates two types of logs: file logs, which are written to a directory, and standard output, which is the container's real-time output stream. This topic shows how to use Logtail to collect standard output from containers and send it to a Logstore.

Overview

Use Logtail to collect standard output (stdout) and standard error (stderr) from your application containers on a Docker host. Logtail sends the collected logs to a Logstore for querying and analysis.

image

Prerequisites

Step 1: Install Logtail and create a machine group

  1. Pull the Logtail image

    Log in to the host and obtain the corresponding ${region_id} based on the region of the Simple Log Service Project. After you replace ${region_id}, use the following command to pull the Logtail image.

    Important
    • For the ${region_id} of each region, see Available regions. For example, the ${region_id} for China (Hangzhou) is cn-hangzhou.

    # Pull the Logtail image:
    docker pull registry.${region_id}.aliyuncs.com/log-service/logtail:v2.1.11.0-aliyun
    # If your server is in an Alibaba Cloud VPC network, run the following command to pull the Logtail image:
    docker pull registry-vpc.${region_id}.aliyuncs.com/log-service/logtail:v2.1.11.0-aliyun
  2. Start the Logtail container

    1. Parameter descriptions

      Parameter

      Description

      ${region_id}

      Obtain the corresponding ${region_id} based on the region of the Simple Log Service Project. For the ${region_id} of each region, see Service regions. To select a network type, see Logtail network types, startup parameters, and configuration files.

      • Example: For a Project in the China (Hangzhou) region, ${region_id} is cn-hangzhou for internal network access within Alibaba Cloud and cn-hangzhou-internet for public network access.

      ${aliyun_account_id}

      The ID of the Alibaba Cloud account (primary account) that owns the Simple Log Service Project. To obtain the ID, see Obtain the ID of an Alibaba Cloud account (primary account) for Simple Log Service.

      ${user_defined_id}

      A custom identifier for the machine group, such as user-defined-docker-1. This identifier must be unique within the Project's region.

    2. Replace the ${region_id}, ${aliyun_account_id}, and ${user_defined_id} placeholders in the following command and run it to start the Logtail container.

      # Start the Logtail container. Replace ${region_id}, ${aliyun_account_id}, and ${user_defined_id}.
      docker run -d \
          -v /:/logtail_host:ro \
          -v /var/run/docker.sock:/var/run/docker.sock \
          --env ALIYUN_LOGTAIL_CONFIG=/etc/ilogtail/conf/${region_id}/ilogtail_config.json \
          --env ALIYUN_LOGTAIL_USER_ID=${aliyun_account_id} \
          --env ALIYUN_LOGTAIL_USER_DEFINED_ID=${user_defined_id} \
          registry.${region_id}.aliyuncs.com/log-service/logtail:v2.1.11.0-aliyun
      Important

      If you customize the startup parameters for the Logtail container, ensure the following conditions are met:

      • At startup, you must configure the ALIYUN_LOGTAIL_CONFIG, ALIYUN_LOGTAIL_USER_ID, and ALIYUN_LOGTAIL_USER_DEFINED_ID environment variables.

      • Mount the /var/run directory on the host to the /var/run directory in the Logtail container.

      • Mount the root directory of the host to the /logtail_host directory in the Logtail container.

      • If the The parameter is invalid : uuid=none error appears in the Logtail log (/usr/local/ilogtail/ilogtail.LOG), create a product_uuid file on the host, add a valid universally unique identifier (UUID), such as 169E98C9-ABC0-4A92-B1D2-AA6239C0D261, to the file, and mount it to the /sys/class/dmi/id/product_uuid directory in the Logtail container.

    3. Run the docker ps command to check whether the container started successfully.image

  3. Create a machine group with a custom identifier

    1. Log on to the Simple Log Service console. In the Projects section, click the one you want.

    2. In the navigation pane on the left, choose Resources > Machine Groups. In the Machine Groups list, choose Machine Group > Create Machine Group.image

    3. On the Create Machine Group page, enter a Name, select Custom Identifier as the machine group identifier, and then enter the value of the ${user_defined_id} parameter from Step 1 in the Custom Identifier field. In this example, the value is user-defined-docker-1.image

      Parameter

      Description

      Machine Group Topic

      Optional. The topic is used to identify the logs generated by different servers.

  4. Check the machine group status

    In the Machine Groups list, click the target machine group. On the Machine Group Configurations page, you can view the machine group configuration and server status.image

    If the Heartbeat status is OK, Logtail is connected. If the status is FAIL, wait one minute and click Refresh. If the status remains FAIL, check the following:

    1. Verify that the Logtail container and the Project are in the same region.

    2. Verify that the host's security group allows outbound traffic from Logtail on port 80.

    For troubleshooting steps, see Troubleshoot container log collection exceptions.

Step 2: Create a Logtail configuration

  1. On the Log Storage > Logstores tab, click the logstore you want.

  2. In the left-side navigation pane, click Logtail Configurations and then click Add Logtail Configuration.image

  3. On the Quick Data Import page, click Docker Standard Output - Legacy.image

  4. Click Use Existing Machine Groups.image

  5. In the Machine Group Configurations step, select the machine group that you created in Step 1, click > to add the machine group to the applied machine groups, and then click Next.image

  6. In the Logtail Configurations step, enter a Configuration Name and click Next.image

  7. In the Query and Analysis Configurations step, click Refresh to preview the collected logs. If no logs are displayed, confirm that the container continuously generates standard output logs. By default, standard output is stored in /var/lib/docker/containers//-json.log. If you still cannot preview any logs, see How to troubleshoot container log collection errors.image

Step 3: View the results

Important

Logtail collects only incremental logs. If no new logs are written to standard output after you apply the Logtail configuration, Logtail does not collect previous logs. For more information, see Read logs.

image

By default, each log entry collected from Docker standard output includes the following fields:

Field name

Description

__source__

The IP address of the Logtail container.

__tag__:__hostname__

The name of the Docker host where Logtail is located.

__tag__:__receive_time__

The time when the log entry arrived at the server.

_time_

The time when the data was uploaded. Example: 2024-02-02T02:18:41.979147844Z.

_source_

The source of the log entry. Valid values: stdout and stderr.

_image_name_

The name of the image.

_container_name_

The name of the container.

_container_ip_

The IP address of the application container.

Related documents