Docker 常用命令

查看docker基础信息

[parallels@centos-linux-7 ~]$ docker info
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 1
Server Version: 1.13.0
Storage Driver: overlay
Backing Filesystem: extfs
Supports d_type: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 03e5862ec0d8d3b3f750e19fca3ee367e13c090e
runc version: 2f7393a47307a16f8cee44a37b262e8b81021e3e
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-327.28.3.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.793 GiB
Name: centos-linux-7.shared
ID: 5D43:ZX5P:OGGA:FCUI:PDB2:JGGK:2PLE:BZGJ:HFMK:LBSB:NZCY:6EEA
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: IPv4 forwarding is disabled
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

查看版本

[parallels@centos-linux-7 ~]$ docker version
Client:
Version: 1.13.0
API version: 1.25
Go version: go1.7.3
Git commit: 49bf474
Built: Tue Jan 17 09:55:28 2017
OS/Arch: linux/amd64

Server:
Version: 1.13.0
API version: 1.25 (minimum version 1.12)
Go version: go1.7.3
Git commit: 49bf474
Built: Tue Jan 17 09:55:28 2017
OS/Arch: linux/amd64
Experimental: false

搜索镜像

[parallels@centos-linux-7 ~]$ docker search learn/tutorial
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
learn/tutorial 24
danmaq/tutorial-learn-ping from learn/tutorial + install ping 0

拉取镜像

[parallels@centos-linux-7 ~]$ docker pull learn/tutorial
Using default tag: latest
latest: Pulling from learn/tutorial
271134aeb542: Pull complete
Digest: sha256:2933b82e7c2a72ad8ea89d58af5d1472e35dacd5b7233577483f58ff8f9338bd
Status: Downloaded newer image for learn/tutorial:latest

查看镜像

[parallels@centos-linux-7 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
learn/tutorial latest a7876479f1aa 3 years ago 128 MB

运行容器

语法

[parallels@centos-linux-7 ~]$ docker run --help

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

示例

[parallels@centos-linux-7 ~]$ docker run learn/tutorial echo 'hello world'
hello world

查看容器

[parallels@centos-linux-7 ~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[parallels@centos-linux-7 ~]$ docker ps -la
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ad1c8d1319fb learn/tutorial "echo 'hello world'" 29 seconds ago Exited (0) 28 seconds ago pedantic_easley

删除容器

[parallels@centos-linux-7 ~]$ docker rm ad1c8d1319fb
ad1c8d1319fb

在容器中安装ping命令

[parallels@centos-linux-7 ~]$ docker run learn/tutorial apt-get install -y ping
Reading package lists...
Building dependency tree...
The following NEW packages will be installed:
iputils-ping
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 56.1 kB of archives.
After this operation, 143 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main iputils-ping amd64 3:20101006-1ubuntu1 [56.1 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 56.1 kB in 1s (44.2 kB/s)
Selecting previously unselected package iputils-ping.
(Reading database ... 7545 files and directories currently installed.)
Unpacking iputils-ping (from .../iputils-ping_3%3a20101006-1ubuntu1_amd64.deb) ...
Setting up iputils-ping (3:20101006-1ubuntu1) ...

提交容器为新镜像

[parallels@centos-linux-7 ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9546af6773d9 learn/tutorial "apt-get install -..." 26 seconds ago Exited (0) 24 seconds ago peaceful_meninsky
[parallels@centos-linux-7 ~]$ docker commit 9546af6773d9 learn/ping
sha256:bc1cf22d42bd14a7d6f687a7f33ffe7ddf0a9e154bc2723c76c95adfa87b7a40
[parallels@centos-linux-7 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
learn/ping latest bc1cf22d42bd 3 seconds ago 139 MB
learn/tutorial latest a7876479f1aa 3 years ago 128 MB

使用新镜像运行容器并且运行ping命令

[parallels@centos-linux-7 ~]$ docker run learn/ping ping www.baidu.com
PING www.a.shifen.com (111.13.100.92) 56(84) bytes of data.
64 bytes from promote.cache-dns.local (111.13.100.92): icmp_req=1 ttl=51 time=33.0 ms
64 bytes from promote.cache-dns.local (111.13.100.92): icmp_req=2 ttl=51 time=34.2 ms
64 bytes from promote.cache-dns.local (111.13.100.92): icmp_req=3 ttl=51 time=32.7 ms
64 bytes from promote.cache-dns.local (111.13.100.92): icmp_req=4 ttl=51 time=33.4 ms
64 bytes from promote.cache-dns.local (111.13.100.92): icmp_req=5 ttl=51 time=33.8 ms
64 bytes from promote.cache-dns.local (111.13.100.92): icmp_req=6 ttl=51 time=35.4 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5009ms
rtt min/avg/max/mdev = 32.772/33.778/35.417/0.903 ms

查看容器具体信息

[parallels@centos-linux-7 ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd32dbc28d19 learn/ping "ping www.baidu.com" About a minute ago Exited (0) About a minute ago infallible_lamarr
9546af6773d9 learn/tutorial "apt-get install -..." 4 minutes ago Exited (0) 4 minutes ago peaceful_meninsky
[parallels@centos-linux-7 ~]$ docker inspect bd32dbc28d19
[
{
"Id": "bd32dbc28d193334acf617c4d2e3e064d41b82854402d23fc313f60a6dbe2e22",
"Created": "2017-01-22T14:55:46.36541004Z",
"Path": "ping",
"Args": [
"www.baidu.com"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-01-22T14:55:46.607808078Z",
"FinishedAt": "2017-01-22T14:55:52.522457658Z"
},
"Image": "sha256:bc1cf22d42bd14a7d6f687a7f33ffe7ddf0a9e154bc2723c76c95adfa87b7a40",
"ResolvConfPath": "/var/lib/docker/containers/bd32dbc28d193334acf617c4d2e3e064d41b82854402d23fc313f60a6dbe2e22/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/bd32dbc28d193334acf617c4d2e3e064d41b82854402d23fc313f60a6dbe2e22/hostname",
"HostsPath": "/var/lib/docker/containers/bd32dbc28d193334acf617c4d2e3e064d41b82854402d23fc313f60a6dbe2e22/hosts",
"LogPath": "/var/lib/docker/containers/bd32dbc28d193334acf617c4d2e3e064d41b82854402d23fc313f60a6dbe2e22/bd32dbc28d193334acf617c4d2e3e064d41b82854402d23fc313f60a6dbe2e22-json.log",
"Name": "/infallible_lamarr",
"RestartCount": 0,
"Driver": "overlay",
"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,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Name": "overlay",
"Data": {
"LowerDir": "/var/lib/docker/overlay/725d2e15038536c6c3f5d19dc9f8c6dc1427190bfc6fb0707987d33ea3631088/root",
"MergedDir": "/var/lib/docker/overlay/7de2e8880f34906ce23c9fc5ffb2bdf79d405aef60413ff9496b2708e79c5804/merged",
"UpperDir": "/var/lib/docker/overlay/7de2e8880f34906ce23c9fc5ffb2bdf79d405aef60413ff9496b2708e79c5804/upper",
"WorkDir": "/var/lib/docker/overlay/7de2e8880f34906ce23c9fc5ffb2bdf79d405aef60413ff9496b2708e79c5804/work"
}
},
"Mounts": [],
"Config": {
"Hostname": "bd32dbc28d19",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": true,
"AttachStderr": true,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [],
"Cmd": [
"ping",
"www.baidu.com"
],
"Image": "learn/ping",
"Volumes": {},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "e64ece3c35e67ef99ef4f9d09287feedb733801842e2077086e5c1f189014a9f",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": null,
"SandboxKey": "/var/run/docker/netns/e64ece3c35e6",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "7ba0c9ef7fc0eef2106f99cea82446fbf0180b1fd1f04b82eaadf082d27fa3d4",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": ""
}
}
}
}
]

将本地镜像上传到docker hub

[parallels@centos-linux-7 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
learn/ping latest bc1cf22d42bd 4 minutes ago 139 MB
learn/tutorial latest a7876479f1aa 3 years ago 128 MB
[parallels@centos-linux-7 ~]$ docker push learn/ping
The push refers to a repository [docker.io/learn/ping]
b618f13a45f8: Preparing
ee1ba0cc9b81: Preparing
denied: requested access to the resource is denied

删除镜像

[parallels@centos-linux-7 ~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
learn/ping latest bc1cf22d42bd 6 minutes ago 139 MB
learn/tutorial latest a7876479f1aa 3 years ago 128 MB
[parallels@centos-linux-7 ~]$ docker rmi bc1cf22d42bd
Untagged: learn/ping:latest
Deleted: sha256:bc1cf22d42bd14a7d6f687a7f33ffe7ddf0a9e154bc2723c76c95adfa87b7a40
Deleted: sha256:4ca762877ea63fb4bf9b378cf9591499f590c6e24f5dfa2a33db98e79865a979

创建交互式容器

heyongjindeMacBook-Pro:~ heyongjin$ docker run -i -t ubuntu /bin/bash
root@1ca4a3680ad0:/# whoami
root
root@1ca4a3680ad0:/# ls -la
total 72
drwxr-xr-x 34 root root 4096 Jan 22 15:37 .
drwxr-xr-x 34 root root 4096 Jan 22 15:37 ..
-rwxr-xr-x 1 root root 0 Jan 22 15:37 .dockerenv
drwxr-xr-x 2 root root 4096 Dec 13 20:24 bin
drwxr-xr-x 2 root root 4096 Apr 12 2016 boot
drwxr-xr-x 5 root root 380 Jan 22 15:37 dev
drwxr-xr-x 45 root root 4096 Jan 22 15:37 etc
drwxr-xr-x 2 root root 4096 Apr 12 2016 home
drwxr-xr-x 8 root root 4096 Sep 13 2015 lib
drwxr-xr-x 2 root root 4096 Dec 13 20:24 lib64
drwxr-xr-x 2 root root 4096 Dec 13 20:23 media
drwxr-xr-x 2 root root 4096 Dec 13 20:23 mnt
drwxr-xr-x 2 root root 4096 Dec 13 20:23 opt
dr-xr-xr-x 91 root root 0 Jan 22 15:37 proc
drwx------ 2 root root 4096 Dec 13 20:24 root
drwxr-xr-x 6 root root 4096 Dec 15 17:45 run
drwxr-xr-x 2 root root 4096 Dec 15 17:45 sbin
drwxr-xr-x 2 root root 4096 Dec 13 20:23 srv
dr-xr-xr-x 12 root root 0 Jan 22 15:37 sys
drwxrwxrwt 2 root root 4096 Dec 13 20:24 tmp
drwxr-xr-x 11 root root 4096 Dec 15 17:45 usr
drwxr-xr-x 13 root root 4096 Dec 15 17:45 var
root@1ca4a3680ad0:/#

退出交互式

Ctrl +P & Ctrl + Q

退出交互式后容器进入守护式,可以用过docker ps查看到容器

heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ca4a3680ad0 ubuntu "/bin/bash" 13 hours ago Up 3 hours sleepy_morse

重新进入交互式容器

heyongjindeMacBook-Pro:~ heyongjin$ docker attach 1ca4a3680ad0
root@1ca4a3680ad0:/#
root@1ca4a3680ad0:/#

重新启动停止的容器

heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
heyongjindeMacBook-Pro:~ heyongjin$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ca4a3680ad0 ubuntu "/bin/bash" 15 hours ago Exited (0) 5 seconds ago sleepy_morse
heyongjindeMacBook-Pro:~ heyongjin$ docker start 1ca4a3680ad0
1ca4a3680ad0
heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ca4a3680ad0 ubuntu "/bin/bash" 15 hours ago Up 2 seconds sleepy_morse

以交互的方式启动已经停止的容器

heyongjindeMacBook-Pro:~ heyongjin$ docker start -i 1ca4a3680ad0
root@1ca4a3680ad0:/#

创建命名的容器

heyongjindeMacBook-Pro:~ heyongjin$ docker run --name='myContainer1' -i -t ubuntu /bin/bash
root@0ad06eb760b8:/# pwd
/
root@0ad06eb760b8:/# heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ad06eb760b8 ubuntu "/bin/bash" 10 hours ago Up 26 seconds myContainer1
heyongjindeMacBook-Pro:~ heyongjin$

强制删除运行中的容器

heyongjindeMacBook-Pro:~ heyongjin$ docker rm -f 0ad06eb760b8
0ad06eb760b8

创建守护式容器

heyongjindeMacBook-Pro:~ heyongjin$ docker run --name deamonContainer1 -d ubuntu /bin/sh -c 'while true;do echo hello world;sleep 1;done'
a1755cc68a64da7f25538a22fa95df70bc731a96a080d804a2b3c22bed7cd443
heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1755cc68a64 ubuntu "/bin/sh -c 'while tr" 10 hours ago Up 15 seconds deamonContainer1
heyongjindeMacBook-Pro:~ heyongjin$

查看守护式容器的运行情况

heyongjindeMacBook-Pro:~ heyongjin$ docker logs -t -f deamonContainer1
2017-01-22T21:00:19.383552180Z hello world
2017-01-22T21:00:20.415617978Z hello world
2017-01-22T21:00:21.420570684Z hello world
2017-01-22T21:00:22.424738468Z hello world
2017-01-22T21:00:23.430845983Z hello world
^C

查看运行中容器的进程情况

heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1755cc68a64 ubuntu "/bin/sh -c 'while tr" 10 hours ago Up 2 minutes deamonContainer1
heyongjindeMacBook-Pro:~ heyongjin$ docker top deamonContainer1
PID USER TIME COMMAND
4248 root 0:00 /bin/sh -c while true;do echo hello world;sleep 1;done
4449 root 0:00 sleep 1
heyongjindeMacBook-Pro:~ heyongjin$

在运行中的容器内启动新的进程

docker exec [-d] [-i] [-t] 容器名 [COMMAND][ARG...]

heyongjindeMacBook-Pro:~ heyongjin$ docker top deamonContainer1
PID USER TIME COMMAND
4248 root 0:00 /bin/sh -c while true;do echo hello world;sleep 1;done
4449 root 0:00 sleep 1
heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1755cc68a64 ubuntu "/bin/sh -c 'while tr" 10 hours ago Up 7 minutes deamonContainer1
heyongjindeMacBook-Pro:~ heyongjin$ docker exec -i -t deamonContainer1 /bin/bash
root@a1755cc68a64:/# pwd
/
root@a1755cc68a64:/# heyongjindeMacBook-Pro:~ heyongjin$ docker top deamonContainer1
PID USER TIME COMMAND
4248 root 0:00 /bin/sh -c while true;do echo hello world;sleep 1;done
4786 root 0:00 /bin/bash
4837 root 0:00 sleep 1
heyongjindeMacBook-Pro:~ heyongjin$

停止守护式容器

docker stop 容器名 -- 发送停止信号

docker kill 容器名 -- 直接停止

heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1755cc68a64 ubuntu "/bin/sh -c 'while tr" 10 hours ago Up 11 minutes deamonContainer1
heyongjindeMacBook-Pro:~ heyongjin$ docker kill deamonContainer1
deamonContainer1
heyongjindeMacBook-Pro:~ heyongjin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
heyongjindeMacBook-Pro:~ heyongjin$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1755cc68a64 ubuntu "/bin/sh -c 'while tr" 10 hours ago Exited (137) 5 seconds ago deamonContainer1

存出镜像

docker save -o ~/Downloads/ubuntu_with_nginx.tar nginx/ubuntu

载入镜像

[parallels@centos-linux-7 Downloads]$ docker load --input ubuntu_with_nginx.tar
32d75bc97c41: Loading layer [==================================================>] 134.6 MB/134.6 MB
87f743c24123: Loading layer [==================================================>] 15.87 kB/15.87 kB
bbe6cef52379: Loading layer [==================================================>] 11.78 kB/11.78 kB
3d515508d4eb: Loading layer [==================================================>] 4.608 kB/4.608 kB
5972ebe5b524: Loading layer [==================================================>] 3.072 kB/3.072 kB
7ba9d224b295: Loading layer [==================================================>] 163.5 MB/163.5 MB
cfde65a31bb9: Loading layer [==================================================>] 1.14 MB/1.14 MB
Loaded image: nginx/ubuntu:latest
[parallels@centos-linux-7 Downloads]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx/ubuntu latest c9d29082edfc About an hour ago 290 MB
ubuntu latest f49eec89601e 2 weeks ago 129 MB

推荐阅读更多精彩内容