1. 程式人生 > >Docker常用命令2

Docker常用命令2

literal p地址 RM follow 行數 服務器 del ats 允許

categories: Docker Commands

Docker命令解讀系列文章將系統講解Docker使用的命令,方便大家學習Docker的基本操作。在寫這個系列文章的時候,主要參考了Docker官方的文檔,有些內容是直接的翻譯。原文檔地址 Docker Docs
轉載請註明出處

目錄:
- history
- events
- stats
- info
- logs
- port
- inspect
- top

history

Usage: docker history [OPTIONS] IMAGE

Show the history of an image

  -H, --human=true     Print sizes and dates in human readable format
  --help=false         Print usage
  --no-trunc=false     Don‘t truncate output
  -q, --quiet=false    Only show numeric IDs

docker history用於查看鏡像的歷史信息。包括鏡像的各層創建的時間,創建時的命令等。

events

Usage: docker events [OPTIONS]

Get real time events from the server

  -f, --filter=[]    Filter output based on conditions provided
  --help=false       Print usage
  --since=""         Show all events created since timestamp
  --until=""         Stream events until this timestamp

docker events命令用於查看Docker服務器中發生的實時事件。“事件”分為鏡像的和容器的兩種:
容器可以報告的事件有:

attach, commit, copy, create, destroy, die, exec_create, exec_start, export, kill, oom, pause, rename, resize, restart, start, stop, top, unpause

鏡像可以報告的事件有:

delete, import, pull, push, tag, untag

--since--until標識用於指定需要報告的事件的時間範圍,其值可以是UNIX時間戳,RFC3399定義的日期或者Golang的時間間隔字符串(30m

1h30m等等)
如果沒有指定--since標識,Docker將只會報告新產生的或正在進行的時間,就好像是從此刻開始監聽。

--filter標識用於事件的篩選,使用它,用戶可以只獲得某一個容器的狀態或某種類型的狀態。其格式為:--filter “event=create”|“container=test”--filter標識可以使用多次。
以下事實需要特別註意:
- 當使用--filter多次但指定的類型的都是一樣時,Docker將多個--filter之間認定為OR關系,例如:
bash
--filter container=588a23dac085 --filter container=a8f7720b8c22

會顯示容器588a23dac085或容器a8f7720b8c22的事件信息。
- 如果使用--filter多次但指定不同的類型,則被認作的AND的關系,例如:
--filter container=588a23dac085 --filter event=start會報告容器588a23dac085的start事件。

使用--filter可以指定以下四種類型的事件:
- container container=<name or id>
- event event=<event type>
- image image=<tag or id>
- label label=<key>orlabel=<key>=<value>

舉例:
直接執行docker events,表示監聽從現在起服務器上的所有可以報告的事件。當有事件發生時,就會顯示在執行docker events的控制端下。
在控制臺1運行docker events

$ docker events

另打開一個控制臺2,運行如下命令:

$ docker start 4386fb97867d
$ docker stop 4386fb97867d
$ docker stop 7805c1d35632

控制臺1就會輸出相應的事件:

2014-05-10T17:42:14.999999999Z07:00 4386fb97867d: (from ubuntu-1:14.04) start
2014-05-10T17:42:14.999999999Z07:00 4386fb97867d: (from ubuntu-1:14.04) die
2014-05-10T17:42:14.999999999Z07:00 4386fb97867d: (from ubuntu-1:14.04) stop
2014-05-10T17:42:14.999999999Z07:00 7805c1d35632: (from redis:2.8) die
2014-05-10T17:42:14.999999999Z07:00 7805c1d35632: (from redis:2.8) stop

stats

Usage: docker stats [OPTIONS] CONTAINER [CONTAINER...]

Display a live stream of one or more containers‘ resource usage statistics

  --help=false       Print usage
  --no-stream=false  Disable streaming stats and only pull the first result

docker stats命令用於顯示一個或多個容器實時的資源使用情況,包括CPU,內存使用量,網絡IO,磁盤IO等信息。
docker stats命令僅顯示正在運行的running容器的實時資源使用情況,不能顯示已經停止的容器的資源使用情況。
可以通過--no-stream標識關閉實時狀態,僅顯示當前時間點的狀態。

info

docker info用於顯示系統信息,主要有下面這些:

Containers: 1
Images: 32
Server Version: 1.9.0
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 34
 Dirperm1 Supported: true
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.19.0-33-generic
Operating System: Ubuntu 14.04.3 LTS
CPUs: 4
Total Memory: 7.493 GiB
Name: chy-station
ID: D3GB:PFOA:JJ2S:3RGI:7RHU:OUDR:C45L:JAMF:7PS7:PIEC:XPIL:T5TJ
WARNING: No swap limit support

logs

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

  -f, --follow=false        Follow log output
  --help=false              Print usage
  --since=                  Show logs since timestamp
  -t, --timestamps=false    Show timestamps
  --tail=all                Number of lines to show from the end of the logs

顯示某個容器的日誌信息,此命令僅支持使用json-filejournald日誌驅動的容器。
默認只顯示已經產生的日誌,通過指定-f(--follow跟隨)標識接著顯示以後的日誌。
--since標識通過指定一個時間戳顯示從某個時間點到現在的日誌信息。
--tail標識用來指定要顯示的日誌的行數,默認值是“all”,即顯示所有。
使用-t(--timestamps)標識會在每一行日誌加上時間戳。
關於docker logs的原理,這裏又一篇很好的文章可以參考:「Allen 談 Docker 系列」之 docker logs 實現剖析

port

docker port命令用於查看容器內所有內部端口與公共端口(宿主機端口)之間的映射關系,或根據某個內部端口查看對應的公共端口。
舉例:

$ docker port test
7890/tcp -> 0.0.0.0:4321
9876/tcp -> 0.0.0.0:1234
$ docker port test 7890/tcp
0.0.0.0:4321

inspect

Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]

Return low-level information on a container or image

  -f, --format=""         Format the output using the given go template
  --help=false            Print usage
  --type=container|image  Return JSON for specified type, permissible
                          values are "image" or "container"
  -s, --size=false        Display total file sizes if the type is container

返回與容器或鏡像相關的配置信息的josn字符串。默認返回所有信息,也可以通過--format標識指定要返回的部分信息。關於--format格式的信息請參考:text/format
下面是常用的格式:

$ docker inspect --format=‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}‘ $INSTANCE_ID   返回IP地址
$ docker inspect --format=‘{{.LogPath}}‘ $INSTANCE_ID 返回保存log信息文件的地址

top

顯示容器中正在運行的進程。

rename

Usage: docker rename [OPTIONS] OLD_NAME NEW_NAME

Rename a container

  --help=false    Print usage

docker rename命令用於對一個容器進行重命名。

版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/vchy_zhao/article/details/70238370

Docker常用命令2