Docker常用命令

Docker帮助命令

socker version:版本信息

1
2
3
4
5
6
7
8
9
10
11
[dw@dk ~]$ docker version
Client: Docker Engine - Community
Version: 20.10.6
API version: 1.41
Go version: go1.13.15
Git commit: 370c289
Built: Fri Apr 9 22:45:33 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version: dial unix /var/run/docker.sock: connect: permission denied

docker info:更加详细的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
[dw@dk ~]$ sudo docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
scan: Docker Scan (Docker Inc.)

Server:
Containers: 1 # 有多少个容器
Running: 0
Paused: 0
Stopped: 1
Images: 1 # 镜像个数
Server Version: 20.10.6
Storage Driver: overlay2 # 存储的驱动在哪
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
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: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc version: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1127.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.62GiB # 可用内存
Name: dk # 宿主机名字
ID: JCYC:SUYH:7WU6:HM6B:EJQP:BHJJ:TISK:5KVS:E3XR:3AE6:DIOG:KAC3
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://kct8gp4w.mirror.aliyuncs.com/
Live Restore Enabled: false

WARNING: IPv4 forwarding is disabled
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

docker --help

类似Linux里的max 命令(解释命令作用及参数,q退出)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
[dw@dk ~]$ docker --help

Usage: docker [OPTIONS] COMMAND # 命令用法

A self-sufficient runtime for containers

Options:
--config string Location of client config files (default "/home/dw/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and
default context set with "docker context use")
-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 "/home/dw/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/dw/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/dw/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit

Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.5.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc.)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
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.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/

Docker镜像命令

Docker images列出本地主机上的镜像

1
2
3
[dw@dk ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
  • REPOSITORY : 表示镜像的仓库源
  • TAG : 镜像标签,用于标识镜像版本
  • IMAGE ID:镜像ID
  • CREATED :镜像创建时间
  • SIZE :镜像大小

同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。

Docker images [Options]

  • -a :列出本地所有的镜像(含中间映像层)[centos7下该命令不显示中间映像层]
  • -q:只显示镜像ID。
  • --digests :显示镜像的摘要信息
  • --no-trunc :显示完整的镜像信息
1
2
3
4
5
6
7
8
9
[dw@dk ~]$ sudo docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB

[dw@dk ~]$ sudo docker images -q
d1165f221234

[dw@dk ~]$ sudo docker images -qa # 返回当前所有镜像的id
d1165f221234

docker search [某个镜像名字]

查找镜像。查是从官网查,拉取从阿里云拉取(前面配置了)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[dw@dk ~]$ sudo docker search tomcat # 搜索tomcat镜像
NAME DESCRIPTION STARS OFFICIAL(官方版) AUTOMATED(自动构建)
tomcat Apache Tomcat is an open source implementati… 3042 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 88 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 57 [OK]
bitnami/tomcat Bitnami Tomcat Docker Image 37 [OK]
kubeguide/tomcat-app Tomcat image for Chapter 1 30
consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 18 [OK]
cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 13 [OK]
arm32v7/tomcat Apache Tomcat is an open source implementati… 11
maluuba/tomcat7-java8 Tomcat7 with java8. 6
rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl… 6 [OK]
unidata/tomcat-docker Security-hardened Tomcat Docker container. 5 [OK]
arm64v8/tomcat Apache Tomcat is an open source implementati… 3
amd64/tomcat Apache Tomcat is an open source implementati… 2
oobsri/tomcat8 Testing CI Jobs with different names. 2
jelastic/tomcat An image of the Tomcat Java application serv… 2
cfje/tomcat-resource Tomcat Concourse Resource 2
ppc64le/tomcat Apache Tomcat is an open source implementati… 1
picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAGER… 1 [OK]
camptocamp/tomcat-logback Docker image for tomcat with logback integra… 1 [OK]
chenyufeng/tomcat-centos tomcat基于centos6的镜像 1 [OK]
99taxis/tomcat7 Tomcat7 1 [OK]
softwareplant/tomcat Tomcat images for jira-cloud testing 0 [OK]
secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK]
s390x/tomcat Apache Tomcat is an open source implementati… 0

docker search [Options] 镜像名字

  • --no-trunc : 显示完整的镜像描述
  • stars : 列出收藏数不小于指定值的镜像
  • --automated : 只列出 automated build类型的镜像
  • --filter : 按照指定条件过滤搜索的镜像

搜索stars不小于12的镜像:docker search --filter stars=12 imagename

1
2
3
4
5
6
7
8
9
[dw@dk ~]$ sudo docker search --filter stars=12 tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 3042 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 88 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 57 [OK]
bitnami/tomcat Bitnami Tomcat Docker Image 37 [OK]
consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 18 [OK]
cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 13 [OK]

显示完全描述信息不截断:

1
2
3
4
5
6
7
8
9
10
11
[dw@dk ~]$ 
[dw@dk ~]$ sudo docker search --filter stars=12 --no-trunc tomcat # 显示完整的description信息
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies 3042 [OK]
tomee Apache TomEE is an all-Apache Java EE certified stack where Apache Tomcat is top dog. 88 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 based docker container. 57 [OK]
bitnami/tomcat Bitnami Tomcat Docker Image 37 [OK]
kubeguide/tomcat-app Tomcat image for Chapter 1 30
consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 18 [OK]
cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 13 [OK]

docker pull 某个镜像名字

该命令用于拉取所需的镜像。

docker pull tomcat == docker pull tomcat:latest

public class Book == public class Book extends Object

拉取镜像时,可以看到,会一层层拉取镜像,这么看来,镜像就像千层饼,一层嵌套一层。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[dw@dk ~]$ sudo docker pull tomcat
Using default tag: latest

latest: Pulling from library/tomcat
d960726af2be: Downloading [==================> ] 18.19MB/50.43MB
e8d62473a22d: Download complete
8962bc0fad55: Download complete
65d943ee54c1: Downloading [=====> ] 5.713MB/51.84MB
da20b77f10ac: Waiting
8669a096f083: Waiting
e0c0a5e9ce88: Waiting
f7f46169d747: Waiting
42d8171e56e6: Waiting
774078a3f8bb: Waiting

[dw@dk ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest c43a65faae57 2 weeks ago 667MB
hello-world latest d1165f221234 2 months ago 13.3kB

docker rmi 某个XXX镜像名字ID

该命令用于删除镜像。

docker rmi -f 镜像ID

1
2
3
4
5
6
7
8
9
10
[dw@dk ~]$ sudo docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 6f304ae98477 is using its referenced image d1165f221234 # 可以看到hello-world里面有两层
[dw@dk ~]$ sudo docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c # <--
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726 # <--

[dw@dk ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest c43a65faae57 2 weeks ago 667MB

镜像之所以这么大是因为镜像将运行环境(java等)也给下载下来,一层层嵌套。

docker rmi -f 镜像名1:TAG 镜像名2:TAG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[dw@dk ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest d1a364dc548d 8 days ago 133MB
tomcat latest c43a65faae57 2 weeks ago 667MB
hello-world latest d1165f221234 2 months ago 13.3kB
[dw@dk ~]$ sudo docker rmi -f hello-world nginx <--(不加tag默认是latest | hello-world:latest)
Untagged: hello-world:latest
Untagged: hello-world@sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
Untagged: nginx:latest
Untagged: nginx@sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750
Deleted: sha256:d1a364dc548d5357f0da3268c888e1971bbdb957ee3f028fe7194f1d61c6fdee
Deleted: sha256:fcc8faba78fe8a1f75025781c8fa1841079b75b54fce8408d039f73a48b7a81b
Deleted: sha256:a476b265974ace4c857e3d88b358e848f126297a8249840c72d5f5ea1954a4bf
Deleted: sha256:56722ee1ee7e73a5c6f96ea2959fa442fb4db9f044399bcd939bb0a6eb7919dc
Deleted: sha256:c657df997c75f6c1a9c5cc683e8e34c6f29e5b4c1dee60b632d3477fd5fdd644
Deleted: sha256:e9e1f772d2a8dbbeb6a4a4dcb4f0d07ff1c432bf94fac7a2db2216837bf9ec5b
Deleted: sha256:02c055ef67f5904019f43a41ea5f099996d8e7633749b6e606c400526b2c4b33
[dw@dk ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest c43a65faae57 2 weeks ago 667MB

docker rmi -f $(docker images -qa)

“$()”会在括号内运行linux脚本命令,通过查询镜像的id,返回给docker rmi -f命令,强制删除镜像,即使镜像正在运行。

Docker容器命令

有镜像才能创建容器,这是根本前提!(需要先下载一个CentOS镜像演示:docker pull centos)

新建并启动容器

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

1
2
3
4
5
6
7
8
9
10
11
[dw@dk ~]$ sudo docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[dw@dk ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest c43a65faae57 2 weeks ago 667MB
centos latest 300e315adb2f 5 months ago 209MB

Options说明

OPTIONS说明(常用):有些是一个减号,有些是两个减号:

  • --name="容器新名字" : 为容器指定一个名称,—name如果未指定会自动生成一个随机命名;

  • -d : 后台运行容器,并返回容器ID,也即启动守护式容器;

  • -i :以交互模式运行容器,通常与 -t 同时使用;

  • -t :为容器重新分配一个伪输入终端,通常与 -i 同时使用;

  • -P : 随机端口映射;

  • -p : 指定端口映射,有以下四种格式

  • ip:hostPort:containerPort
    ip::containerPort
    hostPort:containerPort
    containerPort

启动交互式容器

使用镜像centos:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。
docker run -it centos /bin/bash : 进入centos容器的命令行终端

1
2
3
4
5
6
7
8
9
10
11
[dw@dk ~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest c43a65faae57 2 weeks ago 667MB
centos latest 300e315adb2f 5 months ago 209MB
[dw@dk ~]$ sudo docker run -it 300e315adb2f
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@1cc047f95a5f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1cc047f95a5f /]# pwd
/
[root@1cc047f95a5f /]#

列出当前所有正在运行的容器

docker ps [OPTIONS]

Options说明

  • -a : 列出当前所有正在运行的容器+历史上运行过的
  • -l : 显示最近创建的容器。
  • -n : 显示最近n个创建的容器。
  • -q : 静默模式,只显示容器编号。
  • --no-trunc : 不截断输出。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[dw@dk ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1cc047f95a5f 300e315adb2f "/bin/bash" 19 hours ago Up 19 hours gallant_haslett

[dw@dk ~]$ sudo docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1cc047f95a5f 300e315adb2f "/bin/bash" 19 hours ago Up 19 hours gallant_haslett

[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1cc047f95a5f 300e315adb2f "/bin/bash" 19 hours ago Up 19 hours gallant_haslett
6f304ae98477 d1165f221234 "/hello" 22 hours ago Exited (0) 22 hours ago sharp_euclid

[dw@dk ~]$ sudo docker ps -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1cc047f95a5f 300e315adb2f "/bin/bash" 19 hours ago Up 19 hours gallant_haslett

[dw@dk ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1cc047f95a5f 300e315adb2f "/bin/bash" 19 hours ago Up 19 hours gallant_haslett
6f304ae98477 d1165f221234 "/hello" 22 hours ago Exited (0) 22 hours ago sharp_euclid

[dw@dk ~]$ sudo docker ps -q
1cc047f95a5f

退出容器

宿主机相当于大海,docker就相当于海里的一条鲸鱼,images跳到鲸鱼背上进入 容器中。

exit : 容器停止退出。

ctrl+P+Q : 容器不停止在后台运行,退出容器。

启动容器

docker start 容器ID或者容器名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Up 3 hours mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago epic_benz
1cc047f95a5f 300e315adb2f "/bin/bash" 22 hours ago Up 22 hours gallant_haslett

[dw@dk ~]$ sudo docker start aa33464be938
aa33464be938

[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Up 3 hours mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Up 3 seconds epic_benz
1cc047f95a5f 300e315adb2f "/bin/bash" 22 hours ago Up 22 hours gallant_haslett

重启容器

docker restart 容器ID或者容器名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Up 3 hours mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Up About a minute epic_benz
1cc047f95a5f 300e315adb2f "/bin/bash" 22 hours ago Up 22 hours gallant_haslett

[dw@dk ~]$ sudo docker restart 1cc047f95a5f
1cc047f95a5f

[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Up 3 hours mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Up About a minute epic_benz
1cc047f95a5f 300e315adb2f "/bin/bash" 22 hours ago Up 2 seconds gallant_haslett

停止容器

docker stop 容器ID或者容器名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Up 3 hours mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Up 3 minutes epic_benz
1cc047f95a5f 300e315adb2f "/bin/bash" 22 hours ago Up About a minute gallant_haslett

[dw@dk ~]$ sudo docker stop 1cc047f95a5f # 这里的停止相当于电脑关机,缓慢进行
1cc047f95a5f

[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Up 3 hours mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Up 4 minutes epic_benz
1cc047f95a5f 300e315adb2f "/bin/bash" 22 hours ago Exited (0) 16 seconds ago gallant_haslett

强制停止容器

docker kill 容器ID或者容器名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Up 3 hours mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Up 5 minutes epic_benz
1cc047f95a5f 300e315adb2f "/bin/bash" 22 hours ago Exited (0) About a minute ago gallant_haslett

[dw@dk ~]$ sudo docker kill aa33464be938
aa33464be938

[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Up 3 hours mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Exited (137) 1 second ago epic_benz
1cc047f95a5f 300e315adb2f "/bin/bash" 22 hours ago Exited (0) About a minute ago gallant_haslett

删除已停止的容器

docker rm 容器ID

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[dw@dk ~]$ sudo docker ps # 虽然现在没有容器运行但是缓存记录里存在之前运行过的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

[dw@dk ~]$ sudo docker ps -n 3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Exited (0) 16 seconds ago mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Exited (137) About a minute ago epic_benz
1cc047f95a5f 300e315adb2f "/bin/bash" 22 hours ago Exited (0) 2 minutes ago gallant_haslett

# 删除缓存记录的容器使用 docker rm(rmi : 是删除镜像, rm是删除容器)
docker rm -f container_id :强制删除容器,在容器未关闭的情况下进行
[dw@dk ~]$ sudo docker rm 1cc047f95a5f
1cc047f95a5f

[dw@dk ~]$ sudo docker ps -n 3 # 删除这个后之前的容器缓存替代上来
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8277c5b2cf72 centos "/bin/bash" 3 hours ago Exited (0) 2 minutes ago mycentos121
aa33464be938 centos "/bin/bash" 3 hours ago Exited (137) 3 minutes ago epic_benz
6f304ae98477 d1165f221234 "/hello" 25 hours ago Exited (0) 25 hours ago sharp_euclid

一次性删除多个容器

  1. docker rm -f $(docker ps -a -q)
  2. docker ps -a -q | xargs docker rm
1
2
3
4
5
6
7
8
9
10
11
[dw@dk ~]$ sudo docker ps -qa
8277c5b2cf72
aa33464be938
6f304ae98477

[dw@dk ~]$ sudo docker rm -f $(sudo docker ps -qa)
8277c5b2cf72
aa33464be938
6f304ae98477

[dw@dk ~]$ sudo docker ps -qa

其他重要的容器命令

启动守护式容器

docker run -d 容器名

使用镜像centos:latest以后台模式启动一个容器:docker run -d centos,让容器在后台运行,自己跑,不用进入交互终端。

问题:然后docker ps -a 进行查看, 会发现容器已经退出?
很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程。
容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。

1
2
3
4
5
6
7
8
9
10
11
[dw@dk ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

[dw@dk ~]$ sudo docker run -d centos
WARNING: IPv4 forwarding is disabled. Networking will not work.
3c5e4af09ebeaf8d40b053fabd800b09685c53302248675f2c821832af3d21fa

[dw@dk ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

# 可以发现ps后台没有显示先前守护进程启动的容器(后台、前台运行各有作用)

这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动响应的service即可。例如:service nginx start
但是,这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用,这样的容器后台启动后,会立即自杀因为他觉得他没事可做了。所以,最佳的解决方案是,将你要运行的程序以前台进程的形式运行。

查看容器日志

docker logs -f -t --tail 容器ID

  • -t: 是加入时间戳
  • -f : 跟随最新的日志打印
  • --tail : 数字 显示最后多少条
1
2
# 为了避免后台守护进程由于没事可做自动挂掉,让后台运行循环脚本,每两秒输出hello hj
sudo docker run -d centos /bin/sh -c "while true;do echo hello hj;sleep 2;done"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[dw@dk ~]$ sudo docker run -d centos /bin/sh -c "while true;do echo hello hj;sleep 2;done"
WARNING: IPv4 forwarding is disabled. Networking will not work.
5d3e0a1225a996bef7ad062642a5758d287af34203de0bd7bfd38f47d04f9a0e

[dw@dk ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d3e0a1225a9 centos "/bin/sh -c 'while t…" 10 seconds ago Up 3 seconds dazzling_volhard

[dw@dk ~]$ sudo docker logs 5d3e0a1225a9
hello hj
hello hj
hello hj
hello hj

[dw@dk ~]$ sudo docker logs -t 5d3e0a1225a9 # 打印日志输出时间
2021-06-05T01:41:44.553531090Z hello hj
2021-06-05T01:41:46.559173262Z hello hj

[dw@dk ~]$ sudo docker logs -t -f 5d3e0a1225a9 # 让日志动态打印

[dw@dk ~]$ sudo docker logs -t -f --tail 3 5d3e0a1225a9 #日志动态打印最后三行
2021-06-05T01:44:13.070213731Z hello hj
2021-06-05T01:44:15.074464003Z hello hj
2021-06-05T01:44:17.078454274Z hello hj

查看容器内运行的进程

docker top 容器ID

1
2
3
4
5
6
7
[dw@dk ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d3e0a1225a9 centos "/bin/sh -c 'while t…" 4 minutes ago Up 4 minutes dazzling_volhard
[dw@dk ~]$ sudo docker top 5d3e0a1225a9 # docker容器本质是一个精简版的linux
UID PID PPID C STIME TTY TIME CMD
root 10017 9997 0 09:41 ? 00:00:00 /bin/sh -c while true;do echo hello hj;sleep 2;done
root 10311 10017 0 09:46 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2

查看容器内部细节

docker inspect 容器ID:显示更丰富的容器相关信息

容器是一层套一层,以json字符串形式嵌套描述。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[dw@dk ~]$ sudo docker inspect 5d3e0a1225a9
[
{
"Id": "5d3e0a1225a996bef7ad062642a5758d287af34203de0bd7bfd38f47d04f9a0e",
"Created": "2021-06-05T01:41:38.932984008Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hello hj;sleep 2;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 10017,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-06-05T01:41:44.55458619Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
......

进入正在运行的容器并以命令行交互

docker exec -it 容器ID bashShell

重新进入docker attach 容器ID

之后kill掉这个容器,再运行一个:

1
2
3
4
5
6
7
8
[dw@dk ~]$ sudo docker run -it centos
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@bbb551fb9acb /]# [dw@dk ~]$
[dw@dk ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bbb551fb9acb centos "/bin/bash" 16 seconds ago Up 10 seconds vigilant_sutherland
[dw@dk ~]$ sudo docker attach bbb551fb9acb
[root@bbb551fb9acb /]#

在容器外部传入命令,让容器返回结果,不用进去:

1
2
3
4
[dw@dk ~]$ sudo docker exec -it bbb551fb9acb ls -l /tmp
total 8
-rwx------ 1 root root 701 Dec 4 17:37 ks-script-esd4my7v
-rwx------ 1 root root 671 Dec 4 17:37 ks-script-eusq_sc5

有一个问题,当命令漏了-i单使用-t也能进入,但是进去后输入ls -l /tmp会卡在那,换成exec -it id /bin/bash进入后执行ls -l /tmp响应很快。

上述两个区别

attach 直接进入容器启动命令的终端,不会启动新的进程

exec 是在容器中打开新的终端,并且可以启动新的进程

从容器内拷贝文件到主机上

docker cp 容器ID:容器内路径 目的主机路径

创建测试文件:

1
2
3
4
5
6
[dw@dk tmp]$ sudo docker exec -it bbb551fb9acb /bin/bash
[root@bbb551fb9acb /]# cd /tmp/
[root@bbb551fb9acb tmp]# vi a.xtx
[root@bbb551fb9acb tmp]# cat a.xtx
text
[root@bbb551fb9acb tmp]# read escape sequence

将文件copy出来:

1
2
3
4
5
6
[dw@dk tmp]$ sudo docker exec -it bbb551fb9acb ls /tmp
'\' a.xtx ks-script-esd4my7v ks-script-eusq_sc5
[dw@dk tmp]$ sudo docker cp bbb551fb9acb:/tmp/a.xtx /root

[dw@dk tmp]$ sudo ls /root
anaconda-ks.cfg a.xtx bin

Docker容器常用命令总结

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’s 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 # 从当前 Docker registry 退出
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 # 截取容器停止时的退出状态值


上一篇Link Docker入门

下一篇 Docker的常用命令