使用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

相关文章

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

MySQL 衍生表(Derived Tables)的使用

《MySQL衍生表(DerivedTables)的使用》本文主要介绍了MySQL衍生表(DerivedTables)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学... 目录一、衍生表简介1.1 衍生表基本用法1.2 自定义列名1.3 衍生表的局限在SQL的查询语句select

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

MySQL分区表的具体使用

《MySQL分区表的具体使用》MySQL分区表通过规则将数据分至不同物理存储,提升管理与查询效率,本文主要介绍了MySQL分区表的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、分区的类型1. Range partition(范围分区)2. List partition(列表分区)3. H

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏