鏡像的基本命令
幫助命令
docker version # 顯示 Docker 版本信息。
docker info # 顯示 Docker 系統信息,包括鏡像和容器數
docker --help # 幫助
幫助文檔:https://docs.docker.com/engine/reference/commandline/docker/
鏡像命令
docker images(查看主機上的鏡像)
# 列出本地主機上的鏡像
[root@xiaoyequ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 5 months ago 13.3kB
# 解釋
# REPOSITORY 鏡像的倉庫源
# TAG 鏡像的標籤
# IMAGE ID 鏡像的ID
# CREATED 鏡像創建時間
# SIZE 鏡像大小
# 同一個倉庫源可以有多個 TAG,代表這個倉庫源的不同版本,我們使用REPOSITORY:TAG 定義不同的鏡像,如果你不定義鏡像的標籤版本,docker將默認使用 lastest 鏡像
# 可選項
-a: 列出本地所有鏡像
-q: 只顯示鏡像id
--digests: 顯示鏡像的摘要信息
docker search(搜索鏡像)
瀏覽器搜索:https://hub.docker.com
# 搜索鏡像
[root@xiaoyequ ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9587 [OK]
# docker search 某個鏡像的名稱 對應DockerHub倉庫中的鏡像
# 可選項
--filter=stars=50 : 列出收藏數不小於 50 的鏡像。
docker pull(下載鏡像)
# 下載鏡像
[root@xiaoyequ ~]# docker pull mysql
Using default tag: latest # 不寫tag,默認是latest(最新版本)
latest: Pulling from library/mysql
afb6ec6fdc1c: Pull complete # 分層下載
0bdc5971ba40: Pull complete
97ae94a2c729: Pull complete
f777521d340e: Pull complete
1393ff7fc871: Pull complete
a499b89994d9: Pull complete
7ebe8eefbafe: Pull complete
597069368ef1: Pull complete
ce39a5501878: Pull complete
7d545bca14bf: Pull complete
211e5bb2ae7b: Pull complete
5914e537c077: Pull complete
Digest: sha256:a31a277d8d39450220c722c1302a345c84206e7fd4cdb619e7face046e89031d # 簽名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真實位置
指定版本下載
# 指定版本下載
[root@xiaoyequ ~]# docker pull mysql:5.7
docker rmi(刪除鏡像)
# 刪除鏡像
docker rmi -f 鏡像id # 刪除單個
docker rmi -f 鏡像id 鏡像id 鏡像id # 刪除多個
docker rmi -f $(docker images -qa) # 刪除全部
容器的基本命令
說明:有鏡像才能創建容器,我們這裏使用 centos 的鏡像來測試,就是虛擬一個 centos
docker pull centos
docker run(新建容器並啓動)
# 命令
docker run [OPTIONS] IMAGE [COMMAND][ARG...]
# 常用參數說明
--name="Name" # 給容器指定一個名字
-d # 後臺方式運行容器,並返回容器的id!
-i # 以交互模式運行容器,通過和 -t 一起使用
-t # 給容器重新分配一個終端,通常和 -i 一起使用
-P # 隨機端口映射(大寫)
-p # 指定端口映射(小寫),一般可以有四種寫法
ip:hostPort:containerPort(ip:主機端口:容器端口)
ip::containerPort
hostPort:containerPort (常用)
containerPort
# 測試
[root@xiaoyequ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 470671670cac 4 months ago 237MB
# 使用centos進行用交互模式啓動容器,在容器內執行/bin/bash命令!
[root@xiaoyequ ~]# docker run -it centos /bin/bash
[root@be4b54c57337 /]# ls # 注意地址,已經切換到容器內部了!(類似於一個小型的linux)
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@be4b54c57337 /]# exit # 使用 exit 退出容器
exit
[root@xiaoyequ ~]#
docker ps(列出所有運行的容器)
# 命令
docker ps [OPTIONS]
# 常用參數說明
-a # 列出當前所有正在運行的容器 + 歷史運行過的容器
-l # 顯示最近創建的容器
-n=? # 顯示最近n個創建的容器
-q # 靜默模式,只顯示容器編號。
exit(退出容器)
exit # 容器停止退出
ctrl+P+Q # 容器不停止退出
啓動停止容器
docker start (容器id or 容器名) # 啓動容器
docker restart (容器id or 容器名) # 重啓容器
docker stop (容器id or 容器名) # 停止容器
docker kill (容器id or 容器名) # 強制停止容器
docker rm(刪除容器)
docker rm 容器id # 刪除指定容器
docker rm -f $(docker ps -a -q) # 刪除所有容器
docker ps -a -q|xargs docker rm # 刪除所有容器
常用其他命令
docker run -d(後臺啓動容器)
# 命令
docker run -d 容器名
# 例子
[root@xiaoyequ ~]# docker run -d centos # 啓動centos,使用後臺方式啓動
ee4a238a51ed3bd7131bd595c2ac716692b5f3da87466ad883a20d5d4246d47a
# 問題: 使用docker ps 查看,發現容器已經退出了!
# 解釋:Docker容器後臺運行,就必須有一個前臺進程,容器運行的命令如果不是那些一直掛起的命令,就會自動退出。
# 比如,你運行了nginx服務,但是docker前臺沒有運行應用,這種情況下,容器啓動後,會立即自殺,因爲他覺得沒有程序了,所以最好的情況是,將你的應用使用前臺進程的方式運行啓動。
docker logs(查看日誌)
# 命令
docker logs -f -t --tail 容器id
# 例子:我們啓動 centos,並編寫一段腳本來測試玩玩!最後查看日誌
[root@xiaoyequ ~]# docker run -d centos /bin/sh -c "while true;do echo xiaoyequ;sleep 1;done"
cbfe6f6cc92321101e63353c76d3c79ee032f271d68fac3c52c0bf3c8846cdd1
[root@xiaoyequ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbfe6f6cc923 centos "/bin/sh -c 'while t…" 8 seconds ago Up 7 seconds condescending_sanderson
# -t 顯示時間戳
# -f 打印最新的日誌
# --tail 數字 顯示多少條!
[root@xiaoyequ ~]# docker logs -tf --tail 10 cbfe6f6cc923
2020-06-06T09:59:57.634232481Z xiaoyequ
2020-06-06T09:59:58.636842507Z xiaoyequ
2020-06-06T09:59:59.639316083Z xiaoyequ
2020-06-06T10:00:00.641746110Z xiaoyequ
2020-06-06T10:00:01.644386953Z xiaoyequ
2020-06-06T10:00:02.647106350Z xiaoyequ
2020-06-06T10:00:03.649668294Z xiaoyequ
2020-06-06T10:00:04.652318092Z xiaoyequ
2020-06-06T10:00:05.655020539Z xiaoyequ
docker top(查看容器中運行的進程信息,支持 ps 命令參數。)
# 命令
docker top 容器id
[root@xiaoyequ ~]# docker top cbfe6f6cc923
UID PID PPID C STIME TTY TIME CMD
root 11320 11303 0 17:59 ? 00:00:00 /bin/sh -c while true;do echo xiaoyequ;sleep 1;done
root 11775 11320 0 18:06 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
docker inspect(查看容器/鏡像的元數據)
# 命令
docker inspect 容器id
# 測試
[root@xiaoyequ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dab9935c4259 centos "/bin/bash" About a minute ago Up About a minute frosty_sutherland
[root@xiaoyequ ~]# docker inspect dab9935c4259
[
{
# 完整的id,這裏上面的容器id,就是截取的這個id前幾位!
"Id": "dab9935c42598eef3357a4963adec4ca8d1436ff9abd3f10bb1bc7b2f91c35f1",
"Created": "2020-06-06T10:10:18.08897757Z",
"Path": "/bin/bash",
"Args": [],
# 狀態
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12171,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-06-06T10:10:18.399953427Z",
"FinishedAt": "0001-01-01T00:00:00Z"
}
......
}
]
docker exec/attach(進入正在運行的容器)
# 命令1(-it 用交互模式進入容器)
docker exec -it 容器id bashShell
# 測試1
[root@xiaoyequ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dab9935c4259 centos "/bin/bash" 4 minutes ago Up 4 minutes frosty_sutherland
[root@xiaoyequ ~]# docker exec -it dab9935c4259 /bin/bash
[root@dab9935c4259 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:10 pts/0 00:00:00 /bin/bash
root 14 0 0 10:14 pts/1 00:00:00 /bin/bash
root 27 14 0 10:15 pts/1 00:00:00 ps -ef
# 命令2
docker attach 容器id
# 測試2
[root@xiaoyequ ~]# docker attach 0f3e219a7f65
[root@0f3e219a7f65 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:29 pts/0 00:00:00 /bin/bash
root 14 1 0 10:32 pts/0 00:00:00 ps -ef
區別:
exec :是在容器中打開新的終端,並且可以啓動新的進程 attach :直接進入容器啓動命令的終端,不會啓動新的進程
docker cp(從容器內拷貝文件到主機上)
# 命令
docker cp 容器id:容器內路徑 目的主機路徑
# 測試
# 容器內執行,創建一個文件測試
[root@xiaoyequ home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f3e219a7f65 centos "/bin/bash" 4 hours ago Up 4 hours stoic_galois
[root@xiaoyequ home]# docker attach 0f3e219a7f65
[root@0f3e219a7f65 /]# cd /home
[root@0f3e219a7f65 home]# ls
[root@0f3e219a7f65 home]# touch xiaoyequ.java
[root@0f3e219a7f65 home]# ls
xiaoyequ.java
[root@0f3e219a7f65 home]# exit
exit
# linux複製查看,是否複製成功
[root@xiaoyequ home]# docker cp 0f3e219a7f65:home/xiaoyequ.java /home/
[root@xiaoyequ home]# ls
xiaoyequ.java
小結
常用命令
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 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 # 從當前 Dockerregistry 退出
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 # 截取容器停止時的退出狀態值