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 | [root@localhost my_jar_project]# ls |
1.4 创建并编写Dockerfile文件
1 | # 基于centos镜像 |
1.5 构造镜像
1 | [root@localhost my_jar_project]# docker build -t my_jar_project:1.0 . |
1 | Sending build context to Docker daemon 162.5 MB |
1 | [root@localhost my_jar_project]# docker images |
1.6 使用镜像运行容器
1 | [root@localhost my_jar_project]# docker run -d -p 8080:8080 my_jar_project:1.0 /bin/bash |
1.7 测试
1 | [root@localhost my_jar_project]# ip addr |
打开浏览器,输入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 | [root@localhost my_war_project]# ls |
2.4 创建并编写Dockerfile文件
1 | # 基于之前自己制作的自定义tomcat镜像 |
2.5 构造镜像
1 | [root@localhost my_war_project]# docker build -t my_war_project:1.0 . |
1 | [root@localhost my_war_project]# docker images |
2.6 使用镜像运行容器
1 | [root@localhost my_war_project]# docker run -d -p 8080:8080 my_war_project:1.0 |
或者
1 | [root@localhost my_war_project]# docker run -it -d -p 8080:8080 my_war_project:1.0 |
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"}] |