Amazon Elastic Container Service
开发人员指南 (API Version 2014-11-13)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Docker 诊断

Docker 提供了几种诊断工具,可帮助您解决与容器和任务相关的问题。有关所有可用的 Docker 命令行实用工具的更多信息,请转到 Docker 文档中的 Docker 命令行主题。您可以通过使用 SSH 连接到容器实例来访问 Docker 命令行实用工具。有关更多信息,请参阅 连接到您的容器实例

Docker 容器报告的退出代码也可提供一些诊断信息(例如,退出代码 137 表示容器已收到 SIGKILL 信号)。有关更多信息,请参阅 Docker 文档中的退出状态

列出 Docker 容器

您可以在容器实例上使用 docker ps 命令来列出正在运行的容器。在以下示例中,仅 Amazon ECS 容器代理正在运行。有关更多信息,请转至 Docker 文档中的 docker ps

Copy
[ec2-user ~]$ docker ps

输出:

CONTAINER ID        IMAGE                            COMMAND             CREATED             STATUS              PORTS                        NAMES
cee0d6986de0        amazon/amazon-ecs-agent:latest   "/agent"            22 hours ago        Up 22 hours         127.0.0.1:51678->51678/tcp   ecs-agent

您可以使用 docker ps -a 命令来查看所有容器(甚至是已停止或已终止的容器)。这对于列出意外停止的容器很有用。在以下示例中,容器 f7f1f8a7a245 已在 9 秒前退出,因此它不会显示在未带 -a 标记的 docker ps 输出中。

Copy
[ec2-user ~]$ docker ps -a

输出:

CONTAINER ID        IMAGE                                       COMMAND                CREATED             STATUS                        PORTS                        NAMES
db4d48e411b1        amazon/ecs-emptyvolume-base:autogenerated   "not-applicable"       19 seconds ago                                                                 ecs-console-sample-app-static-6-internalecs-emptyvolume-source-c09288a6b0cba8a53700
f7f1f8a7a245        busybox:buildroot-2014.02                   "\"sh -c '/bin/sh -c   22 hours ago        Exited (137) 9 seconds ago                                 ecs-console-sample-app-static-6-busybox-ce83ce978a87a890ab01
189a8ff4b5f0        httpd:2                                     "httpd-foreground"     22 hours ago        Exited (137) 40 seconds ago                                ecs-console-sample-app-static-6-simple-app-86caf9bcabe3e9c61600
0c7dca9321e3        amazon/ecs-emptyvolume-base:autogenerated   "not-applicable"       22 hours ago                                                                   ecs-console-sample-app-static-6-internalecs-emptyvolume-source-90fefaa68498a8a80700
cee0d6986de0        amazon/amazon-ecs-agent:latest              "/agent"               22 hours ago        Up 22 hours                   127.0.0.1:51678->51678/tcp   ecs-agent

查看 Docker 日志

您可以使用 docker logs 命令查看容器的 STDOUTSTDERR 流。在此示例中,为简洁起见,将为 dc7240fe892a 容器显示日志,并通过 head 命令传输日志。有关更多信息,请转至 Docker 文档中的 docker logs

Copy
[ec2-user ~]$ docker logs dc7240fe892a | head

输出:

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.11. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.11. Set the 'ServerName' directive globally to suppress this message
[Thu Apr 23 19:48:36.956682 2015] [mpm_event:notice] [pid 1:tid 140327115417472] AH00489: Apache/2.4.12 (Unix) configured -- resuming normal operations
[Thu Apr 23 19:48:36.956827 2015] [core:notice] [pid 1:tid 140327115417472] AH00094: Command line: 'httpd -D FOREGROUND'
10.0.1.86 - - [23/Apr/2015:19:48:59 +0000] "GET / HTTP/1.1" 200 348
10.0.0.154 - - [23/Apr/2015:19:48:59 +0000] "GET / HTTP/1.1" 200 348
10.0.1.86 - - [23/Apr/2015:19:49:28 +0000] "GET / HTTP/1.1" 200 348
10.0.0.154 - - [23/Apr/2015:19:49:29 +0000] "GET / HTTP/1.1" 200 348
10.0.1.86 - - [23/Apr/2015:19:49:50 +0000] "-" 408 -
10.0.0.154 - - [23/Apr/2015:19:49:50 +0000] "-" 408 -
10.0.1.86 - - [23/Apr/2015:19:49:58 +0000] "GET / HTTP/1.1" 200 348
10.0.0.154 - - [23/Apr/2015:19:49:59 +0000] "GET / HTTP/1.1" 200 348
10.0.1.86 - - [23/Apr/2015:19:50:28 +0000] "GET / HTTP/1.1" 200 348
10.0.0.154 - - [23/Apr/2015:19:50:29 +0000] "GET / HTTP/1.1" 200 348
time="2015-04-23T20:11:20Z" level="fatal" msg="write /dev/stdout: broken pipe"

检查 Docker 容器

如果您拥有一个容器的 Docker ID,可以使用 docker inspect 命令检查该容器。通过检查容器,可提供在其中启动容器的环境的最详细视图。有关更多信息,请转至 Docker 文档中的 docker inspect

Copy
[ec2-user ~]$ docker inspect dc7240fe892a

输出:

[{
    "AppArmorProfile": "",
    "Args": [],
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "httpd-foreground"
        ],
        "CpuShares": 10,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": null,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/apache2/bin",
            "HTTPD_PREFIX=/usr/local/apache2",
            "HTTPD_VERSION=2.4.12",
            "HTTPD_BZ2_URL=https://www.apache.org/dist/httpd/httpd-2.4.12.tar.bz2"
        ],
        "ExposedPorts": {
            "80/tcp": {}
        },
        "Hostname": "dc7240fe892a",
...