從敲下docker logs開始理解docker日誌原理

參數說明

$ docker logs [OPTIONS] CONTAINER
  Options:
        --details        顯示更多的信息
    -f, --follow         跟蹤日誌輸出,最後一行爲當前時間戳的日誌
        --since string   顯示自具體某個時間或時間段的日誌
        --tail string    從日誌末尾顯示多少行日誌, 默認是all
    -t, --timestamps     顯示時間戳

使用方法

$ docker logs 26b12d17fefc
  nohup: appending output to 'nohup.out'
  nohup: appending output to 'nohup.out'
$ docker logs -t 26b12d17fefc
  2017-07-03T12:12:29.909710295Z nohup: appending output to 'nohup.out'
  2017-07-03T13:58:54.232003809Z nohup: appending output to 'nohup.out'
$ docker logs --tail 1 26b12d17fefc
  nohup: appending output to 'nohup.out'
$ docker logs -t --tail 1 26b12d17fefc
  2017-07-03T13:58:54.232003809Z nohup: appending output to 'nohup.out'
$ docker logs --since 30m 26b12d17fefc
  nohup: appending output to 'nohup.out'
$ docker logs -t --since="2017-07-03T13:58:54.232003809Z" 26b12d17fefc
  2017-07-03T13:58:54.232003809Z nohup: appending output to 'nohup.out'
$ docker logs -t --since="2017-07-03T12:12:29.909710295Z" 26b12d17fefc
  2017-07-03T12:12:29.909710295Z nohup: appending output to 'nohup.out'
  2017-07-03T13:58:54.232003809Z nohup: appending output to 'nohup.out'

容器日誌的輸出形式

  • stdout 標準輸出docker

  • stderr 標準錯誤json

  • 以json格式存放在容器對於到日誌文件中架構

docker日誌內容類型

  • docker自身運行時Daemon的日誌內容app

  • docker容器的日誌內容源碼分析

docker logs的實現原理

「Docker Daemon是Docker架構中一個常駐在後臺的系統進程,它在後臺啓動了一個Server,Server負責接受Docker Client發送的請求;接受請求後,Server經過路由與分發調度,找到相應的Handler來執行請求。–《Docker源碼分析》」日誌

當咱們輸入docker logs的時候會轉化爲Docker Client向Docker Daemon發起請求,Docker Daemon 在運行容器時會去建立一個協程(goroutine),綁定了整個容器內全部進程的標準輸出文件描述符。所以容器內應用的全部只要是標準輸出日誌,都會被 goroutine 接收,Docker Daemon會根據容器id和日誌類型讀取日誌內容,最終會輸出到用戶終端上而且經過json格式存放在/var/lib/docker/containers目錄下。code

容器日誌文件的生命週期

docker logs是跟隨容器而產生的,若是刪除了某個容器,相應的日誌文件也會隨着被刪除協程

相關文章
相關標籤/搜索