4.Docker的命令

 

1,帮助命令

  • docker version 查看docker版本
  • docker info 显示全系统信息
  • docker –help 显示docker相关的所有命令及功能说明

2,镜像命令

2.1 docker images 列表本机上的镜像

  • REPOSITORY:表示镜像的仓库源
  • TAG:镜像的标签
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小

可选项:

  • -a 列表本地的所有镜像及子镜像
  • -q 只显示镜像ID
  • –digests 显示镜像的摘要信息
  • –no-trunc 显示完整的镜像信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost local]# docker images -help
Flag shorthand -h has been deprecated, please use --help
unknown shorthand flag: 'e' in -elp
See 'docker images --help'.
[root@localhost local]# docker images --help

Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--help Print usage
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs

2.2 docker search 镜像搜索命令

搜索网站:https://hub.docker.com

语法:

  • docker search 镜像名称
  • docker search 镜像名称 -[options] 说明
  • -s 列出收藏数不少于指定值的镜像

2.3 docker pull 镜像下载命令

docker pull 镜像名称:[TAG]

例如:

  • docker pull tomcat:8.5 下载8.5的镜像版本
  • dokcer pull tomcat 默认下载最新的tomcat镜像版本 【latest】

2.4 rmi镜像删除命令

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
[root@localhost ~]# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest 5f47aad0b70e 4 days ago 647 MB
docker.io/nginx latest 7e4d58f0e5f3 5 days ago 133 MB
docker.io/mysql 5.7 ef08065b0a30 5 days ago 448 MB
docker.io/mysql latest e1d7dc9731da 5 days ago 544 MB
docker.io/bitnami/tomcat 9.0.37-debian-10-r43 8242e310e96b 6 days ago 462 MB
docker.io/ubuntu latest 4e2eef94cd6b 3 weeks ago 73.9 MB
docker.io/hello-world latest bf756fb1ae65 8 months ago 13.3 kB
[root@localhost ~]# docker rmi 4e2
Untagged: docker.io/ubuntu:latest
Untagged: docker.io/ubuntu@sha256:31dfb10d52ce76c5ca0aa19d10b3e6424b830729e32a89a7c6eee2cda2be67a5
Deleted: sha256:4e2eef94cd6b93dd4d794c18b45c763f72edc22858e0da5b6e63a4566a54c03c
Deleted: sha256:160004bdd9a2800d0085be0315b769a9ce04c07ca175ecae89593eeee9aeb944
Deleted: sha256:9ed638911072c3379e75d2eaf7c2502220d6757446325c8d96236410b0729268
Deleted: sha256:ce7da152e578608030e9a05f9f5259b329fe5dcc5bf48b9f544e48bd69a5f630
Deleted: sha256:2ce3c188c38d7ad46d2df5e6af7e7aed846bc3321bdd89706d5262fefd6a3390
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest 5f47aad0b70e 4 days ago 647 MB
docker.io/nginx latest 7e4d58f0e5f3 5 days ago 133 MB
docker.io/mysql 5.7 ef08065b0a30 5 days ago 448 MB
docker.io/mysql latest e1d7dc9731da 5 days ago 544 MB
docker.io/bitnami/tomcat 9.0.37-debian-10-r43 8242e310e96b 6 days ago 462 MB
docker.io/hello-world latest bf756fb1ae65 8 months ago 13.3 kB

默认镜像的版本是lastest,不是删除lastest版本,需要加TAG

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
[root@localhost ~]# docker images 
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest 5f47aad0b70e 4 days ago 647 MB
docker.io/nginx latest 7e4d58f0e5f3 5 days ago 133 MB
docker.io/mysql 5.7 ef08065b0a30 5 days ago 448 MB
docker.io/mysql latest e1d7dc9731da 5 days ago 544 MB
docker.io/bitnami/tomcat 9.0.37-debian-10-r43 8242e310e96b 6 days ago 462 MB
docker.io/hello-world latest bf756fb1ae65 8 months ago 13.3 kB
[root@localhost ~]# docker rmi docker.io/mysql
Untagged: docker.io/mysql:latest
Untagged: docker.io/mysql@sha256:e1bfe11693ed2052cb3b4e5fa356c65381129e87e38551c6cd6ec532ebe0e808
Deleted: sha256:e1d7dc9731daa2c79858307d65ef35f543daf97457b9c11b681f78bb86f7a158
Deleted: sha256:303dd82484b7080d07af8ab7f383755d8b4d723a2ade8c2e3a516ae59fbc1d63
Deleted: sha256:6346e1a264e283de286e399bc798b8e6a910176180aa1ef1acc54dbace18111a
Deleted: sha256:6c7a3d83131ddf3414e34e14826cbfb8f01a6d6486cd41d9a7a2bcc84350b4f0
Deleted: sha256:79b91792bcadd6668ab861a83623dedcc38574c5f033d2f8336671588a1c4de0
Deleted: sha256:00bdff03938c98e1fee710e8c447a1c30e7365859dd28e2a166792d7cd343fa9
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest 5f47aad0b70e 4 days ago 647 MB
docker.io/nginx latest 7e4d58f0e5f3 5 days ago 133 MB
docker.io/mysql 5.7 ef08065b0a30 5 days ago 448 MB
docker.io/bitnami/tomcat 9.0.37-debian-10-r43 8242e310e96b 6 days ago 462 MB
docker.io/hello-world latest bf756fb1ae65 8 months ago 13.3 kB
[root@localhost ~]# docker rmi docker.io/mysql
Error response from daemon: No such image: docker.io/mysql:latest
[root@localhost ~]# docker rmi docker.io/bitnami/tomcat
Error response from daemon: No such image: docker.io/bitnami/tomcat:latest
[root@localhost ~]# docker rmi docker.io/bitnami/tomcat:9.0.37-debian-10-r43
Untagged: docker.io/bitnami/tomcat:9.0.37-debian-10-r43
Untagged: docker.io/bitnami/tomcat@sha256:67795aeea5636d783f34e1c5f189fb561d31c42f0549d8224e9b739ec4ad3328
Deleted: sha256:8242e310e96b2da1314dbeecb604f3866a13e0c2a6b045ef1e52306406f652b4
Deleted: sha256:d05e7729c70ae17018d611ef4f4b8fc828ab9cc01f0ca724b78c3391be52bdeb
Deleted: sha256:863be56d83ccf08d01f64a49f45dca3d9508ac90eb64e7821c1e66409e077405
Deleted: sha256:0b9af046dea28771763dd13b994ac1f7613cabd07919afb9887a52a1d2634d99
Deleted: sha256:0023ee4b3525af4946a4e591e1c42dec5804baa25dcdfb0007866a46e02faa77
Deleted: sha256:fdec2fff5d8654f282f9bfdf4c6fd5c82560530c032c9328f80641dfdb9bf4cc
Deleted: sha256:09e495a763838dfae4b2a133c26dc4fd903d9e996d21238a7e4bdd9a6e65f6ff
Deleted: sha256:eb27c09b62b27eea1f855c0c70e352931cba3545a09eda84cba09000537df3b9
Deleted: sha256:dd71433212a8b9cd715187f432054939b65d6de1ec0591126667671057866cd0
Deleted: sha256:8076f22510d19684cd416aecf00743189461f833abb5d3abe94a1210e7fc0b5d
Deleted: sha256:41b68c787b092d0e60a16f75ef908e4e977d6f3b29f33fa15418221daf601123

删除全部镜像

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
[root@localhost ~]# docker rmi -f $(docker images -q)
Untagged: docker.io/tomcat:latest
Untagged: docker.io/tomcat@sha256:91fe68dbc7f616b56227f75d3bc2e0a1e0131b80afbd44c6cfdf5761b4b5e6af
Untagged: docker.io/tomcat@sha256:c2b033c9cee06d6a3eb5a4d082935bbb8afee7478e97dcd6bc452bb6ab28da4b
Deleted: sha256:5f47aad0b70e1d6a8324543d3b7536f23baaaf09b09661dbf4e52d7b822c0157
Deleted: sha256:34ccbdf0c4b338bbe26a4c349d517fb7662b65611bd02c35f72358838aeac6b1
Deleted: sha256:5bc61859d971832370605e0fb8d1fe41e0550e3841043de1c19d971691efd4c4
Deleted: sha256:a6f52ba4967c48d8aba223ff71a9764d2dacd53490af3d1e73da96cc66f3e05b
Deleted: sha256:b0cb8e4a85df51db4e5fe10dfa149b2b3f1170c7a3a8d33b0c5ef6e104b9a980
Deleted: sha256:6a05ecc17b6c6bc8a97223bf4a04aa5ad34084270d86ca0605bf5a603e1656f4
Deleted: sha256:40fef2729fc360297758bf86b3011b68ca41f3880f6d6578734b0280b3d8e344
Deleted: sha256:e40886afa47f1a991f807ca03358366b86c1f65471c6c7e10187cf94eae5bcd1
Deleted: sha256:28189e70cc47eb3c32769ee0aad10e6b18a8886206db4df9ccfacb50878ca1ed
Deleted: sha256:92b912e56b356aeb8e2ab873bdf557ee80fbd3bec4482684fa0f0b0c30896acc
Deleted: sha256:4ef54afed7804a44fdeb8cf6562c2a1eb745dcaabd38b1ac60126f0966bf6aef
Untagged: docker.io/nginx:latest
Untagged: docker.io/nginx@sha256:9a1f8ed9e2273e8b3bbcd2e200024adac624c2e5c9b1d420988809f5c0c41a5e
Untagged: docker.io/nginx@sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
Untagged: docker.io/nginx@sha256:fa5ebe5fe25f931f412b27219edf025f7b9613cb98c94cfcb5586eb61347e507
Deleted: sha256:7e4d58f0e5f3b60077e9a5d96b4be1b974b5a484f54f9393000a99f3b6816e3d
Untagged: docker.io/mysql:5.7
Untagged: docker.io/mysql@sha256:14fd47ec8724954b63d1a236d2299b8da25c9bbb8eacc739bb88038d82da4919
Deleted: sha256:ef08065b0a302111b56966aa92c89fa0bacdfc537741cbca88a15b10f14332ca
Deleted: sha256:c8c81ac92392c394197759ca3d50f5f843d85ac1550d8c0bb2b21adc7334100d
Deleted: sha256:2dc86a1b9b92e7c946c684bd349e448d7c4fbb3236686e1a48ddfe5adb86a425
Deleted: sha256:97b541df82456d38e987b630870fcd4e39f05f016717652466b3466841f4162e
Deleted: sha256:aded9a11fc54761c770a9075cfc2d0bb72c72b59171a56cfa4322ab2b2d416e7
Deleted: sha256:3fc742b087b51753835895403bd70b649bf2e4e32c5eb5f2f15c2b7d22409964
Deleted: sha256:00d53c8dcb5c8fd12fbfd2b3b25afcbc3a044e73f1fd395af231f86827238fcf
Deleted: sha256:007d9a78de83bec432afb0e4e847072a6943e853456fe21b70488d25c1c96bf4
Deleted: sha256:31fd54b7e8a4e80375cbfd9ead44c37e0ee03d8c4ca7ace27cc1896e9d40e2a2
Deleted: sha256:22d626b2851ecf92af46f616ce1ff0ef0879ef23b68be2fb92be3311f2308763
Deleted: sha256:5da80bf7a61d70d69c153f015cd2a5aa96594f36f3835ef6dbcd4da41da374f2
Untagged: docker.io/hello-world:latest
Untagged: docker.io/hello-world@sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]#

3. 容器命令

3.1 创建并启动容器

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

1
2
3
4
5
6
7
8
9
10
--name="容器新名字": 为容器指定一个名称;
-d: 后台运行容器,并返回容器ID,也即启动守护式容器;
-i:以交互模式运行容器,通常与 -t 同时使用;
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-P: 随机端口映射;
-p: 指定端口映射,有以下四种格式
      ip:hostPort:containerPort
      ip::containerPort
      hostPort:containerPort
      containerPort

命令结尾加/bin/bash

9996:8080: 9996是容器外访问端口,8080是容器内对应的端口。

1
2
3
4
5
[root@localhost ~]# docker run -it -d -p 9996:8080 --name=tomcat_9996 mytomcat02:1.0 /bin/bash
3775c6c3b922f639e832a7cc775b346cedc7f5f2fb9d892f76c4e5cd628b3b40
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3775c6c3b922 mytomcat02:1.0 "/bin/bash" 4 seconds ago Up 3 seconds 0.0.0.0:9996->8080/tcp tomcat_9996

很重要的要说明的一点: Docker容器在后台运行,就必须有一个前台进程,没有前台进程的容器会退出运行。

3.2 退出容器

  1. exit: 退出容器并关闭前台进程
1
2
3
4
[root@localhost ~]# docker exec -it 3775c6c3b922 /bin/bash
root@3775c6c3b922:/usr/local/tomcat#
[root@5931cbd25bd6 /]# exit
exit
  1. ctrl+p+q: 退出不关闭前台进程
1
2
3
4
[root@localhost ~]# docker attach 3775c6c3b922
root@3775c6c3b922:/usr/local/tomcat#
root@3775c6c3b922:/usr/local/tomcat# [root@localhost ~]#
[root@localhost ~]#

3.3 列出所有容器

Usage: docker ps [OPTIONS]

默认只列出正在运行的容器

options的参数说明:

1
2
3
4
5
-a  显示所有运行和没有运行的容器
-l :显示最近创建的容器。
-n:显示最近n个创建的容器。
-q :静默模式,只显示容器编号。
--no-trunc :不截断输出。
1
2
3
4
5
6
7
8
9
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3775c6c3b922 mytomcat02:1.0 "/bin/bash" 15 minutes ago Up 15 minutes 0.0.0.0:9996->8080/tcp tomcat_9996
1f53c5b786b9 tomcat:8.5 "catalina.sh run" 2 hours ago Exited (143) About an hour ago hardcore_volhard
59f66823f944 5a915ec43999 "catalina.sh run" 20 hours ago Exited (143) About an hour ago jolly_joliot
[root@localhost ~]# docker ps -aq
3775c6c3b922
1f53c5b786b9
59f66823f944

3.4 启动重启关闭容器

参数为:容器ID或容器名称

  1. 启动一个已经关闭的容器实例

docker start -i 59f66823f944

Usage: docker start [OPTIONS] CONTAINER [CONTAINER…]

Start one or more stopped containers

1
2
3
4
5
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
--help Print usage
-i, --interactive Attach container's STDIN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
59f66823f944 5a915ec43999 "catalina.sh run" 17 hours ago Exited (143) 17 hours ago jolly_joliot
[root@localhost ~]# docker start -i 59f66823f944
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/openjdk-11
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
25-Sep-2020 16:55:48.808 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.38
...
25-Sep-2020 16:55:58.781 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [242] ms
25-Sep-2020 16:55:58.806 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
25-Sep-2020 16:55:58.893 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [5680] milliseconds

ctrl+p+q退出容器实例

1
2
3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
59f66823f944 5a915ec43999 "catalina.sh run" 17 hours ago Up 24 seconds 0.0.0.0:9992->8080/tcp jolly_joliot
  1. 重启容器

docker restart 1f53c5b786b9

Usage: docker restart [OPTIONS] CONTAINER [CONTAINER…]

Restart one or more containers

1
2
3
Options:
--help Print usage
-t, --time int Seconds to wait for stop before killing the container (default 10)
1
2
3
4
5
6
7
8
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f53c5b786b9 tomcat:8.5 "catalina.sh run" 2 hours ago Exited (143) About a minute ago hardcore_volhard
[root@localhost ~]# docker restart 1f53c5b786b9
1f53c5b786b9
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f53c5b786b9 tomcat:8.5 "catalina.sh run" 2 hours ago Up 2 seconds 0.0.0.0:9995->8080/tcp hardcore_volhard
  1. 关闭容器

docker stop 1f53c5b786b9

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER…]

Stop one or more running containers

1
2
3
Options:
--help Print usage
-t, --time int Seconds to wait for stop before killing it (default 10)
1
2
3
4
5
6
7
8
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f53c5b786b9 tomcat:8.5 "catalina.sh run" 2 hours ago Up 5 minutes 0.0.0.0:9995->8080/tcp hardcore_volhard
[root@localhost ~]# docker stop 1f53c5b786b9
1f53c5b786b9
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f53c5b786b9 tomcat:8.5 "catalina.sh run" 2 hours ago Exited (143) 3 seconds ago hardcore_volhard

3.5 强制停止容器

docker kill mycentos01

只能停止正在运行的容器

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5931cbd25bd6 centos "/bin/bash" 11 minutes ago Up 3 minutes mycentos02
e6da485c6a25 centos "/bin/bash" 16 minutes ago Exited (0) 2 minutes ago mycentos01
b666386fc45b docker.io/nginx "/docker-entrypoin..." 2 days ago Exited (0) 2 days ago stoic_nightingale
8778d46474ac hello-world "/hello" 2 days ago Exited (0) 2 days ago stupefied_montalcini
8d320d1da37a nginx "/docker-entrypoin..." 3 days ago Exited (0) 3 days ago sharp_yalow
81ff80475ee9 hello-world "/hello" 3 days ago Exited (0) 3 days ago objective_jepsen
[root@localhost ~]# docker kill mycentos01
Error response from daemon: Cannot kill container mycentos01: Container e6da485c6a25c1246c273ce4d0938c3469a2cb829a7ec847515303d91f42a928 is not running
[root@localhost ~]# docker kill mycentos02
mycentos02
[root@localhost ~]#

3.6 删除已停止的容器

  • 单个删除
1
2
[root@localhost ~]# docker rm mycentos01
mycentos01

删除正在运行的容器需要加-f,不能删除不存在的容器

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5931cbd25bd6 centos "/bin/bash" 13 minutes ago Up 48 seconds mycentos02
b666386fc45b docker.io/nginx "/docker-entrypoin..." 2 days ago Exited (0) 2 days ago stoic_nightingale
8778d46474ac hello-world "/hello" 2 days ago Exited (0) 2 days ago stupefied_montalcini
8d320d1da37a nginx "/docker-entrypoin..." 3 days ago Exited (0) 3 days ago sharp_yalow
81ff80475ee9 hello-world "/hello" 3 days ago Exited (0) 3 days ago objective_jepsen
[root@localhost ~]# docker rm mycentos02
Error response from daemon: You cannot remove a running container 5931cbd25bd674748bb9369f8d121e69b3947f38d8923a778e1f2a096b374512. Stop the container before attempting removal or use -f
[root@localhost ~]# docker rm mycentos0201
Error response from daemon: No such container: mycentos0201
  • 多个删除

docker rm -f $(docker ps -aq)

1
2
[root@localhost ~]# docker rm -f $(docker ps -aq)
5931cbd25bd6

4. 访问容器内部命令

4.1 查看容器日志

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# docker logs --help

Usage: docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--help Print usage
--since string Show logs since timestamp
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
1
2
3
4
5
6
...
23-Sep-2020 20:56:54.876 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 3601 ms
23-Sep-2020 20:56:54.997 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
23-Sep-2020 20:56:54.998 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.58
23-Sep-2020 20:56:55.052 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
23-Sep-2020 20:56:55.092 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 215 ms

4.2 查看容器运行进程

1
2
3
[root@localhost ~]# docker top f5267bd65b6e
UID PID PPID C STIME TTY TIME CMD
root 2481 2464 4 05:07 ? 00:00:04 /usr/local/openjdk-8/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start

4.3 查询容器内部细节

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
[root@localhost ~]# docker inspect f5267bd65b6e
[
{
"Id": "f5267bd65b6eaa588e8c2726c6ba3e21b19ffea2ae1e7c5361f1ff108dda5438",
"Created": "2020-09-23T21:07:06.350019156Z",
"Path": "catalina.sh",
"Args": [
"run"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2481,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-09-23T21:07:06.888159297Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:d5ef5658144457b2a37ef7ef7a2ab08c19c881ca7025684b8389489839319979",
"ResolvConfPath": "/var/lib/docker/containers/f5267bd65b6eaa588e8c2726c6ba3e21b19ffea2ae1e7c5361f1ff108dda5438/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/f5267bd65b6eaa588e8c2726c6ba3e21b19ffea2ae1e7c5361f1ff108dda5438/hostname",
"HostsPath": "/var/lib/docker/containers/f5267bd65b6eaa588e8c2726c6ba3e21b19ffea2ae1e7c5361f1ff108dda5438/hosts",
"LogPath": "",
"Name": "/trusting_jang",
"RestartCount": 0,
"Driver": "overlay2",
"MountLabel": "system_u:object_r:svirt_sandbox_file_t:s0:c932,c1015",
"ProcessLabel": "system_u:system_r:svirt_lxc_net_t:s0:c932,c1015",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "journald",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"8080/tcp": [
{
"HostIp": "",
"HostPort": "8080"
}
]
},
"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": "docker-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": "overlay2",
"Data": {
"LowerDir": "/var/lib/docker/overlay2/3d4ed113630b6dc6dab8ebbe92bf404e2191c9c7aa82bf6862afb68a5eced803-init/diff:/var/lib/docker/overlay2/bffc2796a3c6c79abe42d866238bcf908781a77435f199b0334125127d2cb29a/diff:/var/lib/docker/overlay2/20c8b184cba061bc74d24e58e355f8b74cc769037bbb40950571799057ac0c2b/diff:/var/lib/docker/overlay2/27ce2f192cb6e070cba81efb222caff790ad717564a9c6a0a6b2cc4563d84ada/diff:/var/lib/docker/overlay2/43d4d085e12bb03bd7e37eb410ea1e7761059a89b1ddb62378d43d658a9cb11f/diff:/var/lib/docker/overlay2/2fb275acd8cb154aae7d57e7ebe1570892d31ed7a965301cb5a3e7bfa148d2f3/diff:/var/lib/docker/overlay2/1340202b75c4d0fc5deb300977031a69aaa30ddd2a6e8976999095c89b38ad10/diff:/var/lib/docker/overlay2/7f68b23d307f809547c749fb7bb4a5363dcc1b54e610af1668a2f11865a80f51/diff:/var/lib/docker/overlay2/0e604c8d5c80e0cd240ba87c0c87c0c071e6cc15bc100c437fd75e488e24af34/diff:/var/lib/docker/overlay2/0eb793ebd2ab6322c963c690e25acc1103e717b38f9cd360770bf17e5986e2b6/diff:/var/lib/docker/overlay2/cecd452ec4a67f3661446ef7ffb42cd6db2dd91b5fe0f48040febf9af649e3d7/diff",
"MergedDir": "/var/lib/docker/overlay2/3d4ed113630b6dc6dab8ebbe92bf404e2191c9c7aa82bf6862afb68a5eced803/merged",
"UpperDir": "/var/lib/docker/overlay2/3d4ed113630b6dc6dab8ebbe92bf404e2191c9c7aa82bf6862afb68a5eced803/diff",
"WorkDir": "/var/lib/docker/overlay2/3d4ed113630b6dc6dab8ebbe92bf404e2191c9c7aa82bf6862afb68a5eced803/work"
}
},
"Mounts": [],
"Config": {
"Hostname": "f5267bd65b6e",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"8080/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/tomcat/bin:/usr/local/openjdk-8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"LANG=C.UTF-8",
"JAVA_HOME=/usr/local/openjdk-8",
"JAVA_VERSION=8u265",
"CATALINA_HOME=/usr/local/tomcat",
"TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
"LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
"GPG_KEYS=05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23",
"TOMCAT_MAJOR=8",
"TOMCAT_VERSION=8.5.58",
"TOMCAT_SHA512=55d8fa698f7ac118ab33f1be644477fa8424f0fe6eefa80c85acd4e3cbce5f1704ce3cf897dfcd42c5c95cd2ff3b559e774fb5b7ac7279dd6b803a9a2dd8cc8f"
],
"Cmd": [
"catalina.sh",
"run"
],
"ArgsEscaped": true,
"Image": "tomcat:8.5",
"Volumes": null,
"WorkingDir": "/usr/local/tomcat",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "483d671a64f2f4cc218a9a14e03bb2ce595c2db435fac903d7bd891b0e3b853b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8080/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
},
"SandboxKey": "/var/run/docker/netns/483d671a64f2",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "f1835bb673f33c373a4ca717625d6ebac6887087649f4e500b09fa872414407a",
"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": "f27c7f146e9d58efde024a9c0573b8a1e9e3c0523e5ee4087b7216867c09e463",
"EndpointID": "f1835bb673f33c373a4ca717625d6ebac6887087649f4e500b09fa872414407a",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]

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

  • 方式1: docker exec -it 容器ID /bin/bash

重新打开一个新的终端 如果以这种方式进入容器,可以使用exit退出。而不会关闭容器

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# docker exec -it f5267bd65b6e /bin/bash
root@f5267bd65b6e:/usr/local/tomcat#
root@f5267bd65b6e:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@f5267bd65b6e:/usr/local# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5267bd65b6e tomcat:8.5 "catalina.sh run" 7 minutes ago Up 7 minutes 0.0.0.0:8080->8080/tcp trusting_jang
  • 方式2: docker attach 容器ID
1
2
[root@localhost ~]# docker attach 3775c6c3b922
root@3775c6c3b922:/usr/local/tomcat#
  • 两种方式的区别:

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

attach: 直接进行容器终端,不会启动新的进程。这种方式里面使用exit退出会关闭容器,如果不想关闭容器必须使用ctrl+p+q

4.5 进入容器内拷贝文件到主机

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# docker cp --help

Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
--help Print usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# docker cp 99012afdd9b8:/usr/local/tomcat/README.md .
[root@localhost ~]# ls
anaconda-ks.cfg README.md
[root@localhost ~]# cat README.md
## Welcome to Apache Tomcat!

### What Is It?

The Apache Tomcat® software is an open source implementation of the Java
Servlet, JavaServer Pages, Java Expression Language and Java WebSocket
technologies. The Java Servlet, JavaServer Pages, Java Expression Language and
Java WebSocket specifications are developed under the
[Java Community Process](https://jcp.org/en/introduction/overview).
...

5. docker命令总结