本节接着上文Docker学习(八)——Docker镜像操作(第一节)


上文说到了镜像的获取,接下来就可以以镜像为基础启动容器。


获取Docker镜像

以Tomcat为例,首先需要获取Docker镜像:

docker pull tomcat:8.0

解释:下载Tomcat8.0版本的镜像

[root@izwz99z2ljzabken0hvkjrz docker]# docker pull tomcat:8.0
Trying to pull repository docker.io/library/tomcat ... 
8.0: Pulling from docker.io/library/tomcat
f189db1b88b3: Pull complete 
3d06cf2f1b5e: Pull complete 
edd0da9e3091: Pull complete 
eb7768aae14e: Pull complete 
e2780f585e0f: Pull complete 
e5ed720afeba: Pull complete 
d9e134700cfc: Pull complete 
e4804b33d02a: Pull complete 
b9df0c24315e: Pull complete 
49fdae8eaa20: Pull complete 
1aea3d9a32e6: Pull complete 
Digest: sha256:8ecb10948deb32c34aeadf7bf95d12a93fbd3527911fa629c1a3e7823b89ce6f
Status: Downloaded newer image for docker.io/tomcat:8.0

使用命令docker images来列出已经下载的Docker镜像

[root@izwz99z2ljzabken0hvkjrz docker]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              fce289e99eb9        7 months ago        1.84 kB
docker.io/tomcat        8.0                 ef6a7c98d192        11 months ago       356 MB


运行Docker 容器

在有了镜像之后,我们可以使用docker run 这个命令来启动容器,前面说到:Docker的镜像和容器的关系相当于面向对象语言中的类和实例的关系。我们以tomcat镜像为例来创建一个实例:

docker run -it --rm -p 8080:8080 tomcat:8.0

这个命令可以分为三部分,docker run 为一部分,这个是标准的写法。tomcat:8.0为一部分,这个表示要操作的镜像,中间的参数为一部分。

下面就来解释这几个参数的意思(ps:其实Docker的命令有很多,在学习的时候不必要全部都要懂,会一些常用的,以后遇到复杂场景再去研究那些复杂的命令,学习不是一蹴而就的)


-it :这是两个单词的缩写Interaction(交互)和Terminal(终端)。-i表示的是交互式操作,-t表示的是终端。

--rm:这个参数表示在容器退出后随即删除容器。一般在只需要启动一次看看效果的时候加入此参数,防止空间浪费。

-p:表示端口映射,第一个8080是当前主机的端口,第二个8080表示对应的容器里面暴露出的端口。


运行效果如下:

[root@izwz99z2ljzabken0hvkjrz docker]# docker run -it --rm -p 8080:8080  tomcat:8.0
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /docker-java-home/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
20-Aug-2019 06:43:14.232 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.0.53
20-Aug-2019 06:43:14.233 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jun 29 2018 14:42:45 UTC
20-Aug-2019 06:43:14.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.0.53.0
20-Aug-2019 06:43:14.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
20-Aug-2019 06:43:14.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.10.0-514.26.2.el7.x86_64
20-Aug-2019 06:43:14.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
20-Aug-2019 06:43:14.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/lib/jvm/java-7-openjdk-amd64/jre
20-Aug-2019 06:43:14.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.7.0_181-b01
20-Aug-2019 06:43:14.234 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
20-Aug-2019 06:43:14.238 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
20-Aug-2019 06:43:14.239 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
20-Aug-2019 06:43:14.239 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
20-Aug-2019 06:43:14.239 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
20-Aug-2019 06:43:14.239 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
20-Aug-2019 06:43:14.239 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
20-Aug-2019 06:43:14.239 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
20-Aug-2019 06:43:14.240 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
20-Aug-2019 06:43:14.240 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
20-Aug-2019 06:43:14.240 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
20-Aug-2019 06:43:14.240 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.5.1.
20-Aug-2019 06:43:14.240 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
20-Aug-2019 06:43:14.248 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.1.0f  25 May 2017)
20-Aug-2019 06:43:14.374 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
20-Aug-2019 06:43:14.400 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
20-Aug-2019 06:43:14.403 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1204 ms
20-Aug-2019 06:43:14.493 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
20-Aug-2019 06:43:14.493 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53
20-Aug-2019 06:43:14.528 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs
20-Aug-2019 06:44:13.582 WARNING [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [57,937] milliseconds.
20-Aug-2019 06:44:13.620 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 59,092 ms
20-Aug-2019 06:44:13.620 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager
20-Aug-2019 06:44:13.737 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 117 ms
20-Aug-2019 06:44:13.737 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager
20-Aug-2019 06:44:13.824 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 87 ms
20-Aug-2019 06:44:13.824 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT
20-Aug-2019 06:44:13.864 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 38 ms
20-Aug-2019 06:44:13.864 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples
20-Aug-2019 06:44:14.827 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 963 ms
20-Aug-2019 06:44:14.841 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"]
20-Aug-2019 06:44:14.875 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"]
20-Aug-2019 06:44:14.878 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 60474 ms


当然还可以使用以下命令进入到Tomcat的工作路径下:

docker run -it --rm -p 8080:8080 tomcat:8.0 bash

这个命令和上面的命令不同的地方是后面接了一个bash

这个命令表示需要启动容器里面的bash进行交互式操作。

使用exit就可以退出容器,由于我们加入了--rm ,容器退出后就被删除。

Docker删除本地镜像

使用docker rmi 命令来删除本地镜像,同样的命令还有:docker image rm ;

列出镜像docker images:

[root@izwz99z2ljzabken0hvkjrz docker]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              fce289e99eb9        7 months ago        1.84 kB
docker.io/tomcat        8.0                 ef6a7c98d192        11 months ago       356 MB

这里看到有一个hello world,我们删除这个镜像:

[root@izwz99z2ljzabken0hvkjrz docker]# docker rmi docker.io/hello-world
Untagged: docker.io/hello-world:latest
Untagged: docker.io/hello-world@sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3

PS:这里不仅可以使用仓库名来删除镜像,还可以通过images ID来删除镜像,命令格式:

 docker rmi  fce289e99eb9

注意到上面执行删除命令的时候输出前面有Untagged和Deleted两个标签,那么这两个标签是什么意思呢?


Untagged 和 Deleted

其实但我们执行删除命令的时候,实际上是在删除某个标签的镜像。这个时候Docker的做法是先将这个命令的镜像涉及到的所有标签先解绑,这就是看到的untagged提示。因为一个镜像可以对应多个标签,当我们删除了特定的标签的时候,可能还有别的标签指向这个镜像,这个情况下,执行rmi的时候就不会删除这个tag,而是将他们互相解绑,这个时候DELETE情况也就不会发生。

并非所有的docker rmi 命令都会触发delete行为,有可能仅仅是取消了某个标签而已。

当与该镜像关联的所有标签都被删除了,那么这个镜像存在的意义就没有了,就会触发Delete操作。