使用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": {
    ".dkr.ecr..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>
        
        <java.version>1.8java.version>
        <docker.registry.host>${env.docker_registry_host}docker.registry.host>
        <docker.image.prefix>123456789012.dkr.ecr.ap-southeast-1.amazonaws.comdocker.image.prefix>
        <docker.image.name>${project.artifactId}docker.image.name>
        <docker.image.tag>${project.version}docker.image.tag>
        <docker.file>Dockerfiledocker.file>
    properties>
...

          <plugin>
              <groupId>com.spotifygroupId>
              <artifactId>dockerfile-maven-pluginartifactId>
              <version>1.4.13version>
              <configuration>
                  
                  <repository>123456789012.dkr.ecr.ap-southeast-1.amazonaws.com/${project.artifactId}repository>
                  <tag>${docker.image.tag}tag>
                  <dockerfile>${docker.file}dockerfile>
                  
                  <buildArgs>
                      <JAR_FILE>${project.build.finalName}.jarJAR_FILE>
                  buildArgs>
              configuration>
              <executions>
                  <execution>
                      <id>defaultid>
                      <phase>packagephase>
                      <goals>
                          <goal>buildgoal>
                          <goal>pushgoal>
                      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”

你可能感兴趣的:(AWS,Cloud,Java,maven,docker,aws,云计算)