一、spring项目打包成镜像服务器
1、增加docker插件依赖
首先Springboot工程需要在pom.xml文件增加对docker插件的支持,最新的插件是dockerfile-maven-plugin,原来的插件是docker-maven-plugin,现在不建议使用。
1 <plugin> 2 <groupId>com.spotify</groupId> 3 <artifactId>docker-maven-plugin</artifactId> 4 <version>1.0.0</version>
2、docker插件配置 :不用编写Dockerfile
1 <configuration> 2 <serverId>docker-aliyun</serverId> 3 <imageName>registry.cn-hangzhou.aliyuncs.com/<自己仓库的命名空间>/<仓库名称></imageName> 4 <imageTags> 5 <imageTag>${project.version}</imageTag> 6 <!--可以指定多个标签--> 7 <imageTag>${profileActive}</imageTag> 8 </imageTags> 9 <pushImage>true</pushImage> 10 <!--覆盖已存在的标签 镜像--> 11 <forceTags>true</forceTags> 12 <baseImage>java:8</baseImage> 13 <entryPoint>["java", "-Xms512m", "-Xmx512m", "-Denv=${env}", "-Duser.timezone=GMT+08", "-jar", "/${project.build.finalName}.jar"] 14 </entryPoint> 15 <resources> 16 <resource> 17 <targetPath>/</targetPath> 18 <directory>${project.build.directory}</directory> 19 <include>${project.build.finalName}.jar</include> 20 </resource> 21 </resources> 22 </configuration>
3、使用Maven命令进行打包
mvn clean package -P<相应的资源环境profile id> 打成Jar包
mvn docker:build -P<相应的资源环境 profile id> 生产docker镜像文件,并构建上传
遇到的错误:
1)上传push的时候 [WARNING] Failed to push registry.cn-hangzhou.aliyuncs.com/sfwl/sfwl-pay-boss, retrying in 10 seconds (1/5).
解决方案:你先等登录用户 $ sudo docker login --username=阿里云账号 registry.cn-hangzhou.aliyuncs.com 登陆成功后,会在本地服务器的.docker/config.json 加入一条凭证,如下图
1 "auths": { 2 "registry-vpc.cn-hangzhou.aliyuncs.com": { 3 "auth": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 4 }, 5 "registry.cn-hangzhou.aliyuncs.com": { 6 "auth": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 7 } 8 } 9
2) docker: Error response from daemon: pull access denied for registry-vpc.cn-hangzhou.aliyuncs.com/sfwl/sfwl-pay-boss, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://8a948t7x.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker