IDEA 整合 Docker:打包 SpringBoot 应用,直接构建镜像,启动容器

安装/更新 Docker

安装/更新 Docker 就不说了,详情见Linux 安装/更新 Docker

修改 Docker 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1. 查看是否是最新版本,如果不是请升级(参考上面的:”安装/更新 Docker“)
cat /lib/systemd/system/docker.service | grep "containerd.sock"

# 2. 先查看是否有备份文件,不存在就进行备份一下
ll /lib/systemd/system/docker.service*
cp /lib/systemd/system/docker.service /lib/systemd/system/docker.service.bak

# 2. 修改配置文件。
sed -i 's/containerd.sock/containerd.sock -H 0.0.0.0:2375/g' /lib/systemd/system/docker.service

# 2. 重新加载配置文件
systemctl daemon-reload

# 3. 重启 Docker 服务
systemctl restart docker

# 4. 查看配置的端口号(2375)是否开启
netstat -atlunp | grep 2375

IDEA 安装 Docker 插件

在这里插入图片描述

配置 Docker 连接

在这里插入图片描述

配置 Docker Registry

(如果不知道仓库地址,用户名,密码。请查看下一步。)
在这里插入图片描述

查看自己的 Docker Registry

》》》登录 阿里云容器镜像服务

1
2
3
# 自己使用
docker login --username=陶攀峰闯天涯 registry.cn-beijing.aliyuncs.com
》》》YY密码

在这里插入图片描述

项目打包成镜像

pom.xml 控制(不推荐)

不符合 Dockerfile 编写规范,所以不推荐。

只要执行 mvn package 就触发。
下面是 SpringBoot pom.xml 的配置,具体含义都在下面注释了。

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<build>
<!-- 引用我们的项目名字 -->
<finalName>${project.artifactId}</finalName>

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>


<!--使用docker-maven-plugin插件-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<!--将插件绑定在某个phase执行-->
<executions>
<execution>
<id>build-image</id>
<!--用户只需执行mvn package ,就会自动执行mvn docker:build-->
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>

<configuration>
<!--指定生成的镜像名,这里是[作者名/项目名]-->
<imageName>taopanfeng/${project.artifactId}</imageName>

<!--指定标签 这里指定的是镜像的版本,我们默认版本是latest-->
<imageTags>
<imageTag>latest</imageTag>
</imageTags>

<!--指定基础镜像jdk1.8-->
<baseImage>java:8</baseImage>
<!-- 镜像制作人本人信息 -->
<!--<labels>-->
<!-- <label>陶攀峰</label>-->
<!-- <label>1801957499@qq.com</label>-->
<!--</labels>-->

<!--切换到 /app 目录(进入容器的默认目录)-->
<workdir>/app</workdir>

<!--查看我们的java版本-->
<cmd>["java", "-version"]</cmd>

<!--向外暴露端口-->
<exposes>
<expose>8080</expose>
</exposes>

<!--${project.build.finalName}.jar是打包后生成的jar包的名字-->
<cmd>["java", "-jar", "/app/${project.build.finalName}.jar"]</cmd>

<!--指定远程 docker api地址-->
<dockerHost>http://192.168.1.3:2375</dockerHost>

<!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
<resources>
<resource>
<!--directory 就是 package 生产的 target 目录-->
<!--include 用于指定文件-->
<!--targetPath 用于扔到容器的哪个位置-->
<!--总结:把 target 目录中的 xxx.jar 扔到容器的 /app 下面,而不是Linux的 /app 下面-->
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
<targetPath>/app</targetPath>
</resource>
</resources>

</configuration>
</plugin>
</plugins>
</build>

在这里插入图片描述

Dockerfile 控制(推荐)

2020-11-25 15:57:35

修改 pom.xml

dockerfile-maven-plugin 插件历史版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<build>
<!--项目打包之后:finalName.jar-->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--dockerfile maven插件-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
</plugin>
</plugins>
</build>

编写 Dockerfile

1
2
3
4
5
6
7
8
9
FROM java:8
# FROM adoptopenjdk/openjdk8:centos

LABEL name=TaoPanfeng

ADD target/*.jar /app.jar
# COPY target/*.jar /app.jar

CMD java -jar /app.jar

在这里插入图片描述

添加 Dockerfile 配置

在这里插入图片描述

编辑 Dockerfile 配置(重点)

从上往下按顺序看,很简单的。

在这里插入图片描述

执行 Dockerfile 配置。(构建镜像,创建容器)

在这里插入图片描述

查看日志

在这里插入图片描述

解决日志乱码问题

》》》解决 IDEA Docker插件 日志乱码问题
》》》Help -> Edit Custom VM Options -> 添加一行 -Dfile.encoding=utf-8
》》》重启 IDEA!!!重启 IDEA!!!重启 IDEA!!!
在这里插入图片描述

拉取镜像

1
2
1. 输入框,输入镜像名称之后,等待几秒,会显示有哪些镜像可供下载
2. 选择之后,按下 Ctrl + Enter 即可下载镜像

在这里插入图片描述

在这里插入图片描述

创建镜像仓库

在这里插入图片描述

推送镜像

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
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
taopanfeng/junit latest 3df1ad6e28fd 8 minutes ago 684MB
redis latest 74d107221092 39 hours ago 104MB
java 8 d23bdf5b1b1b 3 years ago 643MB
[root@localhost ~]# docker login --username=陶攀峰闯天涯 registry.cn-shanghai.aliyuncs.com
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost ~]#
[root@localhost ~]# docker tag 3df1ad6e28fd registry.cn-shanghai.aliyuncs.com/taopanfeng/junit:0.1
[root@localhost ~]#
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
taopanfeng/junit latest 3df1ad6e28fd 10 minutes ago 684MB
registry.cn-shanghai.aliyuncs.com/taopanfeng/junit 0.1 3df1ad6e28fd 10 minutes ago 684MB
redis latest 74d107221092 39 hours ago 104MB
java 8 d23bdf5b1b1b 3 years ago 643MB
[root@localhost ~]#
[root@localhost ~]# docker push registry.cn-shanghai.aliyuncs.com/taopanfeng/junit:0.1
The push refers to repository [registry.cn-shanghai.aliyuncs.com/taopanfeng/junit]
391b4e91e537: Layer already exists
7f7c53048701: Layer already exists
35c20f26d188: Layer already exists
c3fe59dd9556: Pushing [==============> ] 101.4MB/351.5MB
6ed1a81ba5b6: Layer already exists
a3483ce177ce: Layer already exists
ce6c8756685b: Layer already exists
30339f20ced0: Pushing [==================================================>] 126.6MB
0eb22bfb707d: Retrying in 1 second
a2ae92ffcd29: Pushing [=================> ] 43.97MB/123MB

在这里插入图片描述

拉取仓库镜像

使用另外一台机器测试

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
[root@localhost ~]# docker pull registry.cn-shanghai.aliyuncs.com/taopanfeng/junit:0.1
Trying to pull repository registry.cn-shanghai.aliyuncs.com/taopanfeng/junit ...
0.1: Pulling from registry.cn-shanghai.aliyuncs.com/taopanfeng/junit
7448db3b31eb: Pull complete
c36604fa7939: Pull complete
29e8ef0e3340: Pull complete
a0c934d2565d: Pull complete
a360a17c9cab: Pull complete
cfcc996af805: Pull complete
2cf014724202: Pull complete
4bc402a00dfe: Pull complete
89152796d5ad: Pull complete
f357d934ffb9: Pull complete
Digest: sha256:f7a913d19395a032f37f83125325385cfbac990dcab499e1fecec974d562903e
Status: Downloaded newer image for registry.cn-shanghai.aliyuncs.com/taopanfeng/junit:0.1
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-shanghai.aliyuncs.com/taopanfeng/junit 0.1 3df1ad6e28fd 9 hours ago 684 MB
...
[root@localhost ~]#
[root@localhost ~]# docker run --name junit01 -p 8080:8080 -d registry.cn-shanghai.aliyuncs.com/taopanfeng/junit:0.1
499a04e279d58476bb2c32ca0c2f7840a4c78b7b5b3bd6034444915579db4d76
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker ps -n1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
499a04e279d5 registry.cn-shanghai.aliyuncs.com/taopanfeng/junit:0.1 "java -jar /app/ju..." 8 seconds ago Up 6 seconds 0.0.0.0:8080->8080/tcp junit01
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# curl 127.0.0.1:8080/hello
Hello World! #===> 成功返回
[root@localhost ~]#

其他功能(略)

删除容器,查看容器信息,删除镜像,等… 就不演示了。