Docker常用命令
Docker幫助命令
- docker version:查看docker版本
- docker info:顯示docker系統信息
- docker --help:查看幫助
命令格式:docker [OPTIONS] COMMAND,中括號中為可選項
常用命令
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Docker鏡像命令
-
docker images:列出本地宿主機上的鏡像
REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 5 months ago 13.3kB
REPOSITORY :鏡像倉庫源
TAG:鏡像標簽(不指定鏡像版本標簽,默認為latest)
IMAGE ID:鏡像ID
CREATED:鏡像創建時間
SIZE:鏡像大小
OPTIONS:
- q:只顯示鏡像ID
- a:列出本地所有鏡像
- digiest:顯示鏡像摘要信息(類似于備注)
- no-trunc:顯示鏡像完整信息
-
docker search xxx:根據名字從Docker Hub上查詢鏡像
docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 2758 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 79 [OK] dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 54 [OK] bitnami/tomcat Bitnami Tomcat Docker Image 35 ...
OPIONS:
- no-trunc:顯示完整鏡像描述
- s:列出收藏數不少于指定值的鏡像
- automated:只列出automated buid類型的鏡像
docker pull xxx:下載鏡像
如:下載tomcat鏡像docker pull tomcat
,若不指定版本號,則默認為latest版本-
docker rmi xxx:刪除單個或多個鏡像
- 刪除單個:docker rmi -f 鏡像ID
- 刪除多個:docker rmi -f [鏡像名1] [鏡像名2] ...
- 刪除所有:docker rmi -f $(docker images -qa)
Docker容器命令
-
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]:新建并啟動容器
OPTIONS:
- --name="containerName":為容器指定一個名稱
- -d:后臺運行容器,并返回容器ID,即啟動守護式容器
- -i:以交互模式運行容器,通常與-t同時使用
- -t:為容器重新分配一個偽輸入終端
- -P:隨機端口映射
- -p:指定端口映射,有以下四種格式
- ip:hostPort:containerPort
- ip:containerPort
- hostPort:containerPort
- containerPort
如:啟動一個centos鏡像
1.下載centos鏡像:
docker pull centos
2.下載完成docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 831691599b88 2 weeks ago 215MB redis latest 235592615444 3 weeks ago 104MB tomcat latest 2eb5a120304e 3 weeks ago 647MB hello-world latest bf756fb1ae65 6 months ago 13.3kB
3.以交互模式啟動centos鏡像:
docker run -it 831691599b88
啟動成功
docker run -it 831691599b88 [root@3910ffed2621 /]
此時相當于以ID為831691599b88的centos鏡像為模板生成的一個容器,3910ffed2621即為當前容器ID
-
docker ps:查看docker中正在運行的鏡像
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3910ffed2621 centos "/bin/bash" 11 seconds ago Up 11 seconds nice_banzai
可以看到若沒有指定容器名稱,則會為容器默認分配一個名稱
OPTIONS:
- -a:列出當前所有正在運行容器+歷史運行過的
- -l:顯示最近創建的容器
- -n:顯示最近n個創建的容器
- -q:靜默模式,只顯示容器編號
- --no-trunc:不截斷輸出
-
退出容器
- exit:退出并關閉容器
- Ctrl+P+Q:退出并保持容器
docker start + 容器名或ID:啟動容器
docker stop:停止容器
docker kill:強制停止容器
-
docker rm:刪除已停止的容器(docker rmi為刪除鏡像)
一次刪除多個容器- docker rm -f $(docker ps -a -q)
- docker ps -a -q | xargs docker rm
-
docker run -d:啟動守護式容器
如輸入docker run -d
會輸出一下結果docker run -d centos 74756444790f595a3cad2fcd0372296d3f23a9e3b7ce7c6590099a51bd60ced8
說明容器啟動成功,但輸入docker ps不能看到正在運行的容器,利用docker ps -a查看,可以看到容器已經被創建并退出。因為Docker容器后臺運行,就必須要有一個前臺進程。
容器運行的命令如果不是那些一直掛起的命令(如運行top、tail),就會自動退出。這是docker的機制問題,以nginx為例,正常情況下,我們配置啟動服務器只需要啟動相應的service即可。例如service nginx start。
-
查看容器日志:docker logs -f -t --tail containerID
- -t:加入時間戳
- -f:跟隨最新的日志打印
- --tail:顯示最后多少條
查看容器內運行的進程: docker top containerID
docker top 8da7c54a4da9
UID PID PPID C STIME TTY TIME CMD
root 24821 24797 0 16:47 ? 00:00:00 /bin/sh -c while true;do echo hello centos;sleep 2;done
root 25110 24821 0 16:50 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2
-
查看容器內部細節:docker inspect containerID
以一個json字符串的形式描述正在運行的容器。
-
進入正在運行的容器,并以命令行形式交互:
- docker exec -it containerID bashShell:在容器中打開新的終端,并可以啟動新的進程。
- docker attach containerID:直接進入容器命令的終端,不啟動新進程。
docker attach 7f580824ab84
[root@7f580824ab84 /]# ls -l /tmp
total 8
-rwx------ 1 root root 1379 Jun 11 02:35 ks-script-585nin8f
-rwx------ 1 root root 671 Jun 11 02:35 ks-script-z6zw_bhq
docker exec -t 7f580824ab84 ls -l /tmp
total 8
-rwx------ 1 root root 1379 Jun 11 02:35 ks-script-585nin8f
-rwx------ 1 root root 671 Jun 11 02:35 ks-script-z6zw_bhq
attach是先進入容器再執行命令行指令,而exec可以直接在容器外部輸入指令,只返回指令的運行結果,不用啟
動新的終端。
(docker exec -it containerID /bin/bash 可以達到與docker attach相同的效果。)
- 從容器內拷貝文件到主機上:docker cp containerID: virtualPath localPath