Docker-Compose中Docker Syslog标记中的映像名称,容器名称

时间:2016-05-03 18:57:56

标签: logging docker docker-compose syslog

由Docker Compose使用Syslog驱动程序创建的Docker容器生成的日志使用以下格式“docker / Container_ID”

May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]: 1:C 03 May 00:53:21.313 # Warning: no config file specified, using the default config. In order to specify a config file
use redis-server /path/to/redis.conf
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]:                _._
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]:           _.-``__ ''-._
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]:      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]:  .-`` .-```.  ```\/    _.,_ ''-._
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]: (    '      ,       .-`  | `,    )     Running in standalone mode
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]: |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]: |    `-._   `._    /     _.-'    |     PID: 1
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]:  `-._    `-._  `-./  _.-'    _.-'
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]: |`-._`-._    `-.__.-'    _.-'_.-'|
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]: |    `-._`-._        _.-'_.-'    |           http://redis.io
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]:  `-._    `-._`-.__.-'_.-'    _.-'
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]: |`-._`-._    `-.__.-'    _.-'_.-'|
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]: |    `-._`-._        _.-'_.-'    |
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]:  `-._    `-._`-.__.-'_.-'    _.-'
May  2 17:53:21 pe2enpmas300 docker/addfbda94974[1359]:      `-._    `-.__.-'    _.-'

如果您将这些日志带到Splunk等日志服务器上,这太糟糕了。

我们如何更改标签的名称?

  • 图片名称,包括版本
  • 基于服务名称的容器名称
  • 容器ID

那应该是这样的:

May  2 17:53:21 pe2enpmas300 docker/redis/redisservice/addfbda94974[1359]: 

2 个答案:

答案 0 :(得分:2)

正如https://docs.docker.com/engine/admin/logging/log_tags/所述,更改日志标记名称的方法可以使用那里定义的变量......所以,你可以使用以下内容:

  log_driver: syslog
  log_opt:
    tag: "{{.ImageName}}/{{.Name}}/{{.ID}}"

/ var / log / messages 的输出如下(或系统日志输出记录到的任何地方)

May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]: 1:C 03 May 18:40:47.818 # Warning: no config file specified, using the default config. In order
 to specify a config file use redis-server /path/to/redis.conf
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]:                _._
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]:           _.-``__ ''-._
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]:      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]:  .-`` .-```.  ```\/    _.,_ ''-._
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]: (    '      ,       .-`  | `,    )     Running in standalone mode
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]: |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]: |    `-._   `._    /     _.-'    |     PID: 1
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]:  `-._    `-._  `-./  _.-'    _.-'
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]: |`-._`-._    `-.__.-'    _.-'_.-'|
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]: |    `-._`-._        _.-'_.-'    |           http://redis.io
May  3 11:40:47 pe2enpmas300 docker/redis/npmoserver_redis_1/d14fd2ad2666[44739]:  `-._    `-._`-.__.-'_.-'    _.-'

答案 1 :(得分:0)

对于当前的docker-compose(1.24.1)语法,另一个答案已过时。要设置必要的日志记录,您需要在docker-compose.yaml文件中为服务使用以下内容:

logging:
  driver: syslog
  options:
    tag: "{{.ImageName}}/{{.Name}}/{{.ID}}"