如何查看docker-compose healthcheck日志?

时间:2017-03-11 16:53:44

标签: logging docker docker-compose health-monitoring

在我的docker-compose.yml内,我有以下service healthcheck部分。我想知道MariaDB是否真的准备好处理查询。名为service的{​​{1}}配置为依赖cmd

condition: service_healthy

此运行状况检查不起作用,表明该服务运行状况不佳。

如何检查 db: image: mariadb:10 environment: MYSQL_RANDOM_ROOT_PASSWORD: 1 MYSQL_USER: user MYSQL_PASSWORD: password MYSQL_DATABASE: database healthcheck: test: ["CMD", "mysql", "--user=user", "--password=password", "--execute='SELECT 1'", "--host=127.0.0.1", "--port=3306"] interval: 1s retries: 30 CMD的输出?

2 个答案:

答案 0 :(得分:42)

您可以使用:

docker inspect --format "{{json .State.Health }}" <container name> | jq

输出:

{
  "Status": "unhealthy",
  "FailingStreak": 63,
  "Log": [
    {
      "Start": "2017-03-11T20:49:19.668895201+03:30",
      "End": "2017-03-11T20:49:19.735722044+03:30",
      "ExitCode": 1,
      "Output": "ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''SELECT 1'' at line 1\n"

查找输出部分。

仅获取输出:

docker inspect --format "{{json .State.Health }}" mariadb_db_1 | jq '.Log[].Output'

随意交换jq用于json pretty print的任何工具。

答案 1 :(得分:1)

docker-compose ps将指示每个服务的状态,包括其运行状况(如果定义了运行状况检查)。这对基本概述很有用。

% docker-compose ps
                Name                                Command                       State                                       Ports                            
----------------------------------------------------------------------------------------------------------------------------------------------------------------
remix-theme-editor_analytics_1            /bin/sh -c /analytics/run. ...   Up                                                                                   
remix-theme-editor_base_1                 /bin/bash                        Exit 0                                                                               
remix-theme-editor_flower_1               /entrypoint --environment  ...   Exit 137                                                                             
remix-theme-editor_frontend_1             /bin/sh -c perl -p -i -e ' ...   Exit 137                                                                             
remix-theme-editor_js-app_1               npm run                          Exit 0                                                                               
remix-theme-editor_mq_1                   docker-entrypoint.sh rabbi ...   Up (healthy)            15671/tcp, 15672/tcp, 25672/tcp, 4369/tcp, 5671/tcp, 5672/tcp
remix-theme-editor_mysql-migration_1      /entrypoint_mysql-migratio ...   Exit 0                                                                               
remix-theme-editor_mysql_1                /bin/sh -c /entrypoint_wra ...   Up (health: starting)   127.0.0.2:3308->3306/tcp                                     
remix-theme-editor_page-renderer_1        npm run start:watch              Up                                                                                   
remix-theme-editor_python-app_1           /entrypoint                      Exit 2                                                                               
remix-theme-editor_redis_1                docker-entrypoint.sh /bin/ ...   Up (health: starting)   6379/tcp                                                     
remix-theme-editor_scheduler_1            /entrypoint --environment  ...   Exit 137                                                                             
remix-theme-editor_socket_1               /entrypoint --environment  ...   Exit 1                                                                               
remix-theme-editor_static-builder_1       npm run watch                    Up                                                                                   
remix-theme-editor_static-http_1          nginx -g daemon off;             Up                      127.0.0.2:6544->443/tcp, 80/tcp                              
remix-theme-editor_web_1                  /entrypoint --environment  ...   Exit 1                                                                               
remix-theme-editor_worker_1               /entrypoint --environment  ...   Exit 1                                                                               
remix-theme-editor_worker_screenshots_1   /entrypoint --environment  ...   Exit 1     

如果需要更多详细信息,请结合使用docker inspectdocker ps -q <service-name>

% docker inspect --format "{{json .State.Health }}" $(docker-compose ps -q mq) | jq
{
  "Status": "starting",
  "FailingStreak": 48,
  "Log": [
    {
      "Start": "2018-10-03T00:40:18.671527745-05:00",
      "End": "2018-10-03T00:40:18.71729051-05:00",
      "ExitCode": -1,
      "Output": "OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused \"exec: \\\"nc\\\": executable file not found in $PATH\": unknown"
    },
...

您始终可以通过简单地自己执行运行状况检查代码来自行调试运行状况检查。例如:

% docker exec -it $(docker-compose ps -q socket) nc -w2 127.0.0.1 5672
(UNKNOWN) [127.0.0.1] 5672 (?) : Connection refused

您也可以在shell中执行相同操作:

% docker exec -it $(docker-compose ps -q socket) bash
root@b5da5207d344:~/src# nc -w2 127.0.0.1 5672
(UNKNOWN) [127.0.0.1] 5672 (?) : Connection refused
root@b5da5207d344:~/src# echo $?
1

最后,您可以只在第一个终端窗口中使用docker-compose up,而在另一个终端窗口中使用docker-compose logs -f。这将显示docker-compose-managed容器中的所有日志。