Maven插件自动构建项目镜像

kyaa111 3年前 ⋅ 630 阅读

搭建docker私有仓库

docker pull registry

启动容器

docker run ‐di --name=registry -p 5000:5000 registry

打开浏览器 输入地址http://localhost:5000/v2/_catalog看到{"repositories":[]}表示私有仓库搭建成功并且内容为空

修改daemon.json

vim /etc/docker/daemon.json

添加内容

{"insecure‐registries":["localhost:5000"]}

重启docker

systemctl restart docker

docker start registry

测试

docker pull openjdk:8-jdk-alpine
docker tag openjdk:8-jdk-alpine localhost:5000/openjdk:8
docker push localhost:5000/openjdk:8

重新打开 http://localhost:5000/v2/_catalog

发现多出来openjdk, 就说明成功

修改其他配置

vim /lib/systemd/system/docker.service

其中ExecStart=后添加配置 ‐H tcp://0.0.0.0:2375 ‐H unix:///var/run/docker.sock

然后

systemctl daemon-reload
systemctl restart docker
docker start registry

DockerMaven插件

在pom文件中添加如下代码

与dependencies同级

<build>
        <finalName>blog-eureka</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.blog.eureka.EurekaApplication</mainClass>
                    <executable>true</executable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>${docker-maven-plugin}</version>
                <configuration>
                    <imageName>localhost:5000/${project.build.finalName}</imageName>
                    <baseImage>localhost:5000/openjdk:8</baseImage>
                    <cmd>["java", "-version"]</cmd>
                    <entryPoint>["java", "-jar", "/usr/${project.build.finalName}.jar"]</entryPoint>
                    <!-- 如果要配置标签, 则后面构建的命令不一样 -->
                    <!-- <imageTags>
                        <imageTag>tags</imageTag>
                    </imageTags> -->
                    <!-- 根据服务器的ip配置 -->
                    <dockerHost>http://localhost:2375/</dockerHost>

                    <resources>
                        <resource>
                            <targetPath>/usr</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

最后进入工程目录下

cd blog-eureka

mvn clean package docker:build -DpushImage

如果配置了镜像标签, 使用这个命令

mvn clean package docker:build -DpushImageTag

等待

BUILD SUCCESS

提示

打开http://localhost:5000/v2/_catalog,多出来个blog-eureka

查看

docker images

能看到新打包的镜像

启动容器

docker run -d --name=eureka -p 6001:6001 localhost:5000/blog-eureka

访问成功