在 Azure 容器实例中检索容器日志和事件Retrieve container logs and events in Azure Container Instances

在 Azure 容器实例中,如果容器的行为错误,应首先使用 az container logs 查看其日志,然后使用 az container attach 流式传输其标准输出和标准错误。When you have a misbehaving container in Azure Container Instances, start by viewing its logs with az container logs, and stream its standard out and standard error with az container attach. 还可以在 Azure 门户中查看容器实例的日志和事件,或将容器组的日志和事件数据发送到 Azure Monitor 日志You can also view logs and events for container instances in the Azure portal, or send log and event data for container groups to Azure Monitor logs.

查看日志View logs

要查看容器内应用程序代码的日志,可使用 az container logs 命令。To view logs from your application code within a container, you can use the az container logs command.

下面是在容器实例中设置命令行一文中基于示例任务的容器的日志输出(在使用命令行重写提供无效 URL 之后):The following is log output from the example task-based container in Set the command line in a container instance, after having provided an invalid URL using a command-line override:

$ az container logs --resource-group myResourceGroup --name mycontainer
Traceback (most recent call last):
  File "wordcount.py", line 11, in <module>
    urllib.request.urlretrieve (sys.argv[1], "foo.txt")
  File "/usr/local/lib/python3.6/urllib/request.py", line 248, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/usr/local/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/usr/local/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/lib/python3.6/urllib/request.py", line 570, in error
    return self._call_chain(*args)
  File "/usr/local/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.6/urllib/request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

附加输出流Attach output streams

az container attach 命令在容器启动过程中提供诊断信息。The az container attach command provides diagnostic information during container startup. 启动容器后,它会将 STDOUT 和 STDERR 流式传输到本地控制台。Once the container has started, it streams STDOUT and STDERR to your local console.

例如,下面是在容器实例中设置命令行一文中基于任务的容器的输出(在提供需处理的大型文本文件的有效 URL 之后):For example, here is output from the task-based container in Set the command line in a container instance, after having supplied a valid URL of a large text file to process:

$ az container attach --resource-group myResourceGroup --name mycontainer
Container 'mycontainer' is in state 'Unknown'...
Container 'mycontainer' is in state 'Waiting'...
Container 'mycontainer' is in state 'Running'...
(count: 1) (last timestamp: 2019-03-21 19:42:39+00:00) pulling image "mcr.microsoft.com/azuredocs/aci-wordcount:latest"
Container 'mycontainer1' is in state 'Running'...
(count: 1) (last timestamp: 2019-03-21 19:42:39+00:00) pulling image "mcr.microsoft.com/azuredocs/aci-wordcount:latest"
(count: 1) (last timestamp: 2019-03-21 19:42:52+00:00) Successfully pulled image "mcr.microsoft.com/azuredocs/aci-wordcount:latest"
(count: 1) (last timestamp: 2019-03-21 19:42:55+00:00) Created container
(count: 1) (last timestamp: 2019-03-21 19:42:55+00:00) Started container

Start streaming logs:
[('the', 22979),
 ('I', 20003),
 ('and', 18373),
 ('to', 15651),
 ('of', 15558),
 ('a', 12500),
 ('you', 11818),
 ('my', 10651),
 ('in', 9707),
 ('is', 8195)]

获取诊断事件Get diagnostic events

如果容器无法成功部署,则请查看由 Azure 容器实例资源提供程序提供的诊断信息。If your container fails to deploy successfully, review the diagnostic information provided by the Azure Container Instances resource provider. 若要查看容器事件,请运行 az container show 命令:To view the events for your container, run the az container show command:

az container show --resource-group myResourceGroup --name mycontainer

输出包括容器的核心属性,以及部署事件(此处截断显示):The output includes the core properties of your container, along with deployment events (shown here truncated):

{
  "containers": [
    {
      "command": null,
      "environmentVariables": [],
      "image": "mcr.microsoft.com/azuredocs/aci-helloworld",
      ...
        "events": [
          {
            "count": 1,
            "firstTimestamp": "2019-03-21T19:46:22+00:00",
            "lastTimestamp": "2019-03-21T19:46:22+00:00",
            "message": "pulling image \"mcr.microsoft.com/azuredocs/aci-helloworld\"",
            "name": "Pulling",
            "type": "Normal"
          },
          {
            "count": 1,
            "firstTimestamp": "2019-03-21T19:46:28+00:00",
            "lastTimestamp": "2019-03-21T19:46:28+00:00",
            "message": "Successfully pulled image \"mcr.microsoft.com/azuredocs/aci-helloworld\"",
            "name": "Pulled",
            "type": "Normal"
          },
          {
            "count": 1,
            "firstTimestamp": "2019-03-21T19:46:31+00:00",
            "lastTimestamp": "2019-03-21T19:46:31+00:00",
            "message": "Created container",
            "name": "Created",
            "type": "Normal"
          },
          {
            "count": 1,
            "firstTimestamp": "2019-03-21T19:46:31+00:00",
            "lastTimestamp": "2019-03-21T19:46:31+00:00",
            "message": "Started container",
            "name": "Started",
            "type": "Normal"
          }
        ],
        "previousState": null,
        "restartCount": 0
      },
      "name": "mycontainer",
      "ports": [
        {
          "port": 80,
          "protocol": null
        }
      ],
      ...
    }
  ],
  ...
}

后续步骤Next steps

了解如何为 Azure 容器实例排查常见容器和部署问题Learn how to troubleshoot common container and deployment issues for Azure Container Instances.

了解如何将容器组的日志和事件数据发送到 Azure Monitor 日志Learn how to send log and event data for container groups to Azure Monitor logs.