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

相关推荐
雪域迷影2 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
莹雨潇潇3 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
tangdou3690986557 小时前
Docker系列-5种方案超详细讲解docker数据存储持久化(volume,bind mounts,NFS等)
docker·容器
嘟嘟太菜了8 小时前
使用阿里云试用资源快速部署web应用-dofaker为例
阿里云·云计算
漫无目的行走的月亮10 小时前
在Docker中运行微服务注册中心Eureka
docker
大道归简13 小时前
Docker 命令从入门到入门:从 Windows 到容器的完美类比
windows·docker·容器
sealaugh3213 小时前
aws(学习笔记第一课) AWS CLI,创建ec2 server以及drawio进行aws画图
笔记·学习·aws
zeruns80214 小时前
如何搭建自己的域名邮箱服务器?Poste.io邮箱服务器搭建教程,Linux+Docker搭建邮件服务器的教程
linux·运维·服务器·docker·网站
爱跑步的程序员~14 小时前
Docker
docker·容器
疯狂的大狗14 小时前
docker进入正在运行的容器,exit后的比较
运维·docker·容器