镜像commit操作
一、Docker安装Tomcat
1、官方使用方法,用完容器就删除
docker run -it --rm tomcat:9.0
2、执行 docker run -it --rm tomcat:9.0下载镜像并创建容器,镜像下载完成后会自动创建容器,可看到输出tomcat日志
#此时使用ctrl+c退出执行,再使用docker ps -a,会发现无法看到tomcat容器,原因是使用该方式是“用完即删的”
[root@xgy ~]# docker run -it --rm tomcat:9.0
Unable to find image 'tomcat:9.0' locally
9.0: Pulling from library/tomcat
b9a857cbf04d: Pull complete
d557ee20540b: Pull complete
3b9ca4f00c2e: Pull complete
667fd949ed93: Pull complete
661d3b55f657: Pull complete
511ef4338a0b: Pull complete
a56db448fefe: Pull complete
00612a99c7dc: Pull complete
326f9601c512: Pull complete c547db74f1e1: Pull complete
Digest: sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305
Status: Downloaded newer image for tomcat:9.0
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
29-Jan-2021 05:59:13.830 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.41
...(省略)
3、使用docker ps -a,会发现无法看到tomcat容器,原因是使用该方式是“用完即删的”
#使用docker ps -a,会发现无法看到tomcat容器,原因是使用该方式是“用完即删的”
[root@xgy ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf791c59bb5e nginx "/docker-entrypoint.…" 14 hours ago Exited (0) 14 hours ago nginx-test
771f03d614f7 centos "/bin/bash" 17 hours ago Exited (0) 17 hours ago funny_fermi
9d37ac6a51af centos "/bin/bash" 17 hours ago Exited (0) 17 hours ago nostalgic_keldysh
4、使用命令docker pull tomcat下载最新tomcat镜像
#4、使用命令docker pull tomcat下载最新tomcat镜像
[root@xgy ~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
Digest: sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
5、docker images查看主机中的镜像列表,可看到有9.0和latest版本的镜像
[root@xgy ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 9.0 040bdb29ab37 2 weeks ago 649MB
tomcat latest 040bdb29ab37 2 weeks ago 649MB
nginx latest f6d0b4767a6c 2 weeks ago 133MB
centos latest 300e315adb2f 7 weeks ago 209MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
6、基于最新版本的tomcat镜像创建一个名为tomcat-test的容器,并将该容器的8080端口映射到主机的5001端口(-p命令映射端口),并设置其在后台运行(-d命令设置后台运行)。
[root@xgy ~]# docker run -d -p 5001:8080 --name tomcat-test tomcat
996b9f7e923dcd5270268e42a74bd026c9fd654c12464c0a7e76cbba3daafa98
[root@xgy ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
996b9f7e923d tomcat "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:5001->8080/tcp tomcat-test
7、使用curl+ip:prot命令访问5001端口,发现输出404,这表示访问tomcat成功,但是找不到tomcat中的webapps中的html文件
[root@xgy ~]# curl localhost:5001
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.41</h3></body></html>[root@xgy ~]#
#8、使用docker exec+/bin/bash命令表示以交互模式进入tomcat-test容器
[root@xgy ~]# docker exec -it tomcat-test /bin/bash
9、ls命令查看容器根目录中有哪些文件,可看到有webapps文件夹
root@996b9f7e923d:/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
10、进入webapps文件夹,使用ls命令,发现文件夹中没有任何文件
#发现出现了以下问题:
#(1)linux命令少了;
#(2)webapps文件夹中没有任何内容,这是阿里云镜像的原因,默认是最小镜像,所有不必要的文件都被剔除掉,保证最小可运行环境;
root@996b9f7e923d:/usr/local/tomcat# cd webapps
root@996b9f7e923d:/usr/local/tomcat/webapps# ls
root@996b9f7e923d:/usr/local/tomcat/webapps#
11、进入webapps.dist目录,使用ls命令发现该文件夹中有webapps目录需要的文件,此时讲该文件夹中的内容复制到webapps目录中即可
root@996b9f7e923d:/usr/local/tomcat/webapps# cd ..
root@996b9f7e923d:/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@996b9f7e923d:/usr/local/tomcat# cd webapps.dist/
root@996b9f7e923d:/usr/local/tomcat/webapps.dist# ls
ROOT docs examples host-manager manager
root@996b9f7e923d:/usr/local/tomcat/webapps.dist# cd ..
#12、递归复制webapps.dist目录中的文件到webapps中
root@996b9f7e923d:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@996b9f7e923d:/usr/local/tomcat# cd webapps
#13、进入webapps目录中,使用ls命令可发现我们所需要的文件
root@996b9f7e923d:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
#14、退出容器,使用curl可正常访问,详情可看下图
root@996b9f7e923d:/usr/local/tomcat/webapps# exit
exit
[root@xgy ~]# curl localhost:5001
浏览器中访问5001端口结果如下:
二、镜像commit操作
1.命令解析
docker commit 提交容器成为新的副本
#命令和git原理类似
docker commit -m "提交的描述信息" -a="作者" 容器id 目标镜像名:[TAG]
2.实操
1、查看已启动的容器列表
#1查看已启动的容器列表
[root@xgy ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED PORTS NAMES
2、查看系统中的镜像列表
#2查看系统中的镜像列表
[root@xgy ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
redis latest 621ceef7494a 2 weeks ago
tomcat 9.0 040bdb29ab37 2 weeks ago
tomcat latest 040bdb29ab37 2 weeks ago
nginx latest f6d0b4767a6c 2 weeks ago
centos latest 300e315adb2f 7 weeks ago
hello-world latest bf756fb1ae65 13 months ago
3、使用docker commit 命令自定义一个的tomcat镜像,一个包含webapps的tomcat镜像
[root@xgy ~]# docker commit -a="xgy" -m="create private image that clude webapps" tomcat-private:1.0
sha256:990239ccbdc91e901bb1942379d1cdcf895ff953f659cd97f6563f0f49faab7c
4、使用docker images命令列举镜像,可看到第3步中创建的tomcat-private镜像
[root@xgy ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
tomcat-private 1.0 990239ccbdc9 6 seconds ago
redis latest 621ceef7494a 2 weeks ago
tomcat 9.0 040bdb29ab37 2 weeks ago
tomcat latest 040bdb29ab37 2 weeks ago
nginx latest f6d0b4767a6c 2 weeks ago
centos latest 300e315adb2f 7 weeks ago
hello-world latest bf756fb1ae65 13 months ago
赞赏
如果您觉得文章还不错,那就请作者喝杯咖啡吧!