Search code examples
amazon-ecsfabric8

How to push docker image to Amazon ECR using io.fabric8 maven plugin with authorization


I have a plugin to create a image, once created it need to be pushed to amazon ECR Please look into my plugin in the below`

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
   <groupId>io.fabric8</groupId>
   <artifactId>docker-maven-plugin</artifactId>
   <version>0.24.0</version>
   <configuration>
      <dockerHost>https://accountID.dkr.ecr.us-east-1.amazonaws.com</dockerHost>
      <authConfig>
         <authToken>authorization Token</authToken>
         <username>Access Key ID</username>
         <password>Secret Key Id</password>
      </authConfig>
      <images>
         <image>
            <alias>service</alias>
            <name>${project.artifactId}</name>
            <build>
               <from>openjdk:8-jdk-alpine</from>
               <entryPoint>
                  <exec>
                     <arg>java</arg>
                     <arg>-jar</arg>
                     <arg>maven/app.jar</arg>
                  </exec>
               </entryPoint>
               <assembly>
                  <descriptorRef>artifact-with-dependencies</descriptorRef>
               </assembly>
            </build>
         </image>
      </images>
   </configuration>
   <executions>
      <execution>
         <id>docker-build</id>
         <goals>
            <goal>build</goal>
         </goals>
      </execution>
   </executions>
</plugin>

`

I have tried with above plugin with authorization token as authtoken . when i am running maven build getting not authorized .

Help will be appreciated

Thanks, Damodar


Solution

  • You can use the AWS ECR credential helper

    Please read the documentation for further details, but here are the major steps to get it working:

    1. AWS credentials are set up (e.g. ~/.aws/credentials)
    2. install the ECR credential helper
    3. Adapt your ~/.docker/config.json (for us it has been the second mentioned option with specific aws account id)
    {
      "credHelpers": {
        "accountID.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
      },
      ... (already existing stuff in my setup)
    }
    
    1. Use correct environment variables in the shell where the fabric8 maven-docker-plugin is executed:
    AWS_SDK_LOAD_CONFIG=true
    AWS_PROFILE=your_aws_profile
    

    Maybe you will need to define AWS_REGION as environment variable as well.

    Now when running the maven build, the ECR credential helper should take care of the authentication process, so the "authConfig"-part of the config in the question can be removed.