title: ECS Fargate task fails: CannotPullContainerError: inspect image has been retried 5 time(s): httpReaderSeeker: failed open: unexpected status code
body: <p>We have other ECS Services running which use images from our private ECR repo. However for our Services in the same cluster which are trying to pull from Docker Hub we are getting the following error:</p>
  <blockquote>
  <p>CannotPullContainerError: inspect image has been retried 5 time(s): httpReaderSeeker: failed open: unexpected status code https://registry-1.docker.io...: 4...</p>
  </blockquote>
  <p>(The message itself is truncated at the end: it is literally &quot;4...&quot;).</p>
  <p>Judging by the fact that it's getting a status code response, that suggests that it's able to talk to Docker Hub and it's not a network connectivity issue within our AWS configuration. We are trying to use an image in our ECS Task from a public repo, one is a Redis image and another is a Hasura image. I'm not sure how to see the status code itself since it's truncated in the AWS console.</p>
  <p>When I hit the <a href="https://registry-1.docker.io/v2/hasura/graphql-engine/manifests/sha256:0b62bc854e92f1859b5c9c19ce40c4bfaa643b65b1807e2cf55a4078db30b8a8" rel="nofollow noreferrer">URL</a> from the error in my browser this is the response:</p>
  <pre><code>{&quot;errors&quot;:[{&quot;code&quot;:&quot;UNAUTHORIZED&quot;,&quot;message&quot;:&quot;authentication required&quot;,&quot;detail&quot;:[{&quot;Type&quot;:&quot;repository&quot;,&quot;Class&quot;:&quot;&quot;,&quot;Name&quot;:&quot;hasura/graphql-engine&quot;,&quot;Action&quot;:&quot;pull&quot;}]}]}
  </code></pre>
  <p>I get a similar response with the Redis image. I didn't think we needed any authentication to pull public images - we've run ECS Tasks in the past without requiring authentication to Docker Hub?</p>
  <p><strong>For completeness, I've included the checks below for troubleshooting this error, however as mentioned, since we're getting a response code from Docker Hub it doesn't look like these checks are relevant.</strong></p>
  <p>AWS has this <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_cannot_pull_image.html" rel="nofollow noreferrer">guide to troubleshoot</a> 'CannotPullContainer' errors and for this particular error on Fargate there is <a href="https://aws.amazon.com/premiumsupport/knowledge-center/ecs-fargate-pull-container-error/" rel="nofollow noreferrer">this guide</a>. Here are the things from the guide we have checked:</p>
  <blockquote>
  <p>Confirm that your VPC networking configuration allows your Amazon ECS infrastructure to reach the image repository</p>
  </blockquote>
  <p>This ECS Task was in a private subnet, and it's route table had the following routes:</p>
  <pre><code>10.0.0.0/16 -&gt; local (active)
  0.0.0.0/0 -&gt; NAT Gateway (active)
  </code></pre>
  <p>The NAT Gateway has status available and an Elastic IP address assigned.</p>
  <blockquote>
  <p>Check the VPC DHCP Option Set</p>
  </blockquote>
  <p>Looking at the VPC and going to the DHCP options set we can see Domain name servers is set to: 'AmazonProvidedDNS'</p>
  <blockquote>
  <p>Check the task execution role permissions
  More details about configuring this are in <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html" rel="nofollow noreferrer">this guide</a>.</p>
  </blockquote>
  <p>The same IAM role is used in the task definition for both the 'task role' and 'task execution role.' This has been with the following default policy as defined in the guide mentioned:</p>
  <pre><code>{
      &quot;Version&quot;: &quot;2012-10-17&quot;,
      &quot;Statement&quot;: [
          {
              &quot;Sid&quot;: &quot;&quot;,
              &quot;Effect&quot;: &quot;Allow&quot;,
              &quot;Action&quot;: [
                  &quot;logs:PutLogEvents&quot;,
                  &quot;logs:CreateLogStream&quot;,
                  &quot;ecr:GetDownloadUrlForLayer&quot;,
                  &quot;ecr:GetAuthorizationToken&quot;,
                  &quot;ecr:BatchGetImage&quot;,
                  &quot;ecr:BatchCheckLayerAvailability&quot;
              ],
              &quot;Resource&quot;: &quot;*&quot;
          }
      ]
  }
  </code></pre>
  <blockquote>
  <p>Check that the image exists</p>
  </blockquote>
  <p><a href="https://hub.docker.com/layers/hasura/graphql-engine/v1.3.0/images/sha256-45a57d1e3170c6ca06b0c8f979822daafd77461e888daf3db584032aa1f821ba?context=explore" rel="nofollow noreferrer">This is the  image</a> we're trying to pull from Docker Hub. The image exists and I can pull it from my local machine without having to authenticate.</p>