使用Dockerfile Maven Plugin 将Docker镜像Push到AWS ECR (Elastic Container Registry)

本文主要是介绍使用Dockerfile Maven Plugin 将Docker镜像Push到AWS ECR (Elastic Container Registry),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 小结
  • 问题解决
    • AWS ECR (Elastic Container Registry)的登录
    • 问题 pull access denied for jdk, repository does not exist
    • 问题 Could not acquire image ID or digest following build
    • dockerfile-maven-plugin 使用
  • 参考

小结

本文记录使用Dockerfile Maven Plugin 将Docker镜像Push到AWS ECR (Elastic Container Registry),碰到了一些问题,并进行了解决。

问题解决

AWS ECR (Elastic Container Registry)的登录

使用以下AWS指令来登录AWS ECR:

[ec2-user@ip-10-0-3-241 ~]$ export AWS_ACCESS_KEY_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx..."
[ec2-user@ip-10-0-3-241 ~]$ 
[ec2-user@ip-10-0-3-241 ~]$ export AWS_SECRET_ACCESS_KEY="yyyyyyyyyyyyyyyyyyyyyyyyyyy..."
[ec2-user@ip-10-0-3-241 ~]$ 
[ec2-user@ip-10-0-3-241 ~]$ export AWS_SESSION_TOKEN="zzzzzzzzzzzzzzzzzzzzzz..."[ec2-user@ip-10-0-3-241 ~]$ aws ecr get-login-password --region ap-southeast-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-southeast-1.amazonaws.com

登录后密钥会存放主在这里~/.docker/config.json

[ec2-user@ip-10-0-3-241 ~]$ cat ~/.docker/config.json 
{"auths": {"123456789012.dkr.ecr.ap-southeast-1.amazonaws.com": {"auth": "jjjjjjjjjjjjjjjjjjjjjjjjjjjjj..."}}
}

如果使用credHelpers ,那么~/.docker/config.json就会更新为以下:

{"credHelpers": {"<ecr-id>.dkr.ecr.<aws-region>.amazonaws.com": "ecr-login"},//...
}

问题 pull access denied for jdk, repository does not exist

问题:
DOCKER error: Pull access denied for frolvlad/alpine-oraclejdk8, repository does not exist or may require 'docker login'

可能由于仓库不存在了,这里显示jdk找不到了。

进行以下修改,原本的Dockerfile内容:

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD spring-petclinic-rest-1.7.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

修改为 openjdk,如以下,问题解决。

FROM openjdk:8
VOLUME /tmp
ADD target/spring-petclinic-rest-1.7.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

问题 Could not acquire image ID or digest following build

Could not acquire image ID or digest following build的问题可能由以下几个原因导致:

  • dockerfile-maven-plugin版本较低,需要从1.3.×升级到1.4.×即可,这已经使用了1.4.13,所以应该不是这个原因导致的。
  • 需要将Dockerfile放到与`pom.xml``的根目录,进行了尝试,然而并没有解决问题。
  • 那么最后主不剩下最后一个可能,Dockerfile配置错误,需要进行排查。

排查Dockerfile配置错误比较麻烦,需要使用docker指令进行测试。

[ec2-user@ip-10-0-3-241 ~]$ docker build  --file Dockerfile --tag  123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/spring-petclinic-rest:1.7 .Sending build context to Docker daemon  36.96MB
Step 1/6 : FROM openjdk:8
8: Pulling from library/openjdk
001c52e26ad5: Pull complete 
d9d4b9b6e964: Pull complete 
2068746827ec: Pull complete 
9daef329d350: Pull complete 
d85151f15b66: Pull complete 
52a8c426d30b: Pull complete 
8754a66e0050: Pull complete 
Digest: sha256:86e863cc57215cfb181bd319736d0baf625fe8f150577f9eb58bd937f5452cb8
Status: Downloaded newer image for openjdk:8---> b273004037cc
Step 2/6 : VOLUME /tmp---> Running in 57e01b20a90c
Removing intermediate container 57e01b20a90c---> 0f6cbb5eebe2
Step 3/6 : ADD spring-petclinic-rest-1.7.jar app.jar
ADD failed: file not found in build context or excluded by .dockerignore: stat spring-petclinic-rest-1.7.jar: file does not exist

以上Dockerfile配置路配置有些问题,进行修改,解决问题。

使用以下指令进行Push测试:

[ec2-user@ip-10-0-3-241 ~]$ docker tag 123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/spring-petclinic-rest:1.7 123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/spring-petclinic-rest:latest
[ec2-user@ip-10-0-3-241 ~]$ docker images
REPOSITORY                                                                TAG            IMAGE ID       CREATED         SIZE
123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/spring-petclinic-rest   1.7            251a9e43ecdb   6 hours ago     594MB
123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/spring-petclinic-rest   latest         251a9e43ecdb   6 hours ago     594MB
[ec2-user@ip-10-0-3-241 ~]$ docker push 123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/spring-petclinic-rest:latest
The push refers to repository [123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/spring-petclinic-rest]
f07704dde9be: Layer already exists 
f93e29e9f576: Layer already exists 
6b5aaff44254: Layer already exists 
53a0b163e995: Layer already exists 
b626401ef603: Layer already exists 
9b55156abf26: Layer already exists 
293d5db30c9f: Layer already exists 
03127cdb479b: Layer already exists 
9c742cd6c7a5: Layer already exists 
latest: digest: sha256:d688043cdeaa1a28b43e6f8ede753175cbb9be02f12545df2518bf45b7502ae5 size: 2219

dockerfile-maven-plugin 使用

pom.xml记录如下:

    <properties><!-- Generic properties --><java.version>1.8</java.version><docker.registry.host>${env.docker_registry_host}</docker.registry.host><docker.image.prefix>123456789012.dkr.ecr.ap-southeast-1.amazonaws.com</docker.image.prefix><docker.image.name>${project.artifactId}</docker.image.name><docker.image.tag>${project.version}</docker.image.tag><docker.file>Dockerfile</docker.file></properties>
...<plugin><groupId>com.spotify</groupId><artifactId>dockerfile-maven-plugin</artifactId><version>1.4.13</version><configuration><!-- repository>${docker.image.prefix}/${docker.image.name}</repository --><repository>123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/${project.artifactId}</repository><tag>${docker.image.tag}</tag><dockerfile>${docker.file}</dockerfile><!-- dockerfile>Dockerfile</dockerfile --><buildArgs><JAR_FILE>${project.build.finalName}.jar</JAR_FILE></buildArgs></configuration><executions><execution><id>default</id><phase>package</phase><goals><goal>build</goal><goal>push</goal></goals></execution></executions></plugin>...

运行maven指令可以成功编译并将image推送到AWS云:

mvn clean package dockerfile:build -DpushImage -Dmaven.test.skip=true

参考

Dockerfile Maven Plugin Repository
Github: Build and push docker image to Amazon from maven
Github: amazon-ecs-java-microservices/1_ECS_Java_Spring_PetClinic/
Github: spring-projects/spring-petclinic
Github: spotify/docker-maven-plugin
stackoverflow: DOCKER error: Pull access denied for coffeeteareadb, repository does not exist or may require ‘docker login’
CSDN: Error response from daemon: pull access denied for jdk, repository does not exist or may require ‘do
简书: win10下dockerfile插件执行失败
简书: 使用dockerfile-maven-plugin打包maven项目为docker image
Sanduo: spotify dockerfile-maven-plugin ‘Could not acquire image ID or digest following build’错误解决 _
Github: Could not acquire image ID or digest following build #282
Github: Could not acquire image ID or digest following build #216
FreeKB: Docker - Build an image using a Dockerfile
stackoverlfow: Error when logging into ECR with Docker login: “Error saving credentials… not implemented”

这篇关于使用Dockerfile Maven Plugin 将Docker镜像Push到AWS ECR (Elastic Container Registry)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/474005

相关文章

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

使用Python合并 Excel单元格指定行列或单元格范围

《使用Python合并Excel单元格指定行列或单元格范围》合并Excel单元格是Excel数据处理和表格设计中的一项常用操作,本文将介绍如何通过Python合并Excel中的指定行列或单... 目录python Excel库安装Python合并Excel 中的指定行Python合并Excel 中的指定列P

浅析Rust多线程中如何安全的使用变量

《浅析Rust多线程中如何安全的使用变量》这篇文章主要为大家详细介绍了Rust如何在线程的闭包中安全的使用变量,包括共享变量和修改变量,文中的示例代码讲解详细,有需要的小伙伴可以参考下... 目录1. 向线程传递变量2. 多线程共享变量引用3. 多线程中修改变量4. 总结在Rust语言中,一个既引人入胜又可