15. Docker命令练习

 

1.启动一个tomcat容器实例,以复制方式把webapps复制进容器,制作一个webapps里有示例项目的tomcat镜像

下载tomcat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost download]# yum install wget
[root@localhost download]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.58/bin/apache-tomcat-8.5.58.tar.gz
[root@localhost download]# ls -lh
total 10M
-rw-r--r--. 1 root root 10M Sep 11 06:03 apache-tomcat-8.5.58.tar.gz
[root@localhost download]# tar -zxvf apache-tomcat-8.5.58.tar.gz
[root@localhost download]# ls
apache-tomcat-8.5.58 apache-tomcat-8.5.58.tar.gz
[root@localhost download]# cd apache-tomcat-8.5.58
[root@localhost apache-tomcat-8.5.58]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
[root@localhost apache-tomcat-8.5.58]# cd webapps/
[root@localhost webapps]# ls
docs examples host-manager manager ROOT

复制webapps到容器中

1
2
3
4
5
6
7
8
9
[root@localhost webapps]# docker cp . 1d396b710a51:/usr/local/tomcat/webapps/
[root@localhost webapps]# docker exec -it 1d396b710a51 /bin/bash
root@1d396b710a51:/usr/local/tomcat# cd webapps
root@1d396b710a51:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
root@1d396b710a51:/usr/local/tomcat/webapps# exit
exit
[root@localhost webapps]#

制作新的镜像

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost webapps]# docker commit -a "Tom" -m "webapps Tomcat" 1d396b710a51 mytomcat02:1.0
sha256:5a915ec4399963811ce9e148bcc396a34b1470a473e46dbbb99cc822b4345a04
[root@localhost webapps]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat02 1.0 5a915ec43999 3 seconds ago 652 MB
mytomcat01 1.0 cce38f19a569 25 minutes ago 647 MB
docker.io/tomcat 8.5 d5ef56581444 9 days ago 530 MB
docker.io/tomcat latest f796d3d2c195 9 days ago 647 MB
docker.io/nginx latest 7e4d58f0e5f3 2 weeks ago 133 MB
docker.io/centos latest 0d120b6ccaa8 6 weeks ago 215 MB
docker.io/hello-world latest bf756fb1ae65 8 months ago 13.3 kB
[root@localhost webapps]#

运行新的镜像

1
2
3
4
5
6
7
[root@localhost webapps]# docker run -it -d -p 9992:8080 5a915ec43999
59f66823f9440443a18290661603063e36fd3be9c6e0c41a9b41876ae64d4914
[root@localhost webapps]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
59f66823f944 5a915ec43999 "catalina.sh run" 17 seconds ago Up 16 seconds 0.0.0.0:9992->8080/tcp jolly_joliot
1d396b710a51 cce38f19a569 "catalina.sh run" 25 minutes ago Up 25 minutes 0.0.0.0:9999->8080/tcp focused_easley
85e3bfc0c8e2 f796d3d2c195 "catalina.sh run" 36 minutes ago Up 36 minutes 0.0.0.0:8888->8080/tcp elastic_carson

进入新的容器

1
2
3
4
5
6
7
8
9
10
[root@localhost webapps]# docker exec -it 59f66823f944 /bin/bash
root@59f66823f944:/usr/local/tomcat#
root@59f66823f944:/usr/local/tomcat#
root@59f66823f944:/usr/local/tomcat# ls
BUILDING.txt LICENSE RELEASE-NOTES bin lib native-jni-lib webapps work
CONTRIBUTING.md NOTICE RUNNING.txt conf logs temp webapps.dist
root@59f66823f944:/usr/local/tomcat# cd webapps
root@59f66823f944:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
root@59f66823f944:/usr/local/tomcat/webapps#

在浏览器中访问192.168.88.128:9992

2. 启动一个tomcat容器实例,以数据卷的方式把webapps共享给tomcat容器,运行一个webapps里有示例项目的tomcat实例

启动一个tomcat容器并且挂载数据卷

1
2
3
4
5
6
7
8
9
[root@localhost software]# docker run -it -d -p 9995:8080 -v /usr/local/software/tomcat_p_9995/:/usr/local/tomcat/webapps --privileged=true tomcat:8.5
1f53c5b786b9a57843e5fa714afb7d7b8151614b5c2f602029c14d1a229a417a
[root@localhost software]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f53c5b786b9 tomcat:8.5 "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:9995->8080/tcp hardcore_volhard
[root@localhost software]# docker exec -it 1f53c5b786b9 /bin/bash
root@1f53c5b786b9:/usr/local/tomcat/webapps# ls
root@1f53c5b786b9:/usr/local/tomcat# exit
exit

容器里的webapps是空的

把webapps复制到宿主机的数据卷中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost download]# ls
apache-tomcat-8.5.58 apache-tomcat-8.5.58.tar.gz
[root@localhost download]# cd apache-tomcat-8.5.58
[root@localhost apache-tomcat-8.5.58]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
[root@localhost apache-tomcat-8.5.58]# cp -r webapps/* /usr/local/software/tomcat_p_9995/
[root@localhost tomcat_p_9995]# ls
docs examples host-manager manager ROOT
[root@localhost tomcat_p_9995]# docker exec -it 1f53c5b786b9 /bin/bash
root@1f53c5b786b9:/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@1f53c5b786b9:/usr/local/tomcat# cd webapps
root@1f53c5b786b9:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager