背景
整理一下常用的Docker命令
常用命令
幫助命令
1、查看版本
$ docker version
Client:
Version: 18.03.0-ce
API version: 1.37
Go version: go1.9.4
Git commit: 0520e24302
Built: Fri Mar 23 08:31:36 2018
OS/Arch: windows/amd64
Experimental: false
Orchestrator: swarm
Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea838
Built: Wed Nov 13 07:28:45 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
2、查看詳細信息
$ docker info
Containers: 2
Running: 1
Paused: 0
Stopped: 1
Images: 2
Server Version: 19.03.5
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 4.14.154-boot2docker
Operating System: Boot2Docker 19.03.5 (TCL 10.1)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 989.5MiB
Name: default
ID: XKXE:UYOL:PRQH:OYPM:DOWD:OBDF:4ZWT:FBKX:XAA7:QTCA:RQMD:SJYK
Docker Root Dir: /mnt/sda1/var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
provider=virtualbox
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://r7g9xefx.mirror.aliyuncs.com/
Live Restore Enabled: false
3、查看可用命令
$ docker --help
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default
"C:\\Users\\songzeceng\\.docker")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"C:\\Users\\songzeceng\\.docker\\machine\\machines\\default\\ca.pem")
--tlscert string Path to TLS certificate file (default
"C:\\Users\\songzeceng\\.docker\\machine\\machines\\default\\cert.pem")
--tlskey string Path to TLS key file (default
"C:\\Users\\songzeceng\\.docker\\machine\\machines\\default\\key.pem")
--tlsverify Use TLS and verify the remote (default true)
-v, --version Print version information and quit
Management Commands:
config Manage Docker configs
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
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
Run 'docker COMMAND --help' for more information on a command.
鏡像命令
1、列出本地鏡像
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
scrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GB
hello-world latest fce289e99eb9 14 months ago 1.84kB
選項:
1)、列出本地鏡像,含中間鏡像層
$ docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
scrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GB
hello-world latest fce289e99eb9 14 months ago 1.84kB
2)、只列出本地鏡像id
$ docker images -q
4ddd2efcb0df
fce289e99eb9
3)、列出本地鏡像和說明
$ docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
scrapinghub/splash latest sha256:01c89e3b0598e904fea184680b82ffe74524e83160f793884dc88d184056c49d 4ddd2efcb0df 8 weeks ago 2.17GB
hello-world latest sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752 fce289e99eb9 14 months ago 1.84kB
4)、列出本地鏡像和完整說明
$ docker images --digests --no-trunc
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
scrapinghub/splash latest sha256:01c89e3b0598e904fea184680b82ffe74524e83160f793884dc88d184056c49d sha256:4ddd2efcb0df639651607fe30ad532abf14e19c2c8f7d2b3c7f5eedfb7dbe4ce 8 weeks ago 2.17GB
hello-world latest sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752 sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e 14 months ago 1.84kB
2、查詢某個鏡像的名字
$ docker search tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 2654 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 75 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK]
bitnami/tomcat Bitnami Tomcat Docker Image 31 [OK]
kubeguide/tomcat-app Tomcat image for Chapter 1 28
consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 17 [OK]
cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 12 [OK]
....
其中official標籤下爲ok的是官方鏡像
3、下載鏡像
$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
68ced04f60ab: Pull complete
f9748e016a5c: Pull complete
da54b038fed1: Pull complete
6895ec5eb2c0: Pull complete
111ba0647b87: Pull complete
c1dce60f2f1a: Pull complete
702ec598d0af: Pull complete
4aba2fcbe869: Pull complete
b26bbbd533e6: Pull complete
7bd100a66c55: Pull complete
74149336419a: Pull complete
145ea1f01648: Pull complete
Digest: sha256:4a30434ce03d2fa396d0414f075ad9ca9b0b578f14ea5685e24dcbf789450a2c
Status: Downloaded newer image for mysql:latest
檢驗下載的鏡像
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 9b51d9275906 2 days ago 547MB
tomcat latest 4e7840b49fad 9 days ago 529MB
scrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GB
hello-world latest fce289e99eb9 14 months ago 1.84kB
4、刪除鏡像
$ docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
鏡像名(hello-world)後面可以加版本號(hello-world:3.2),默認最新
刪除後檢驗
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 9b51d9275906 2 days ago 547MB
tomcat latest 4e7840b49fad 9 days ago 529MB
scrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GB
選項:
1)、批量刪除
$ docker rmi -f mysql hello-world
...
2)、刪除全部
$ docker rmi -f $(docker images -qa)
Untagged: mysql:latest
Untagged: mysql@sha256:4a30434ce03d2fa396d0414f075ad9ca9b0b578f14ea5685e24dcbf789450a2c
Deleted: sha256:9b51d9275906910446e03bb86e16a0fe0051d6518ba7ae39c8780fc2323fd637
Deleted: sha256:3b5f20d41feb31513164dab347e9f5bea57dc9c644819aeea2dd5cbb7c7213bf
Deleted: sha256:0c9402cadbef6d34b3c8db27a0d793011c2de41cabc14354734752ec7bd325c1
...
檢驗之
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
5、提交容器副本,使之成爲一個新的鏡像
$ docker commit -a "szc" -m "tomcat of szc" 73832de15319 szc/tomcat:1.1
sha256:6d1afa72732ce3c920783e9ce354559ec64ffde81cb453c887640b17b2582b59
-a指定作者,-m指定提交信息,後面的73832de15319 爲要提交的容器id,最後szc/tomcat:1.1爲新鏡像的名字和版本
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
szc/tomcat 1.1 6d1afa72732c About a minute ago 529MB
tomcat latest 4e7840b49fad 9 days ago 529MB
centos latest 470671670cac 7 weeks ago 237MB
scrapinghub/splash latest 4ddd2efcb0df 8 weeks ago 2.17GB
$ docker run -it -p 7777:8080 szc/tomcat:1.1
:後面是tag值,不加表示latest
容器命令
1、新建並運行一個鏡像容器,並啓動一個交互式的僞終端
$ docker run -it centos
[root@92ef52a1b68f /]#
root@後面的92ef52a1b68f就是這個容器的id
選項:1)、以守護進程的方式運行容器,可以在run後面加上-d選項
$ docker run -d centos
1b4eef18d21a2c9de9c99e9dbca3b675fe9e1f8ad45f031954a8d453e0b4a949
$
此時如果用docker ps -a查看,會發現容器已經退出。因爲容器運行的如果不是那些一直掛起的命令,就會立刻退出。
2)、也可以指定端口號,在run後面加上-p選項,後面跟上容器在docker上的端口:容器在宿主機上的端口
$ docker run -it -p 8080:8080 tomcat
2、列出所有運行着的容器
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92ef52a1b68f centos "/bin/bash" 6 minutes ago Up 6 minutes exciting_johnson
選項:列出歷史上所有運行過的容器
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92ef52a1b68f centos "/bin/bash" 9 minutes ago Up 9 minutes exciting_johnson
a8c26efb021b fce289e99eb9 "/hello" 12 hours ago Exited (0) 12 hours ago exciting_austin
fb410e7667b4 scrapinghub/splash "python3 /app/bin/sp…" 21 hours ago Exited (255) 11 hours ago 0.0.0.0:8050->8050/tcp reverent_swartz
最後面的名字可以在啓動容器時使用--name指定,如下
$ docker run -i -t --name "szc" centos
[root@916e23b387b1 /]#
然後在另一Quickstart Terminal使用docker ps時輸出如下
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
916e23b387b1 centos "/bin/bash" 8 seconds ago Up 8 seconds szc
3、容器退出
1)、容器停止退出
[root@92ef52a1b68f /]# exit
exit
songzeceng@LAPTOP-GCPD2768 MINGW64 /d/develop/Docker Toolbox
$
命令行驗證
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92ef52a1b68f centos "/bin/bash" 10 minutes ago Exited (0) 49 seconds ago exciting_johnson
a8c26efb021b fce289e99eb9 "/hello" 12 hours ago Exited (0) 12 hours ago exciting_austin
fb410e7667b4 scrapinghub/splash "python3 /app/bin/sp…" 21 hours ago Exited (255) 11 hours ago 0.0.0.0:8050->8050/tcp reverent_swartz
2)、容器不停止退出:Ctrl+P+Q
此時再進去,可以用docker attach(不啓動新的進程)
[root@bbf512a4aa58 /]#
$ docker attach bbf512a4aa58
[root@bbf512a4aa58 /]#
或者使用docker exec
$ docker exec -t bbf512a4aa58 /bin/bash
[root@bbf512a4aa58 /]#
/bin/bash表示以root的身份進入容器終端
3)、如果只是執行一段命令,可以用exec(啓動新的進程)
$ docker exec -t bbf512a4aa58 ls -l /tmp
total 8
-rwx------ 1 root root 671 Jan 13 21:49 ks-script-_srt3u3c
-rwx------ 1 root root 1379 Jan 13 21:49 ks-script-gpqu_kuo
$
-t後面跟着容器id,再後面是要執行的命令
4、啓動容器
$ docker start 92ef52a1b68f
92ef52a1b68f
start後面是容器id或容器名
5、重啓容器
$ docker restart 92ef52a1b68f
92ef52a1b68f
restart後面是容器id或容器名
6、關閉容器
$ docker stop 92ef52a1b68f
92ef52a1b68f
或者
$ docker kill 92ef52a1b68f
92ef52a1b68f
7、刪除已停止的容器
$ docker rm 92ef52a1b68f
92ef52a1b68f
可以加上-f刪除正在運行着的容器;如果要刪除所有已停止的容器,可以輸入如下命令
$ docker rm $(docker ps -a)
1b4eef18d21a
916e23b387b1
exciting_austin
fb410e7667b4
...
8、查看容器日誌
$ docker logs -t -f bbf512a4aa58
[root@bbf512a4aa58 /]# ls -l
2020-03-08T01:42:04.553406000Z total 48
.....
2020-03-08T01:42:34.398428190Z [root@bbf512a4aa58 /]# pwd
2020-03-08T01:42:34.398477984Z /
其中-t表示顯示日期,-f表示不斷追加。下面是運行容器時執行的所有命令和輸出
9、查看容器內部細節
$ docker inspect bbf512a4aa58
[
{
"Id": "bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe",
"Created": "2020-03-08T01:41:24.391275544Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3571,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-03-08T01:41:24.584563557Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee",
"ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe/resolv.conf",
"HostnamePath": "/mnt/sda1/var/lib/docker/containers/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe/hostname",
"HostsPath": "/mnt/sda1/var/lib/docker/containers/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe/hosts",
"LogPath": "/mnt/sda1/var/lib/docker/containers/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe/bbf512a4aa589d193ca3808f496342a2eaa799dab636b8a7ee6323f6066985fe-json.log",
"Name": "/agitated_wilbur",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
30,
120
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/mnt/sda1/var/lib/docker/overlay2/b6a51489dbd3b188c7863e3e37cf344f20dbaa5528b5870f2d8e30ff3118a875-init/diff:/mnt/sda1/var/lib/docker/overlay2/956a46cec26f803c02d493bde34cd3b2ef9b779d2df20629036fe96324ac5eb2/diff",
"MergedDir": "/mnt/sda1/var/lib/docker/overlay2/b6a51489dbd3b188c7863e3e37cf344f20dbaa5528b5870f2d8e30ff3118a875/merged",
"UpperDir": "/mnt/sda1/var/lib/docker/overlay2/b6a51489dbd3b188c7863e3e37cf344f20dbaa5528b5870f2d8e30ff3118a875/diff",
"WorkDir": "/mnt/sda1/var/lib/docker/overlay2/b6a51489dbd3b188c7863e3e37cf344f20dbaa5528b5870f2d8e30ff3118a875/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "bbf512a4aa58",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200114",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS",
"org.opencontainers.image.created": "2020-01-14 00:00:00-08:00",
"org.opencontainers.image.licenses": "GPL-2.0-only",
"org.opencontainers.image.title": "CentOS Base Image",
"org.opencontainers.image.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "1b44bd7ba751a5be73ce9f497b59487718d62444ea468d2199994acc898bde9b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/1b44bd7ba751",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "a25e990586b1166973b72e22bc85b4c8eed851b12a88a6bdb180755670d09fdd",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ea6a244a17bafa7d0161cb9016496335d961f50b7380e6d730d8113fff1d9985",
"EndpointID": "a25e990586b1166973b72e22bc85b4c8eed851b12a88a6bdb180755670d09fdd",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
10、查看容器內運行着的進程
$ docker top bbf512a4aa58
UID PID PPID C STIME TTY TIME CMD
root 3571 3549 0 01:41 ? 00:00:00 /bin/bash
11、把文件從容器複製到主機上
$ docker cp bbf512a4aa58:/tmp/ks-script-gpqu_kuo /
格式爲docker cp 容器id:容器文件路徑 目標路徑