title: Using Gradle plugin to push docker images to ECR
body: <p>I am using <a href="https://github.com/bmuschko/gradle-docker-plugin" rel="nofollow noreferrer">gradle-docker-plugin</a> to build and push docker images to Amazon's ECR. To do this I am also using a remote docker daemon running on an EC2 instance. I have configured a custom task <code>EcrLoginTask</code> to fetch the ECR authorization token using <code>aws-java-sdk-ecr</code> library.  Relevant  code looks like : - </p>
  
  <pre><code>class EcrLoginTask extends DefaultTask {
      String accessKey
      String secretCode
      String region
      String registryId
  
      @TaskAction
      String getPassword() {
          AmazonECR ecrClient = AmazonECRClient.builder()
                  .withRegion(Regions.fromName(region))
                  .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretCode))).build()
          GetAuthorizationTokenResult authorizationToken = ecrClient.getAuthorizationToken(
                  new GetAuthorizationTokenRequest().withRegistryIds(registryId))
          String token = authorizationToken.getAuthorizationData().get(0).getAuthorizationToken()
          System.setProperty("DOCKER_PASS", token) // Will this work ?
          return token
      }
  
  }
  
  buildscript {
      repositories {
          mavenCentral()
      }
      dependencies {
          classpath 'com.amazonaws:aws-java-sdk-ecr:1.11.244'
          classpath 'com.bmuschko:gradle-docker-plugin:3.2.1'
      }
  }
  
  
  
  docker {
      url = "tcp://remote-docker-host:2375"
      registryCredentials {
          username = 'AWS'
          password = System.getProperty("DOCKER_PASS")    // Need to provide at runtime !!!
          url = 'https://123456789123.dkr.ecr.eu-west-1.amazonaws.com'
      }
  }
  
  task getECRPassword(type: EcrLoginTask) {
      accessKey AWS_KEY
      secretCode AWS_SECRET
      region AWS_REGION
      registryId '139539380579'
  }
  
  task dbuild(type: DockerBuildImage) {
      dependsOn build
      inputDir = file(".")
      tag "139539380579.dkr.ecr.eu-west-1.amazonaws.com/n6duplicator"
  }
  
  task dpush(type: DockerPushImage) {
      dependsOn dbuild, getECRPassword
      imageName "123456789123.dkr.ecr.eu-west-1.amazonaws.com/n6duplicator"
  }
  </code></pre>
  
  <p>The remote docker connection works fine, ECR token is also fetched successfully and the <code>dbuild</code> task also gets executed successfully. </p>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>The <code>dpush</code> task fails - "Could not push image: no basic auth credentials"</p>
  
  <p>I believe this is because the authorization token received using the <code>EcrLoginTask</code> was not passed on to  in the <code>docker</code> configuration closure <code>password</code> property.</p>
  
  <p>How do I fix it ? I need to provide the credentials on the fly each time the build is executed.</p>