2015-09-09 102 views
9

根據to the official Docker docs,有可能獲得容器的stdoutstderr輸出爲GELF messages,這是一種可以被例如, Graylog/Graylog2logstash使用docker-compose和GELF日誌驅動程序

這工作正常,當我從命令行手動運行我的容器。例如,

docker run --log-driver=gelf --log-opt gelf-address=udp://localhost:12201 busybox echo This is my message. 

將發送日誌消息給我Graylog2服務器上本地主機具有在端口配置了UDP監聽輸入12201.

現在,我想使用相同的日誌選項與docker-compose運行的,根據文檔should be possible in principle。但是,文檔不提任何日誌格式,但json-filesyslognone,當我有類似

my-container: 
    container_name: ... 
    build: ... 
    ports: ... 
    log_driver: "gelf" 
    log_opt: 
    gelf-address: "udp://localhost:12201" 

在我docker-compose.yml文件,然後docker-compose up失敗:

Traceback (most recent call last): 
    File "<string>", line 3, in <module> 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 39, in main 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 21, in sys_dispatch 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 27, in dispatch 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 24, in dispatch 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 59, in perform_command 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 495, in up 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.project", line 265, in up 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.service", line 369, in execute_convergence_plan 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.service", line 270, in create_container 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.service", line 643, in _get_container_create_options 
    File "/code/build/docker-compose/out00-PYZ.pyz/compose.service", line 656, in _get_container_host_config 
    File "/code/build/docker-compose/out00-PYZ.pyz/docker.utils.types", line 27, in __init__ 
ValueError: LogConfig.type must be one of (json-file, syslog, none) 

根據記錄,這在docker-compose 1.4.0和docker 1.8.1上,build d12ea79。

顯然,Docker和docker-compose在這裏的執行級別不同。我剛剛發現這已經解決並且包含在Github的Master branch上,參見 https://github.com/docker/compose/issues/1869https://github.com/docker/docker-py/pull/724

有沒有辦法從當前主分支中重新安裝docker-compose或手動將其添加到現有安裝中?我找不到提交到我主機上任何地方的文件...

+1

這個問題已經在'docker-py == 1.4.0'中修復了,並且會在docker-compose == 1.5.0版本中修復。 – dnephin

+1

是的,我也看到了。然而,'docker-compose == 1.5.0'將在[2015年10月13日](https://github.com/docker/compose/wiki/1.5.0-Milestone-Project-Page)上發佈,所以我可能要等一下。我試圖通過'pip install -U https:// github.com/docker/docker-py.git'從Github安裝最新的'master',然後'docker-compose --version'給出正確的輸出'1.5的3.0 dev'。但是,像docker-compose up這樣的所有docker-compose命令都會失敗,並引發ValueError(「沒有JSON對象可以被解碼」)。ValueError:沒有JSON對象可以被解碼。 對此有何想法? – Dirk

+0

它現在應該被修改爲現在的 – dnephin

回答

9

問題已修復。

我剛纔測試記錄使用到graylog2泊塢窗,構成1.5.0rc2

您可以使用此工作示例YAML嘗試:

example: 
container_name: example 
image: debian:wheezy 
    command: /bin/sh -c "while true; do date && echo "hello"; sleep 1; done" 
ports: 
    - "1234:1234" 
log_driver: "gelf" 
log_opt: 
    gelf-address: "udp://graylog.example.com:12201" 
    gelf-tag: "first-logs" 

當容器啓動時出現警告

example | WARNING: no logs are available with the 'gelf' log driver

但它似乎並不影響消息發送到graylog。

+0

不錯。現在也按照問題中所述順利運行。 – Dirk

+1

包含該警告是因爲您正在查看的輸出與「docker logs」相同的來源生成,該日誌不適用於離線日誌記錄驅動程序。 – allingeek

2

對於多克 - 撰寫V2服務,語法也略有改變,這是所有新的「記錄」部分現在:

version: "2" 

services: 
    example: 
    container_name: example 
    image: debian:wheezy 
    command: /bin/sh -c "while true; do date && echo "hello"; sleep 1; done" 
    ports: 
     - "1234:1234" 
    logging: 
     driver: "gelf" 
     options: 
     gelf-address: "udp://graylog.example.com:12201" 
     tag: "first-logs" 

一個重要的提示:地址爲gelf-address需要是外部地址服務器,因爲Docker通過主機的網絡解析這個地址。

相關問題