序
在上一篇基于Docker進(jìn)行Golang開發(fā)文章中對Docker進(jìn)行了安裝萧恕,并對如何用Docker進(jìn)行Golang開發(fā)進(jìn)行了描述勋颖,本文再對Docker命令進(jìn)行講解。
查看Docker命令
? tonny@tonny-pc ~ docker -h
Usage: docker [OPTIONS] COMMAND [arg...]
docker [ --help | -v | --version ]
A self-sufficient runtime for containers.
Options:
--config=~/.docker Location of client config files
-D, --debug Enable debug mode
-H, --host=[] Daemon socket(s) to connect to
-h, --help Print usage
-l, --log-level=info Set the logging level
--tls Use TLS; implied by --tlsverify
--tlscacert=~/.docker/ca.pem Trust certs signed only by this CA
--tlscert=~/.docker/cert.pem Path to TLS certificate file
--tlskey=~/.docker/key.pem Path to TLS key file
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Commands:
attach Attach 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 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 a container, image or task
kill Kill one or more running container
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
network Manage Docker networks
node Manage Docker Swarm nodes
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 a container
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
service Manage Docker services
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
swarm Manage Docker Swarm
tag Tag an image into a repository
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
volume Manage Docker volumes
wait Block until a container stops, then print its exit code
Run 'docker COMMAND --help' for more information on a command.
**attach Attach to a running container **
--將終端依附到容器上
- 運(yùn)行一個(gè)交互型容器
[root@localhost ~]# docker run -i -t centos /bin/bash
[root@f0a02b473067 /]#
- 在另一個(gè)窗口上查看該容器的狀態(tài)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a75f165ce6 centos "/bin/bash" 5 seconds ago Up 5 seconds cranky_mahavira
- 退出第一步中運(yùn)行的容器
[root@d4a75f165ce6 /]# exit
exit
- 查看該容器的狀態(tài)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a75f165ce6 centos "/bin/bash" 2 minutes ago Exited (0) 23 seconds ago cranky_mahavira
可見此時(shí)容器的狀態(tài)是Exited呛牲,那么议泵,如何再次運(yùn)行這個(gè)容器呢?可以使用docker start命令鬓催。
* 再次運(yùn)行該容器
``` bash
[root@localhost ~]# docker start cranky_mahavira
cranky_mahavira
- 再次查看該容器的狀態(tài)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a75f165ce6 centos "/bin/bash" 6 minutes ago Up 29 seconds cranky_mahavira
因?yàn)樵撊萜魇墙换バ偷姆嗡兀丝涛覀儼l(fā)現(xiàn)沒有具體的終端可以與之交互,這時(shí)可使用attach命令宇驾。
- 通過attach命令進(jìn)行交互
[root@localhost ~]# docker attach cranky_mahavira
[root@d4a75f165ce6 /]#
**build Build an image ****from a Dockerfile **
--通過Dockerfile創(chuàng)建鏡像
commit Create a ****new image from a container's changes
--通過容器創(chuàng)建本地鏡像
注意:如果是要push到docker hub中倍靡,注意生成鏡像的命名。
[root@localhost ~]# docker commit centos_v1 centos:v1
68ad49c999496cff25fdda58f0521530a143d3884e61bce7ada09bdc22337638
[root@localhost ~]# docker push centos:v1
You cannot push a "root" repository. Please rename your repository to <user>/<repo> (ex: <user>/centos)
用centos:v1就不行课舍,因?yàn)樗黳ush到docker hub中時(shí)菌瘫,是推送到相應(yīng)用戶下,必須指定用戶名布卡。譬如我的用戶名是ivictor,則新生成的本地鏡像命名為: docker push victor/centos:v1雇盖,其中v1是tag忿等,可不寫,默認(rèn)是latest崔挖。
**cp Copy file/folders from a container to a HOSTDIR or to STDOUT **
--在宿主機(jī)和容器之間相互COPY文件
cp的用法如下:
Usage:
docker cp [OPTIONS] CONTAINER:PATH LOCALPATH|-
docker cp [OPTIONS] LOCALPATH|- CONTAINER:PATH
**create Create a new container **
--創(chuàng)建一個(gè)新的容器贸街,注意庵寞,此時(shí),容器的status只是Created
diff Inspect changes on a container's filesystem
--查看容器內(nèi)發(fā)生改變的文件薛匪,以我的mysql容器為例
**events Get real time events from the server **--實(shí)時(shí)輸出Docker服務(wù)器端的事件捐川,包括容器的創(chuàng)建,啟動逸尖,關(guān)閉等古沥。
**exec Run a command in a running container **
--用于容器啟動之后,執(zhí)行其它的任務(wù)
通過exec命令可以創(chuàng)建兩種任務(wù):后臺型任務(wù)和交互型任務(wù)
#后臺型任務(wù):
[root@localhost ~]#docker exec -d cc touch 123 #其中cc是容器名
#交互型任務(wù):
[root@localhost ~]# docker exec -i -t cc /bin/bash
root@1e5bb46d801b:/# ls
123 bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
** export Export a container's filesystem as a tar archive**
--將容器的文件系統(tǒng)打包成tar文件
有兩種方式:
[root@localhost ~]# docker export -o mysqldb1.tar mysqldb
[root@localhost ~]# docker export mysqldb > mysqldb.tar
**history Show the history of an image **
--顯示鏡像制作的過程娇跟,相當(dāng)于dockfile
images List images
--列出本機(jī)的所有鏡像
**import Import the contents ****from a tarball to create a filesystem image **
--根據(jù)tar文件的內(nèi)容新建一個(gè)鏡像岩齿,與之前的export命令相對應(yīng)
**info Display system-wide information **
--查看docker的系統(tǒng)信息
**inspect Return low-level information on a container or image **
--用于查看容器的配置信息,包含容器名苞俘、環(huán)境變量盹沈、運(yùn)行命令、主機(jī)配置吃谣、網(wǎng)絡(luò)配置和數(shù)據(jù)卷配置等乞封。
**kill Kill a running container **
--強(qiáng)制終止容器
關(guān)于stop和kill的區(qū)別,docker stop命令給容器中的進(jìn)程發(fā)送SIGTERM信號岗憋,默認(rèn)行為是會導(dǎo)致容器退出肃晚,當(dāng)然, 容器內(nèi)程序可以捕獲該信號并自行處理澜驮,例如可以選擇忽略陷揪。而docker kill則是給容器的進(jìn)程發(fā)送SIGKILL信號,該信號將會使容器必然退出杂穷。
**load Load an image from a tar archive or STDIN **
--與下面的save命令相對應(yīng)悍缠,將下面sava命令打包的鏡像通過load命令導(dǎo)入
**login Register or log in to a Docker registry
**--登錄到自己的Docker register,需有Docker Hub的注冊賬號
**logout Logout from a Docker registry **
--退出登錄
**logs Fetch the logs of a container **
--用于查看容器的日志耐量,它將輸出到標(biāo)準(zhǔn)輸出的數(shù)據(jù)作為日志輸出到docker logs命令的終端上飞蚓。常用于后臺型容器
**pause Pause all processes within a container **
--暫停容器內(nèi)的所有進(jìn)程, 此時(shí)廊蜒,通過docker stats可以觀察到此時(shí)的資源使用情況是固定不變的趴拧, 通過docker logs -f也觀察不到日志的進(jìn)一步輸出。
**port List port mappings or a specific mapping for the CONTAINER **
--輸出容器端口與宿主機(jī)端口的映射情況
ps List containers
--列出所有容器山叮,其中docker ps用于查看正在運(yùn)行的容器著榴,ps -a則用于查看所有容器。
**pull Pull an image or a repository from a registry **
--從docker hub中下載鏡像
**push Push an image or a repository to a registry **
--將本地的鏡像上傳到docker hub中 前提是你要先用docker login登錄上屁倔,不然會報(bào)錯(cuò)誤脑又。
**rename Rename a container **
--更改容器的名字
restart Restart a running container
--重啟容器
**rm Remove one or more containers **
--刪除容器
注意,不可以刪除一個(gè)運(yùn)行中的容器,必須先用docker stop或docker kill使其停止问麸。 當(dāng)然可以強(qiáng)制刪除往衷,必須加-f參數(shù) 如果要一次性刪除所有容器,可使用 docker rm -f docker ps -a -q
严卖,其中席舍,-q指的是只列出容器的ID。
rmi Remove one or more images
--刪除鏡像
run Run a command in a new container
--讓創(chuàng)建的容器立刻進(jìn)入運(yùn)行狀態(tài)哮笆,該命令等同于docker create創(chuàng)建容器后再使用docker start啟動容器
** save Save an image(s) to a tar archive **
--將鏡像打包来颤,與上面的load命令相對應(yīng)
search Search the Docker Hub for images
--從Docker Hub中搜索鏡像
**start Start one or more stopped containers **
--啟動容器
**stats Display a live stream of container(s) resource usage statistics **
--動態(tài)顯示容器的資源消耗情況,包括:CPU疟呐、內(nèi)存脚曾、網(wǎng)絡(luò)I/O
** stop Stop a running container **
--停止一個(gè)運(yùn)行的容器
** tag Tag an image into a repository **
--對鏡像進(jìn)行重命名
**top Display the running processes of a container **
--查看容器中正在運(yùn)行的進(jìn)程
**unpause Unpause all processes within a container **
--恢復(fù)容器內(nèi)暫停的進(jìn)程,與pause參數(shù)相對應(yīng)启具。
**version Show the Docker version information **
--查看docker的版本
wait Block until a container stops, then print its exit code
--捕捉容器停止時(shí)的退出碼
執(zhí)行此命令后本讥,該命令會“hang”在當(dāng)前終端,直到容器停止鲁冯,此時(shí)拷沸,會打印出容器的退出碼。