在项目中使用Docker Maven Plugin插件远程构建镜像

日常开发过程中,可以在项目的pom.xml文件中配置Docker容器,这样方便我们直接将项目打包成Docker镜像,发布到容器中.

在Java项目中配置

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
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>micro-deploy-docker</imageName>
<!--注意 修改成远程服务器地址-->
<dockerHost> http://192.168.10.11:2375</dockerHost>
<!-- Docker file 文件的所在目录 -->
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
</dependency>
</dependencies>
</plugin>

修改Docker的配置

1
vim /usr/lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd后面加上-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

1
2
3
4
5
6
7
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
ExecReload=/bin/kil ts HUP $MAINPID

项目的Dockerfile

1
2
3
4
5
6
7
8
9
FROM xxx.com/java:8
MAINTAINER xxx <xxx@gamil.com>
2019/8/16
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY ./pro/target/uve-engine-service-0.0.1-SNAPSHOT.jar /app/app.jar
COPY ./pro/start.sh /app
EXPOSE 8288
ENTRYPOINT ["/bin/bash","/app/start.sh"]

Done.