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

文章目录

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

小结

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

问题解决

AWS ECR (Elastic Container Registry)的登录

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

shell 复制代码
[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

shell 复制代码
[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就会更新为以下:

shell 复制代码
{
  "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内容:

shell 复制代码
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,如以下,问题解决。

shell 复制代码
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指令进行测试。

shell 复制代码
[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测试:

shell 复制代码
[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记录如下:

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云:

shell 复制代码
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"

相关推荐
IT技术分享社区11 分钟前
C#实战:使用腾讯云识别服务轻松提取火车票信息
开发语言·c#·云计算·腾讯云·共识算法
尘浮生2 小时前
Java项目实战II基于Spring Boot的光影视频平台(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·后端·maven·intellij-idea
川石课堂软件测试3 小时前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
九河云7 小时前
AWS账号注册费用详解:新用户是否需要付费?
服务器·云计算·aws
神一样的老师7 小时前
利用亚马逊AWS IoT核心和MQTT进行数据采集的综合指南
云计算·aws
追风林10 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
aloha_78910 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
城南vision12 小时前
Docker学习—Docker核心概念总结
java·学习·docker
wclass-zhengge12 小时前
Docker篇(Docker Compose)
运维·docker·容器
尢词12 小时前
SpringMVC
java·spring·java-ee·tomcat·maven