原创

使用Docker打包maven项目的两种姿势

温馨提示:
本文最后更新于 2019年10月18日,已超过 1,650 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

前言

注意事项1

在Windows安装的maven目录下的setting.xml文件中配置插件白名单。

  <!-- 自定义插件白名单 -->
  <pluginGroups>
    <pluginGroup>com.spotify</pluginGroup>
  </pluginGroups>

注意事项2

首先不管是方法一还是方法二都得开放远程服务。

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

添加-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \

方法一

1.配置Windows的环境变量

DOCKER_HOST=tcp://192.168.200.110:2375

或者
远程使用Docker.png

2.添加maven插件配置

    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <!-- 镜像名称,我使用项目的名称 -->
                    <repository>${project.name}</repository>
                    <!-- 镜像标签,我使用项目的版本号 -->
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定main方法入口 -->
                    <mainClass>com.lzhpo.springbootdockerdemo2.SpringbootDockerDemo2Application</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

3.编写Dockerfile文件

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

4.打包镜像

mvn clean package dockerfile:build -DskipTests

方法二

使用这个插件不需要dockerfile 直接在maven中定义dockerfile的逻辑 官网建议使用 dockerfile-maven-plugin插件 这个插件可以不配置环境变量 在maven中配置pom.xml修改如下,与配置的属性和dockerfile基本一致。

1.编写maven的pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>1.1.1</version>
            <configuration>
                <!-- 镜像名称 -->
                <imageName>springboot-docker-demo1</imageName>
                <!-- 远程仓库的地址和端口。dockerHost必须是http,不能是tcp。 -->
                <dockerHost>http://192.168.200.110:2375</dockerHost>
                <!-- 使用的是maven -->
                <baseImage>maven</baseImage>
                <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                <!-- copy the service's jar file from target into the root directory of the image -->
                <resources>
                    <resource>
                        <targetPath>/</targetPath>
                        <directory>${project.build.directory}</directory>
                        <include>${project.build.finalName}.jar</include>
                    </resource>
                </resources>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 指定main方法入口 -->
                <mainClass>com.lzhpo.demo1.Demo1Application</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

2.打包镜像

3.Linux控制台

总结方法一和二

方法一使用的是dockerfile-maven-plugin插件,需要编写Dockerfile文件,Windows需要配置环境变量DOCKER_HOST

方法二直接使用docker-maven-plugin插件,配置好远程的Docker服务地址和端口(如果Docker不是安装在localhost)。

将镜像推送到阿里云仓库

# 登录阿里云Docker Registry
[root@lzhpo ~]# docker login --username=15083759163 registry.cn-hangzhou.aliyuncs.com

# 查看本地的Docker 镜像,springboot-docker-demo1、springboot-docker-demo2是我刚才打包的,我就用springboot-docker-demo1来演示
[root@lzhpo ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
springboot-docker-demo2       0.0.1-SNAPSHOT      8e683f99070d        33 minutes ago      122 MB
springboot-docker-demo1       latest              b027189125c8        About an hour ago   633 MB
docker.io/maven               latest              e941463218b9        4 weeks ago         616 MB
docker.io/zookeeper           3.5                 eb0a01329223        4 weeks ago         225 MB
docker.io/openzipkin/zipkin   latest              69391b73bd3e        7 weeks ago         253 MB
docker.io/rabbitmq            management          7601e834fa14        2 months ago        177 MB
docker.io/mongo               latest              cdc6740b66a7        2 months ago        361 MB
docker.io/redis               latest              857c4ab5f029        2 months ago        98.2 MB
docker.io/logstash            6.8.1               b6a9d6f1254e        4 months ago        855 MB
docker.io/elasticsearch       6.8.1               446946b24cdd        4 months ago        899 MB
docker.io/openjdk             8-jdk-alpine        a3562aa0b991        5 months ago        105 MB

# 将本地的Docker的springboot-docker-demo1(或使用镜像ID)的标签与registry.cn-hangzhou.aliyuncs.com/lzhpo/springboot-docker-demo:latest关联起来(会自动创建阿里云端的)
[root@lzhpo ~]# docker tag springboot-docker-demo1 registry.cn-hangzhou.aliyuncs.com/lzhpo/springboot-docker-demo:latest

# 提交push到阿里云仓库
[root@lzhpo ~]# docker push registry.cn-hangzhou.aliyuncs.com/lzhpo/springboot-docker-demo:latest
本文目录