Docker Compose Up-在日志输出中删除服务名称

问题描述 投票:0回答:1

是否可以在docker-compose up的日志输出中删除服务名称?

我的日志当前看起来像这样:

serviceA        | log1
serviceB        | log2
serviceB        | log3
serviceA        | log4

但是我希望他们看起来像这样:

log1
log2
log3
log4

documentation指出,您可以为以下服务配置日志记录:

version: "3.7"
services:
  some-service:
    image: some-service
    logging:
      driver: "json-file"
      options:
        max-size: "200k"
        max-file: "10"

但是运行docker-compose up时,我找不到从日志中删除服务名称的任何选项。

更新

EchoMike444所建议,我已经尝试了以下命令:

docker-compose up --no-color 2>&1 | sed 's/^[^ ]*  | //'

带有以下泊坞窗组成文件:

version: "3"
services:
  consumer:
    image: debian:stretch-slim
    command:
      [
        "bash",
        "-c",
        "while ( true ) ; do echo $$(date) $$( hostname ) ; sleep $$(( $$RANDOM %4 )) ; done ",
      ]
  ideas:
    image: debian:stretch-slim
    restart: always
    command:
      [
        "bash",
        "-c",
        "while ( true ) ; do echo $$(date) $$( hostname ) ; sleep $$(( $$RANDOM %4 )) ; done ",
      ]

输出看起来像这样

ideas_1     | Sun Oct 20 05:56:23 UTC 2019 99295bdcbf2c
ideas_1     | Sun Oct 20 05:56:25 UTC 2019 99295bdcbf2c
Sun Oct 20 05:56:25 UTC 2019 dd45c5900159
ideas_1     | Sun Oct 20 05:56:27 UTC 2019 99295bdcbf2c
ideas_1     | Sun Oct 20 05:56:27 UTC 2019 99295bdcbf2c
Sun Oct 20 05:56:27 UTC 2019 dd45c5900159
Sun Oct 20 05:56:29 UTC 2019 dd45c5900159
ideas_1     | Sun Oct 20 05:56:30 UTC 2019 99295bdcbf2c

因此它适用于服务consumer,但不适用于ideas

docker logging docker-compose
1个回答
1
投票

当前您无法删除前缀,我在源代码中没有看到一个选项。

[docker-composepython编写,可以随时进行修补。

https://github.com/docker/compose/blob/master/compose/cli/log_printer.py

解决方法将:

 docker-compose up --no-color 2>&1 | sed 's/^[^ ]*  *| //' 

 docker-compose up -d
 docker-compose logs --no-color -f 2>&1 | sed 's/^[^ ]*  *| //'

我用这个很小的docker-compose.yaml测试了MacOS和Linux>

version: '3.7'
services:
  serviceA:
    image: debian:stretch-slim
    command: ["bash","-c","while ( true ) ; do echo $$(date) $$( hostname ) ; sleep $$(( $$RANDOM %4 )) ; done "]
  serviceB:
    image: debian:stretch-slim
    restart: always
    command: ["bash","-c","while ( true ) ; do echo $$(date) $$( hostname ) ; sleep $$(( $$RANDOM %4 )) ; done "]
© www.soinside.com 2019 - 2024. All rights reserved.