Docker學習筆記(三)——Docker經常使用命令

經常使用命令及其詳解

  1. 幫助命令
  • docker version:版本信息
  • docker info:查看docker詳細信息
  • docker --help:查看命令幫助信息
  1. 鏡像命令
  • docker images:列出本地全部的鏡像(含中間印象層)
參數 說明
REPOSITORY 表示鏡像的倉庫源
TAG 鏡像的標籤
IMAGE ID 鏡像ID
CREATED 鏡像建立時間
SIZE 鏡像大小

同一倉庫源能夠有多個 TAG,表明這個倉庫源的不一樣個版本,咱們使用 REPOSITORY:TAG 來定義不一樣的鏡像。linux

若是你不指定一個鏡像的版本標籤,例如你只使用 ubuntu,docker 將默認使用 ubuntu:latest 鏡像web

docker images [OPTIONS]經常使用OPTIONS說明:redis

  • -a:列出本地的全部鏡像,包括中間印象層
  • -q:只顯示鏡像的ID
  • --digests:顯示鏡像的摘要信息
  • --no-trunc:顯示完整的鏡像信息
  • docker search [某鏡像的名字]:查找某個鏡像docker

    注意:即便本地鏡像源配置的是阿里雲,可是這個search也是去docker hub上去查找,只不過在pull的時候從阿里雲上pull。shell

docker search [OPTIONS] [某鏡像的名字]經常使用OPTIONS說明:ubuntu

  • -s:列出收藏數(stars)很多於指定值的鏡像
  • --automated:只列出automated build類型的鏡像
  • --no-trunc:顯示完整的鏡像信息 注意:在使用-s時,會出現Flag --stars has been deprecated, use --filter=stars=3 instead。也就是說-s已經被啓用了,可使用 docker search --filter stars=x [某個鏡像的名字]來代替。一樣--automated可使用--filter=is-automated=true 來代替。
  • docker pull [鏡像名字][:TAG]:下載某個鏡像centos

    注意:docker pull tomcat 至關因而docker pull tomcat:latest,不加後面的TAG的話默認是latest tomcat

    pull redis example

  • docker rmi [某個鏡像的名字/ID]:刪除單個某個鏡像bash

    注意:若是守護進程 daemon正在使用該鏡像,就會出現如下錯誤: 服務器

    刪除出錯
    這種狀況下,可使用docker rmi -f [某個鏡像的名字/ID]來強制刪除。

刪除多個:docker rmi -f [鏡像名1][:TAG] [鏡像名2][:TAG]

刪除所有(組合命令):docker rmi -f $(docker images -qa)

說明:$()是shell腳本中的變量返回值的賦值

  • docker commit:提交容器副本,使之成爲一個新的鏡像

    此命令就是把一個鏡像作修改以後使用副本從新提交成一個新的鏡像。至關於改裝。具體的使用方式爲docker commit -m="提交的描述信息" -a="做者" [容器ID] [要建立的目標鏡像名]:[標籤名]

  1. 容器命令
  • docker run + [鏡像名稱]

docker run hello-world
根據下面打印的信息,能夠看出,運行 docker run首先會在本地containers中找hello-world容器,若是沒有,再在本地找hello-world的鏡像(images),若是仍是沒有,就去遠程鏡像源(遠程倉庫)去pull。把鏡像pull到本地以後再生產容器去運行。當第二次再運行該命令時,直接運行本地的hello-world容器。
第二次運行
docker run 流程

接下來,運行docker pull centos獲取centos的鏡像,下面部分所有使用該鏡像演示容器命令。

  • docker run [OPTIONS] IMAGE [COMMAND] [ARG...]:新建並啓動容器

    若是本地有,就直接啓動;若是本地沒有,先pull再run。

相關OPTIONS說明:

  • --name="容器新名字": 爲容器指定一個名稱,至關於別名
  • -d: 後臺運行容器,並返回容器ID,也即啓動守護式容器
  • -i以交互模式運行容器,一般與 -t 同時使用
  • -t爲容器從新分配一個僞輸入終端,一般與 -i 同時使用
  • -P: 隨機端口映射
  • -p: 指定端口映射,有如下四種格式       ip:hostPort:containerPort       ip::containerPort       hostPort:containerPort       containerPort

命令參數說明: docker run -it centos是啓動交互式容器,運行以後能夠看到下圖中shell的交互由docker變成了centos。

docker 裏面啓動 centos
docker run -d啓動後centos後再用docker ps -a 發現容器已經退出 很重要的要說明的一點: Docker容器後臺運行,就必須有一個前臺進程。容器運行的命令若是不是那些一直掛起的命令(好比運行top,tail),就是會自動退出的。

docker run -d centos

  • docker ps:列出全部當前正在運行的容器

上圖沒有指定name熟悉,那麼docker會隨機分配給其一個name,linux下輸入ps是查看那些進程在運行,輸入docker ps是查看那些容器在運行。

docker ps

相關OPTIONS說明(經常使用):

  • -a:列出當前全部正在運行的容器+歷史上運行過
  • -l:顯示最近建立的容器
  • -n:顯示最近n個建立的容器
  • -q :靜默模式,只顯示容器編號
  • --no-trunc :不截斷輸出
  • exit :容器中止退出

    ctrl + P +Q :容器不中止退出 當中止不退出時,使用docker ps能夠查看到centos容器正在運行

    中止不退出時centos的狀態

  • docker start [容器ID/容器名]:啓動容器

  • docker restart [容器ID/容器名]:從新啓動容器

  • docker stop[容器ID/容器名]:中止容器(正常關機)

  • docker rm[容器ID]:刪除已中止的容器

    使用docker ps -n 3查看運行過的近三個容器:

    查看近期的三個運行的容器
    能夠看到centos正在運行,還有其餘兩個已經中止。下面使用docker rm 刪除其中一個已中止的容器:
    刪除後再查看近期運行的容器
    能夠看到,ID爲b78aeba89c42 的容器已經被刪除。

  • docker rm -f $(docker ps -a -q):一次性刪除多個容器

    docker ps -a -q | xargs docker rm:一次性刪除多個容器(Linux管道)

  • docker logs -f -t -tail [tail numbers] [容器ID]:顯示容器的日誌

相關OPTIONS說明(經常使用):

  • -t:顯示時加入時間戳
  • -f:跟隨最新的日誌打印
  • -tail [numbers]:顯示最後的多少條
  • docker top [容器ID]:從查看容器內運行的進程

    docker top示例

  • docker inspect [容器ID]:從查看容器內部細節

  • docker attach [容器ID]:從新進入正在運行的容器中,並以命令行進行交互,配合上面啓動容器後ctrl + P + Q退出後從新進入。

    docker exec -it [容器ID] [shell command]:能夠不進入容器,直接在宿主機上運行容器裏的shell command,並返回結果。

    命令示例

能夠看到,在宿主機上執行centos 裏面的ls命令,返回centos的文件列表以後交互的shell仍是宿主機。

兩種命令的區別: attach是直接進入容器啓動終端命令,不會啓動新的進程,而exec是在容器中打開新的終端,並能夠啓動新的進程。也就是說,使用docker attach [容器ID]進入容器以後再使用exit退出,容器就關閉了。而使用docker exec -it [容器ID] /bin/bash以bash的方式進入容器以後使用exit退出關閉的只是一個進程,容器還在啓動。

attach示例
exec示例

  • docker cp [容器ID]:[容器內的路徑] [目的主機的路徑]:從容器中拷貝文件到主機
    docker cp示例

命令彙總(陽哥太貼心啦)

attach :Attach to a running container # 當前 shell 下 attach 鏈接指定運行鏡像

build :Build an image from a Dockerfile # 經過 Dockerfile 定製鏡像

commit :Create a new image from a container changes # 提交當前容器爲新的鏡像

cp : Copy files/folders from the containers filesystem to the host path #從容器中拷貝指定文件或者目錄到宿主機中

create :Create a new container # 建立一個新的容器,同 run,但不啓動容器

diff :Inspect changes on a container's filesystem # 查看 docker 容器變化

events :Get real time events from the server # 從 docker 服務獲取容器實時事件

exec :Run a command in an existing container # 在已存在的容器上運行命令

export :Stream the contents of a container as a tar archive # 導出容器的內容流做爲一個 tar 歸檔文件[對應 import ]

history :Show the history of an image # 展現一個鏡像造成歷史

images :List images # 列出系統當前鏡像

import :Create a new filesystem image from the contents of a tarball # 從tar包中的內容建立一個新的文件系統映像[對應export]

info :Display system-wide information # 顯示系統相關信息

inspect :Return low-level information on a container # 查看容器詳細信息

kill :Kill a running container # kill 指定 docker 容器

load :Load an image from a tar archive # 從一個 tar 包中加載一個鏡像[對應 save]

login :Register or Login to the docker registry server # 註冊或者登錄一個 docker 源服務器

logout :Log out from a Docker registry server # 從當前 Docker registry 退出

logs :Fetch the logs of a container # 輸出當前容器日誌信息

port :Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口對應的容器內部源端口

pause :Pause all processes within a container # 暫停容器

ps :List containers # 列出容器列表

pull Pull an image or a repository from the docker registry server # 從docker鏡像源服務器拉取指定鏡像或者庫鏡像

push :Push an image or a repository to the docker registry server # 推送指定鏡像或者庫鏡像至docker源服務器

restart Restart a running container # 重啓運行的容器

rm :Remove one or more containers # 移除一個或者多個容器

rmi :Remove one or more images # 移除一個或多個鏡像[無容器使用該鏡像纔可刪除,不然需刪除相關容器纔可繼續或 -f 強制刪除]

run :Run a command in a new container # 建立一個新的容器並運行一個命令

save :Save an image to a tar archive # 保存一個鏡像爲一個 tar 包[對應 load]

search :Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像

start :Start a stopped containers # 啓動容器

stop :Stop a running containers # 中止容器

tag :Tag an image into a repository # 給源中鏡像打標籤

top :Lookup the running processes of a container # 查看容器中運行的進程信息

unpause :Unpause a paused container # 取消暫停容器

version :Show the docker version information # 查看 docker 版本號

wait :Block until a container stops, then print its exit code # 截取容器中止時的退出狀態值

相關文章
相關標籤/搜索