1.上传解压导入Java镜像
1.1 上传
[ root@localhost ~ ] # ls java. tar. gz
java. tar. gz
1.2 解压
[ root@localhost ~ ] # tar xzf java. tar. gz
[ root@localhost ~ ] # ls java. tar
java. tar
1.3 导入
[ root@localhost ~ ] # docker load - i java. tar
a2ae92ffcd29: Loading layer [ == == == == == == == == == == == == == == == == == == == == == == == == == > ] 128.9 MB/ 128.9 MB
0 eb22bfb707d: Loading layer [ == == == == == == == == == == == == == == == == == == == == == == == == == > ] 45.18 MB/ 45.18 MB
30339f 20 ced0: Loading layer [ == == == == == == == == == == == == == == == == == == == == == == == == == > ] 126.6 MB/ 126.6 MB
ce6c8756685b: Loading layer [ == == == == == == == == == == == == == == == == == == == == == == == == == > ] 1.404 MB/ 1.404 MB
a3483ce177ce: Loading layer [ == == == == == == == == == == == == == == == == == == == == == == == == == > ] 3.584 kB/ 3.584 kB
6 ed1a81ba5b6: Loading layer [ == == == == == == == == == == == == == == == == == == == == == == == == == > ] 3.584 kB/ 3.584 kB
c3fe59dd9556: Loading layer [ == == == == == == == == == == == == == == == == == == == == == == == == == > ] 356.7 MB/ 356.7 MB
35 c20f26d188: Loading layer [ == == == == == == == == == == == == == == == == == == == == == == == == == > ] 426 kB/ 426 kB
Loaded image: java: latest
[ root@localhost ~ ] #
1.4 查看导入的Java镜像
[ root@localhost ~ ] # docker images java
REPOSITORY TAG IMAGE ID CREATED SIZE
java latest d23bdf5b1b1b 3 years ago 643 MB
[ root@localhost ~ ] #
2. 创建工作目录并上传Tomcat源码包
[ root@localhost ~ ] # mkdir tomcat
[ root@localhost ~ ] # cd tomcat/
[ root@localhost tomcat] # ls
apache- tomcat- 8.5 .35 . tar. gz
[ root@localhost tomcat] #
3. 编写创建Tomcat镜像的dockerfile文件
[ root@localhost tomcat] # vim dockerfile
[ root@localhost tomcat] # cat dockerfile
FROM java
ADD apache- tomcat- 8.5 .35 . tar. gz / opt/
WORKDIR / opt/
RUN mv apache- tomcat- 8.5 .35 tomcat
CMD [ "/opt/tomcat/bin/catalina.sh" , "run" ]
4.构建并查看Tomcat镜像
4.1 构建
root@localhost tomcat] # docker build - t tomcat: 1.1 . /
Sending build context to Docker daemon 9.646 MB
Step 1 / 5 : FROM java
-- - > d23bdf5b1b1b
Step 2 / 5 : ADD apache- tomcat- 8.5 .35 . tar. gz / opt/
-- - > b5a6e9a1eb67
Step 3 / 5 : WORKDIR / opt/
-- - > Running in d3e7263b4c50
Removing intermediate container d3e7263b4c50
-- - > 53704 d569348
Step 4 / 5 : RUN mv apache- tomcat- 8.5 .35 tomcat
-- - > Running in 40 d782ee6332
Removing intermediate container 40 d782ee6332
-- - > 7 d0398900e3f
Step 5 / 5 : CMD [ "/opt/tomcat/bin/catalina.sh" , "run" ]
-- - > Running in 8 d30f8aee88a
Removing intermediate container 8 d30f8aee88a
-- - > 42 b73735e5ab
Successfully built 42 b73735e5ab
Successfully tagged tomcat: 1.1
4.2 查看
[ root@localhost tomcat] # docker images | grep - B 1 tomcat
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 1.1 42 b73735e5ab 7 minutes ago 671 MB
[ root@localhost tomcat] #
5.基于Tomcat镜像运行Tomcat 容器
5.1 运行
[ root@localhost tomcat] # docker run -- name tomcat - d - p 8080 : 8080 tomcat: 1.1
b05700a05947441d5c9943b3589b64a3d8294f5cb627a693f40b0a34421dbcd0
5.2 查看
[ root@localhost tomcat] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b05700a05947 tomcat: 1.1 "/opt/tomcat/bin/cat…" 4 minutes ago Up 4 minutes 0.0 .0 .0 : 8080 - > 8080 / tcp tomcat
6. 打开浏览器输入 IP:8080 验证
7.优化dockerfile
7.1查看存放可执行文件的目录
[ root@localhost tomcat] # echo $PATH
/ usr/ local/ sbin: / usr/ local/ bin: / usr/ sbin: / usr/ bin: / root/ bin
[ root@localhost tomcat] #
7.2 在dockerfile使用ENV指令增加环境变量
[ root@localhost tomcat] # vim dockerfile
[ root@localhost tomcat] # cat dockerfile
FROM java
ENV PATH= $PATH: / opt/ tomcat/ bin/
ADD apache- tomcat- 8.5 .35 . tar. gz / opt/
WORKDIR / opt/
RUN mv apache- tomcat- 8.5 .35 tomcat
CMD [ "catalina.sh" , "run" ]
[ root@localhost tomcat] #
7.3 依据dockerfile构建Tomcat镜像
[ root@localhost tomcat] # docker build - t tomcat: 1.2 . /
Sending build context to Docker daemon 9.646 MB
Step 1 / 6 : FROM java
-- - > d23bdf5b1b1b
Step 2 / 6 : ENV PATH= $PATH: / opt/ tomcat/ bin/
-- - > Running in 1908e40 b298c
Removing intermediate container 1908e40 b298c
-- - > 3 b9adf3e41a9
Step 3 / 6 : ADD apache- tomcat- 8.5 .35 . tar. gz / opt/
-- - > 23 c57923582f
Step 4 / 6 : WORKDIR / opt/
-- - > Running in 78 c5c9b90828
Removing intermediate container 78 c5c9b90828
-- - > 1 da5d5f69b8e
Step 5 / 6 : RUN mv apache- tomcat- 8.5 .35 tomcat
-- - > Running in 93 dfd7226ee5
Removing intermediate container 93 dfd7226ee5
-- - > b1aa62b372c3
Step 6 / 6 : CMD [ "catalina.sh" , "run" ]
-- - > Running in a6587e561f64
Removing intermediate container a6587e561f64
-- - > 0 cd85211e13f
Successfully built 0 cd85211e13f
Successfully tagged tomcat: 1.2
[ root@localhost tomcat] #
7.4 基于Tomcat镜像运行并查看Tomcat容器
7.4 .1 运行
[ root@localhost tomcat] # docker run -- name tomcat2 - d - p 8081 : 8080 tomcat: 1.2
a466e18d3a58c106e9ff2a8041b6197738bdc441811f6583cfec1742604d4822
[ root@localhost tomcat] #
7.4 .2 查看
[ root@localhost tomcat] # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a466e18d3a58 tomcat: 1.2 "catalina.sh run" 4 seconds ago Up 3 seconds 0.0 .0 .0 : 8081 - > 8080 / tcp tomcat2
7.5 打开浏览器输入 IP:8081 验证