首页 > 解决方案 > CannotPullContainerError:启动 ECS 任务时出现上下文取消错误

问题描述

我正在使用 Fargate 启动 ECS 任务,并且容器在 PENDING 几分钟后最终处于 STOPPED 状态。状态给出以下错误消息:

CannotPullContainerError: context canceled

我正在使用 PrivateLink 允许 ECS 主机与 ECR 注册表对话,而无需通过公共 Internet,这就是它的配置方式(无服务器语法增强 CloudFormation):

      Properties:
        PrivateDnsEnabled: true
        ServiceName: com.amazonaws.ap-southeast-2.ecr.dkr
        SubnetIds:
          - { Ref: frontendSubnet1 }
          - { Ref: frontendSubnet2 }
        VpcEndpointType: Interface
        VpcId: { Ref: frontendVpc }

关于导致错误的任何想法?

标签: amazon-web-servicesaws-ecr

解决方案


您是否还添加了 S3 端点?这是我的模板的一个工作片段,我能够通过 aws 支持解决这个问题:

  EcrDkrEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
  PrivateDnsEnabled: true
  SecurityGroupIds: [!Ref 'FargateContainerSecurityGroup']
  ServiceName: !Sub 'com.amazonaws.${AWS::Region}.ecr.dkr'
  SubnetIds: [!Ref 'PrivateSubnetOne', !Ref 'PrivateSubnetTwo']
  VpcEndpointType: Interface
  VpcId: !Ref 'VPC'

对于 S3,您需要知道路由表是必要的 - 通常您希望使用与 Internet 网关相同的路由表,其中包含路由 0.0.0.0/0

  S3Endpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
  ServiceName: !Sub 'com.amazonaws.${AWS::Region}.s3'
  VpcEndpointType: Gateway
  VpcId: !Ref 'VPC'
  RouteTableIds: [!Ref 'PrivateRouteTable'] 

如果没有 cloudwatch 的端点,您将再次失败,这也是必要的:

  CloudWatchEndpoint:
Type: 'AWS::EC2::VPCEndpoint'
Properties:
  PrivateDnsEnabled: true
  SecurityGroupIds: [!Ref 'FargateContainerSecurityGroup']
  ServiceName: !Sub 'com.amazonaws.${AWS::Region}.logs'
  SubnetIds: [!Ref 'PrivateSubnetOne', !Ref 'PrivateSubnetTwo']
  VpcEndpointType: Interface
  VpcId: !Ref 'VPC'

编辑:私有路由表:

  PrivateRoute:
Type: AWS::EC2::Route
DependsOn: InternetGatewayAttachement
Properties:
  RouteTableId: !Ref 'PublicRouteTable'
  DestinationCidrBlock: '0.0.0.0/0'
  GatewayId: !Ref 'InternetGateway'

推荐阅读