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

在Java项目中配置

pom.xml文件中加入以下内容:

<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的配置

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

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

[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

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.