11.Dockerfile详解【5】

 

制作项目镜像

1. 使用jar包做项目镜像

运行jar包需要jdk环境。

1.1 准备jar包

1.2 在linux里面创建一个目录

1
[root@localhost docker_file]# mkdir my_jar_project

1.3 上传jdk和jar包到linux的目录

1
2
[root@localhost my_jar_project]# ls
bjsxt.jar Dockerfile jdk-8u271-linux-x64.tar.gz

1.4 创建并编写Dockerfile文件

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
# 基于centos镜像
FROM centos
# 设置作者和邮箱
MAINTAINER Tom<Tom@qq.com>
# 声明环境变量
ENV SOFTWARE_PATH /usr/local/software
ENV JDK_NAME jdk1.8.0_271
ENV WORKPATH /usr/local/project
# jdk设置部分-start
# 创建目录
RUN mkdir ${SOFTWARE_PATH}
# 把jdk和tomcat复制到相应目录
ADD jdk-8u271-linux-x64.tar.gz /${SOFTWARE_PATH}/
# 配置jdk和tomcat环境变量
ENV JAVA_HOME ${SOFTWARE_PATH}/${JDK_NAME}
ENV CLASSPATH ${JAVA_HOME}/lib/dt.jar:${JDK_HOME}/lib/tools.jar
ENV CATALINA_HOME ${SOFTWARE_PATH}/${TOMCAT_NAME}
ENV CATALINA_BASE ${SOFTWARE_PATH}/${TOMCAT_NAME}
ENV PATH ${PATH}:${JAVA_HOME}/bin:${CATALINA_HOME}/lib:${CATALINA_HOME}/bin
# jdk设置部分-end
# 声明工作目录
WORKDIR ${WORKPATH}
# 添加当前目录里的bjsxt.jar,并重命名为app.jar
ADD bjsxt.jar app.jar
# 添加容器卷
VOLUME ${WORKPATH}
# 声明端口
EXPOSE 8080
# 运行jar包
ENTRYPOINT ["java", "-jar", "app.jar"]

1.5 构造镜像

1
[root@localhost my_jar_project]# docker build -t my_jar_project:1.0 .
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
Sending build context to Docker daemon 162.5 MB
Step 1/17 : FROM centos
---> 0d120b6ccaa8
Step 2/17 : MAINTAINER Tom<Tom@qq.com>
---> Using cache
---> e91b1474c962
Step 3/17 : ENV SOFTWARE_PATH /usr/local/software
---> Using cache
---> 7c51deda1e35
Step 4/17 : ENV JDK_NAME jdk1.8.0_271
---> Using cache
---> c931c9163dee
Step 5/17 : ENV WORKPATH /usr/local/project
---> Running in 2a50a3237b1b
---> 1428de5c1b39
Removing intermediate container 2a50a3237b1b
Step 6/17 : RUN mkdir ${SOFTWARE_PATH}
---> Running in 40ffe38deabd

---> 20dc7b21d1b3
Removing intermediate container 40ffe38deabd
Step 7/17 : ADD jdk-8u271-linux-x64.tar.gz /${SOFTWARE_PATH}/
---> 1d21c21eebc4
Removing intermediate container 8df29f752500
Step 8/17 : ENV JAVA_HOME ${SOFTWARE_PATH}/${JDK_NAME}
---> Running in d47ed606d60c
---> 416ded5d1abe
Removing intermediate container d47ed606d60c
Step 9/17 : ENV CLASSPATH ${JAVA_HOME}/lib/dt.jar:${JDK_HOME}/lib/tools.jar
---> Running in 85e201d81c85
---> e3850e41ebfd
Removing intermediate container 85e201d81c85
Step 10/17 : ENV CATALINA_HOME ${SOFTWARE_PATH}/${TOMCAT_NAME}
---> Running in a3064cfbbdaa
---> 2cab766940ca
Removing intermediate container a3064cfbbdaa
Step 11/17 : ENV CATALINA_BASE ${SOFTWARE_PATH}/${TOMCAT_NAME}
---> Running in 2f17eef9074b
---> 8a317dbde928
Removing intermediate container 2f17eef9074b
Step 12/17 : ENV PATH ${PATH}:${JAVA_HOME}/bin:${CATALINA_HOME}/lib:${CATALINA_H OME}/bin
---> Running in 68b269dca2c2
---> 0581e4b251ad
Removing intermediate container 68b269dca2c2
Step 13/17 : WORKDIR ${WORKPATH}
---> d154afbd81a2
Removing intermediate container e95085f78723
Step 14/17 : ADD bjsxt.jar app.jar
---> 093bfaca0f9c
Removing intermediate container 7799e3530352
Step 15/17 : VOLUME ${WORKPATH}
---> Running in ecdef36d2277
---> 561d533af090
Removing intermediate container ecdef36d2277
Step 16/17 : EXPOSE 8080
---> Running in 30054444d64c
---> 036a3f07310b
Removing intermediate container 30054444d64c
Step 17/17 : ENTRYPOINT java -jar app.jar
---> Running in 74f242241f1f
---> 9f2ae069071e
Removing intermediate container 74f242241f1f
Successfully built 9f2ae069071e
1
2
3
[root@localhost my_jar_project]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_jar_project 1.0 9f2ae069071e 15 seconds ago 590 MB

1.6 使用镜像运行容器

1
2
[root@localhost my_jar_project]# docker run -d -p 8080:8080 my_jar_project:1.0 /bin/bash
6414ea32b7df6d479b7ba528296de906dc0870def2e5584d0af4bc9d1fa9710b

1.7 测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost my_jar_project]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:1f:b2:aa brd ff:ff:ff:ff:ff:ff
inet 192.168.168.130/24 brd 192.168.168.255 scope global noprefixroute dynamic ens33
valid_lft 1341sec preferred_lft 1341sec
inet6 fe80::8c0b:4db3:b3f3:c20b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:90:7d:b0:56 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:90ff:fe7d:b056/64 scope link
valid_lft forever preferred_lft forever
7: veth1a3ed8b@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 6a:b9:28:dc:e8:7b brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::68b9:28ff:fedc:e87b/64 scope link
valid_lft forever preferred_lft forever

打开浏览器,输入http://192.168.168.130:8080/user/loadAllUser

返回:

1
[{"id":1,"name":"小明1","adress":"武汉1","birth":"2020-11-23 10:59:20"},{"id":2,"name":"小明2","adress":"武汉2","birth":"2020-11-23 10:59:20"},{"id":3,"name":"小明3","adress":"武汉3","birth":"2020-11-23 10:59:20"},{"id":4,"name":"小明4","adress":"武汉4","birth":"2020-11-23 10:59:20"},{"id":5,"name":"小明5","adress":"武汉5","birth":"2020-11-23 10:59:20"},{"id":6,"name":"小明6","adress":"武汉6","birth":"2020-11-23 10:59:20"},{"id":7,"name":"小明7","adress":"武汉7","birth":"2020-11-23 10:59:20"},{"id":8,"name":"小明8","adress":"武汉8","birth":"2020-11-23 10:59:20"},{"id":9,"name":"小明9","adress":"武汉9","birth":"2020-11-23 10:59:20"},{"id":10,"name":"小明10","adress":"武汉10","birth":"2020-11-23 10:59:20"}]

2. 使用war包做项目镜像

运行war包需要tomcat环境。

2.1 准备war包

2.2 在linux里面创建一个目录

1
[root@localhost docker_file]# mkdir my_war_project

2.3 上传war包到linux的目录

1
2
[root@localhost my_war_project]# ls
bjsxt.war Dockerfile

2.4 创建并编写Dockerfile文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 基于之前自己制作的自定义tomcat镜像
FROM mytomcat02:1.0
# 作者
MAINTAINER Tom<Tom@qq.com>
# 设置环境变量
ENV SOFTWARE_PATH /usr/local/software
ENV TOMCAT_NAME tomcat-8.5
ENV TOMCAT_PATH ${SOFTWARE_PATH}/${TOMCAT_NAME}
# 设置工作目录
WORKDIR ${TOMCAT_PATH}/webapps
# 清空webapps目录
RUN rm -rf *
# 复制war包到webapps
COPY bjsxt.war ./bjsxt.war
# 声明端口
EXPOSE 8080
# 启动tomcat
CMD ["../bin/catalina.sh", "run"]

2.5 构造镜像

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
[root@localhost my_war_project]# docker build -t my_war_project:1.0 .
Sending build context to Docker daemon 19.34 MB
Step 1/10 : FROM mytomcat02:1.0
---> 9715fb4d02ba
Step 2/10 : MAINTAINER Tom<Tom@qq.com>
---> Running in 5e8c172ca4e1
---> 7d09738b3c42
Removing intermediate container 5e8c172ca4e1
Step 3/10 : ENV SOFTWARE_PATH /usr/local/software
---> Running in ceaabf94344e
---> 4119b55c0654
Removing intermediate container ceaabf94344e
Step 4/10 : ENV TOMCAT_NAME tomcat-8.5
---> Running in d9af62cd4f3f
---> 29c95852ae31
Removing intermediate container d9af62cd4f3f
Step 5/10 : ENV TOMCAT_PATH ${SOFTWARE_PATH}/${TOMCAT_NAME}
---> Running in d77128301cb5
---> 6796eed7167b
Removing intermediate container d77128301cb5
Step 6/10 : WORKDIR ${TOMCAT_PATH}/webapps
---> d13864f99004
Removing intermediate container a97a34b3440f
Step 7/10 : RUN rm -rf *
---> Running in 81444d55e497

---> 4252b5938fa6
Removing intermediate container 81444d55e497
Step 8/10 : COPY bjsxt.war ./bjsxt.war
---> a57d7f417c43
Removing intermediate container b516d95ac75e
Step 9/10 : EXPOSE 8080
---> Running in 4284ed213a3d
---> 7be5e2bb4d9b
Removing intermediate container 4284ed213a3d
Step 10/10 : CMD ../bin/catalina.sh run
---> Running in 0d56c580fa92
---> 28e4ac5b7c65
Removing intermediate container 0d56c580fa92
Successfully built 28e4ac5b7c65
1
2
3
[root@localhost my_war_project]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_war_project 1.0 28e4ac5b7c65 19 seconds ago 676 MB

2.6 使用镜像运行容器

1
2
3
4
5
[root@localhost my_war_project]# docker run -d -p 8080:8080 my_war_project:1.0
6c4590e7a2a6e05a4f799f308c03c54a8708824c53907a684214cfb630177a2d
[root@localhost my_war_project]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c4590e7a2a6 my_war_project:1.0 "../bin/catalina.s..." 4 seconds ago Up 2 seconds 0.0.0.0:8080->8080/tcp practical_wescoff

或者

1
2
3
4
5
[root@localhost my_war_project]# docker run -it -d -p 8080:8080 my_war_project:1.0
59a96e3f407974599fe49ee7d3bf481c485eb7ac52315a7fd6668fb88ef8fc3b
[root@localhost my_war_project]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
59a96e3f4079 my_war_project:1.0 "../bin/catalina.s..." 3 seconds ago Up 1 second 0.0.0.0:8080->8080/tcp awesome_lovelace

2.7 测试

打开浏览器,输入http://192.168.168.130:8080/bjsxt/user/loadAllUser

返回:

1
[{"id":1,"name":"小明1","adress":"武汉1","birth":"2020-11-23 10:59:20"},{"id":2,"name":"小明2","adress":"武汉2","birth":"2020-11-23 10:59:20"},{"id":3,"name":"小明3","adress":"武汉3","birth":"2020-11-23 10:59:20"},{"id":4,"name":"小明4","adress":"武汉4","birth":"2020-11-23 10:59:20"},{"id":5,"name":"小明5","adress":"武汉5","birth":"2020-11-23 10:59:20"},{"id":6,"name":"小明6","adress":"武汉6","birth":"2020-11-23 10:59:20"},{"id":7,"name":"小明7","adress":"武汉7","birth":"2020-11-23 10:59:20"},{"id":8,"name":"小明8","adress":"武汉8","birth":"2020-11-23 10:59:20"},{"id":9,"name":"小明9","adress":"武汉9","birth":"2020-11-23 10:59:20"},{"id":10,"name":"小明10","adress":"武汉10","birth":"2020-11-23 10:59:20"}]