搭建本地自动化部署环境

Docker开启远程访问

1
2
3
4
5
6
7
8
9
10
11
vim /lib/systemd/system/docker.service
#修改ExecStart这行
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
# 重新加载配置文件
systemctl daemon-reload
#重启服务
systemctl restart docker.service
#查看端口是否开启
netstat -nlpt
#直接curl看是否生效
curl http://127.0.0.1:2375/info

Idea中Docker配置

可以直接在idea->plug 或者在官网下载idea对应版本的的Docker integration 安装。安装后restart Idea 即可。

image-20210427092135832

重启后配置docker,连接到远程docker服务。从File->Settings->Build,Execution,Deployment->Docker打开配置界面。

image-20210427092826974

在项目pom文件中添加docker 依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${project.artifactId}</imageName>
<dockerDirectory></dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>

在项目根路径创建构建项目的Dockerfile文件

1
2
3
4
5
6
FROM java:8
VOLUME /tmp
COPY target/demo-0.0.1-SNAPSHOT.jar demo.jar
RUN bash -c "touch /demo.jar"
EXPOSE 8080
ENTRYPOINT ["java","-jar","demo.jar"]

4、创建Docker镜像

在idea右上角maven插件中执行package进行打包

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.429 s
[INFO] Finished at: 2021-04-27T09:54:50+08:00
[INFO] ------------------------------------------------------------------------

点击Docker,再点击+号,添加一个docker命令,输入Name,选择Server,选择Dockerfile文件,输入镜像tag,完成配置。
image-20210427113507420

点击run 就会提交并在docker上构建镜像

1
2
3
4
5
6
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.429 s
[INFO] Finished at: 2021-04-27T09:54:50+08:00
[INFO] ------------------------------------------------------------------------