JavaEE:用Maven Docker插件编译上传工程为Docker镜像
说明:用Maven Docker插件会将工程编译打包,并上传到docker私有仓库,生成一个镜像,需要手动创建启动此镜像的容器。
docker安装:https://blog.csdn.net/a526001650a/article/details/104177208
docker私有仓库安装:https://blog.csdn.net/a526001650a/article/details/107369195
一、让宿主机Docker允许被远程访问:
1.修改docker.service文件:
[root@localhost ~]# vi /lib/systemd/system/docker.service
在ExecStart第1行/usr/bin/dockerd-current后面增加内容:
ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \
2.刷新并重启docker让配置生效:
(1)刷新配置:
[root@localhost ~]# systemctl daemon-reload
(2)重启Docker(没启动则用启动命令):
[root@localhost ~]# systemctl restart docker
3.让防火墙允许2375端口通行:
[root@localhost ~]# firewall-cmd --zone=public --add-port=2375/tcp --permanent
[root@localhost ~]# firewall-cmd --reload
4.访问查看效果(显示json表示成功了):
http://192.168.233.128:2375/version
5.启动私有仓库(myreg为自定名的私有仓库):
[root@localhost ~]# docker start myreg
二、用Maven Docker插件编译打包工程模块,上传到Docker私有仓库,生成镜像:
1.创建一个普通web工程mall_usercenter:
application.yml:
server:
port: 10001
spring:
application:
name: mall_usercenter
UserCenterApplication.java:
//申明为引导类,类名自定义
@SpringBootApplication
public class UserCenterApplication {
public static void main(String[] args) {//应用入口
SpringApplication.run(UserCenterApplication.class, args); //运行自定义引导类
}
}
UserController.java(请求处理类):
@Controller
public class UserController {
@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
@ResponseBody
public String getUserInfo() {
return "获取用户信息成功";
}
}
2.在mall_usercenter工程的pom.xml中配置docker插件,用于自动打包并上传:
<project ...>
<build>
<!-- 应用名称 -->
<finalName>mall_usercenter</finalName>
<plugins>
<!-- springboot插件,其他插件的基础依赖 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- docker插件 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>192.168.233.128:5000/${project.artifactId}:${project.version}</imageName><!-- docker中的镜像名称 -->
<baseImage>docker.io/java</baseImage><!-- docker中的基础镜像名称 -->
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint><!-- 打成一个mall.jar -->
<resources><!-- docker打包规则,固定 -->
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<dockerHost>http://192.168.233.128:2375</dockerHost> <!-- docker供外部访问的IP与端口 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
"工程\target\docker\Dockerfile"文件内容如下(此文件自动生成的,不用创建):
FROM docker.io/java
ADD /mall_usercenter.jar //
ENTRYPOINT ["java", "-jar", "/mall_usercenter.jar"]
3.进入IntelliJ IDEA的Terminal命令行,输入以下命令(mvn命令要安装apache-maven,build编译,DpushImage上传):
...mall_usercenter>mvn clean package docker:build -DpushImage
三、查看效果:
1.创建已上传镜像的容器(192.168.233.128:5000/mall_usercenter:1.0-SNAPSHOT就是上传的镜像):
[root@localhost ~]# docker run -di --name=mall_usercenter -p 10001:10001 192.168.233.128:5000/mall_usercenter:1.0-SNAPSHOT
2.访问接口,看效果:
http://192.168.233.128:10001/getUserInfo
本文地址:https://blog.csdn.net/a526001650a/article/details/107383883
下一篇: Go语言结构体定义和使用方法