IDEA 使用Docker构建镜像
安装Docker
Windows
1、下载并安装Docker Desktop
2、勾选配置
3、配置参数
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"host": [
"0.0.0.0:2375"
],
"insecure-registries": [
"127.0.0.1:8888"//接不受信任的镜像仓库地址
],
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"https://cr.console.aliyun.com",
"https://mirror.ccs.tencentyun.com"
]
}
Linux
1、安装docker
2、开启tcp端口访问,执行命令sudo vim /lib/systemd/system/docker.service
修改配置文件
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375
3、重新加载配置systemctl daemon-reload
4、重启dockersystemctl restart docker
IDEA操作
1、IDEA安装docker插件并添加docker
2、在工程得pom.xml添加配置
<build>
<!-- jar包名称-->
<finalName>food-service</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 打包镜像名称 -->
<imageName></imageName>
<!-- 本地docker地址 -->
<dockerHost>http://localhost:2375</dockerHost>
<!-- Dockerfile路径 -->
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
3、添加dockerfile文件并配置
FROM 基础包
ADD target/服务名.jar /app.jar
ENV TZ Asia/Shanghai
ENTRYPOINT ["java","-jar","/app.jar"]
4、maven打包