title: Docker Python library cannot properly login into AWS ECR
body: <p>I wrote a Python script to pull images from AWS ECR to an Ubuntu instance. On this instance, I run docker commands with <code>sudo</code> as Docker is not setup as a non-root user.</p>
  
  <p>I do use <code>sudo</code> when invoking the script. What I find is if I am currently logged into AWS ECR first and then I run the script, it works as expected. However, if I am not logged in, and the auth token is expired, it appears that <code>docker login</code> works, but when I try and pull I get a message indicating that the "repository does not exist or my require 'docker login'".</p>
  
  <p>Examining logs, it verifies this with </p>
  
  <blockquote>
    <p>Feb 15 06:00:38 ubuntu-xenial dockerd[1388]:
    time="2019-02-15T06:00:38.832827449Z" level=error msg="Not continuing
    with pull after error: denied: Your Authorization Token has expired.
    Please run 'aws ecr get-login --no-include-email' to fetch a new one."</p>
  </blockquote>
  
  <pre><code>def log_into_aws_ecr(docker_client, region):
      # To do, set region
      ecr_client = boto3.client('ecr', region_name=region)
  
      # Get all repos
      response = ecr_client.describe_repositories()
      repo_names = []
      repositories = response.get('repositories', [])
      for repo in repositories:
          name = repo.get('repositoryName', '')
          if len(name):
              repo_names.append(name)
      token = ecr_client.get_authorization_token()
      username, password = base64.b64decode(token['authorizationData'][0]['authorizationToken']).decode('utf-8').split(":")
      registry_url = token['authorizationData'][0]['proxyEndpoint']
      login_results = docker_client.login(username, password, email='', registry=registry_url)
  
      prefix='https://'
      if registry_url.startswith(prefix):
          registry = registry_url[len(prefix):]
      else:
          registry = registry_url
      auth_config_payload = {'username': username, 'password': password }
      return ecr_client, repo_names, registry
  </code></pre>
  
  <p>Please note that this code is also being re-factored now, so there are some variables that are defined but not currently in use.</p>
  
  <p>The supplied <code>docker_client</code> is obtained via the line</p>
  
  <pre><code>    docker_client = docker.from_env()
  </code></pre>
  
  <p>I've tried running as</p>
  
  <p><code>sudo -E ./myscript.py image</code></p>
  
  <p>But this doesn't work either. I have a variant of this using a bash script and that works fine.</p>
  
  <p>Output for the <code>docker_client.login</code> looks like</p>
  
  <blockquote>
    <p>Looking for auth entry for 'ABCXYZ.dkr.ecr.us-west-2.amazonaws.com'
    Found 'ABCXYZ.dkr.ecr.us-west-2.amazonaws.com'</p>
  </blockquote>
  
  <p>And if I dump the response, it looks like this.</p>
  
  <blockquote>
    <p>{'password': 'PASSWORD HERE', 'email': None, 'username': 'AWS',
    'serveraddress': 'ABCXYZ.dkr.ecr.us-west-2.amazonaws.com'}</p>
  </blockquote>