maven构建docker镜像推送至私有docker仓库
1、创建私有docker仓库
2、在maven项目中,添加【Dockerfile】文件,【Dockerfile】文件一般放置在项目main/docker目录下
FROM openjdk:8 MAINTAINER Sunny 914204907@qq.com VOLUME /tmp ADD springboot-test.jar app.jar RUN bash -c 'touch /app.jar' RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone EXPOSE 18889 ENTRYPOINT ["java", "-jar", "app.jar"]
3、修改maven的setting文件,添加docker私有仓库的账号密码
<servers> <server> <id>docker-hub</id> <username>docker-user</username> <password>123456</password> <configuration> <email>914204907@qq.com</email> </configuration> </server> </servers>
4、修改pom文件
<build> <finalName>springboot-test</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- Docker --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <!-- 将插件绑定在某个phase执行 --> <executions> <execution> <id>build-image</id> <!-- 用户只需执行mvn package ,就会自动执行mvn docker:build --> <phase>package</phase> <goals> <goal>build</goal> <!--build命令,相当于docker的build命令 会读取Dockerfile来构件docker镜像--> </goals> </execution> <execution> <id>image-tag</id> <phase>package</phase> <goals> <goal>tag</goal> <!--tag命令,相当于docker的tag命令--> </goals> <configuration> <image>cxw/${project.build.finalName}</image> <!--镜像名--> <newName>192.168.2.216:5000/cxw/${project.build.finalName}</newName> <!--打的标签名--> </configuration> </execution> <execution> <id>package-push</id> <phase>package</phase> <goals> <goal>push</goal> <!--相当于docker的push命令!!! 这里会去maven的setting文件中根据configuration/serverId标签的id,读取到docker私有仓库的用户名和密码--> </goals> <configuration> <imageName>192.168.2.216:5000/cxw/${project.build.finalName}</imageName> <!--要push的镜像名--> </configuration> </execution> </executions> <configuration> <!-- 指定生成的镜像名 --> <imageName>cxw/${project.build.finalName}</imageName> <!-- 指定标签 --> <imageTags> <imageTag>${project.version}</imageTag> </imageTags> <!-- 指定 Dockerfile 路径 --> <dockerDirectory>src/main/docker</dockerDirectory> <!-- 指定远程 docker api地址 --> <dockerHost>http://192.168.2.216:2375</dockerHost> <authConfig> <username>docker-user</username> <username>123456</username> </authConfig> <resources> <resource> <targetPath>/</targetPath> <!-- jar包所在的路径此处配置的对应target目录 --> <directory>${project.build.directory}</directory> <!-- 需要包含的jar包,这里对应的是Dockerfile中添加的文件名 --> <include>${project.build.finalName}.jar</include> </resource> </resources> <serverId>docker-hub</serverId> <!--mvn setting.xml配置的那个id--> </configuration> </plugin> </plugins> </build>
手动推送docker镜像至私有仓库的方式
1、给镜像打tag
docker image ls
session-web:latest
docker tag session-web:latest 127.0.0.1:5000/session-web:latest
2、推送镜像
docker push 127.0.0.1:5000/session-web:latest