Windows コンテナを ECS on EC2 の bridge モードで network bridge not found のエラーでタスク起動できないときの原因と対処方法

Windows コンテナがサポートされているネットワークモードは限定的です。通常の Linux コンテナ同様に設定するとタスク起動できません。
2022.01.19

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Windows コンテナを ECS on EC2 実行環境を利用し、タスク定義のネットワークモード bridge を指定しました。タスク起動(Windowsコンテナ起動)時にCannotStartContainerError: Error response from daemon: network bridge not foundのエラーで起動できない状況を調査する機会がありました。同エラー発生時の切り分けの参考用にまとめます。

検証環境

Windows の ECS on EC2 環境です。

項目 バージョン
ECS AMI ID ami-03c81c3cd5a64c9f7
ECS AMI Name Windows_Server-2019-English-Full-ECS_Optimized-2021.12.16
ECS Agent 1.57.1
Docker 20.10.7

状況

ECS on EC2、ECSのサービス、タスク定義の作成はマネージメントコンソールから行いました。 タスクを実行すると停止理由が Task failed to start で状況の理由には以下のメッセージが記録されます。

状況の理由

CannotStartContainerError: Error response from daemon: network bridge not found

問題のタスク定義全文

折りたたみ

タスク定義

{
  "ipcMode": null,
  "executionRoleArn": null,
  "containerDefinitions": [
    {
      "dnsSearchDomains": null,
      "environmentFiles": null,
      "logConfiguration": null,
      "entryPoint": [
        "powershell",
        "-Command"
      ],
      "portMappings": [
        {
          "hostPort": 8080,
          "protocol": "tcp",
          "containerPort": 80
        }
      ],
      "command": [
        "New-Item -Path C:\\inetpub\\wwwroot\\index.html -Type file -Value '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p>'; C:\\ServiceMonitor.exe w3svc"
      ],
      "linuxParameters": null,
      "cpu": 1024,
      "environment": [],
      "resourceRequirements": null,
      "ulimits": null,
      "dnsServers": null,
      "mountPoints": [],
      "workingDirectory": null,
      "secrets": null,
      "dockerSecurityOptions": null,
      "memory": 1024,
      "memoryReservation": null,
      "volumesFrom": [],
      "stopTimeout": null,
      "image": "mcr.microsoft.com/windows/servercore/iis",
      "startTimeout": null,
      "firelensConfiguration": null,
      "dependsOn": null,
      "disableNetworking": null,
      "interactive": null,
      "healthCheck": null,
      "essential": true,
      "links": null,
      "hostname": null,
      "extraHosts": null,
      "pseudoTerminal": null,
      "user": null,
      "readonlyRootFilesystem": null,
      "dockerLabels": null,
      "systemControls": null,
      "privileged": null,
      "name": "windows_sample_app"
    }
  ],
  "placementConstraints": [],
  "memory": "1024",
  "taskRoleArn": null,
  "compatibilities": [
    "EXTERNAL",
    "EC2"
  ],
  "taskDefinitionArn": "arn:aws:ecs:ap-northeast-1:123456789012:task-definition/windows-simple-iis:1",
  "family": "windows-simple-iis",
  "requiresAttributes": [],
  "pidMode": null,
  "requiresCompatibilities": [],
  "networkMode": "bridge",
  "runtimePlatform": null,
  "cpu": "1024",
  "revision": 1,
  "status": "ACTIVE",
  "inferenceAccelerators": null,
  "proxyConfiguration": null,
  "volumes": []
}

原因

Windows コンテナではネットワークモード設定はdefaultawsvpcのみサポートされています。bridgeはサポートされていません。

networkMode

Supported: Yes

When you register a task definition with Windows containers, you must use default or awsvpc network mode.

ということに気づけたのは以下の投稿により

この問題に遭遇するケース

マネージメントコンソールからタスク定義を作成するとネットワークモードbridgeを選択できます。 Windows コンテナか否かの判定はないため、Windows コンテナではサポートされていないネットワークモードを指定したタスク定義を作成できてしまう。

今回はマネージメントコンソールから簡素な検証環境を構築したくbridgeを選択した故に、Windows コンテナのネットワークモードサポート状況を調べることになりました。

対処

ネットワークモードを"networkMode": bridge,から"networkMode": null,と修正する。null指定はdefaultのネットワークモードを選択することを意味します。

タスク定義

{
  "ipcMode": null,
  "executionRoleArn": null,
  "containerDefinitions": [
    {
      "dnsSearchDomains": null,
      "environmentFiles": null,
      "logConfiguration": null,
      "entryPoint": [
        "powershell",
        "-Command"
      ],
      "portMappings": [
        {
          "hostPort": 8080,
          "protocol": "tcp",
          "containerPort": 80
        }
      ],
      "command": [
        "New-Item -Path C:\\inetpub\\wwwroot\\index.html -Type file -Value '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p>'; C:\\ServiceMonitor.exe w3svc"
      ],
      "linuxParameters": null,
      "cpu": 1024,
      "environment": [],
      "resourceRequirements": null,
      "ulimits": null,
      "dnsServers": null,
      "mountPoints": [],
      "workingDirectory": null,
      "secrets": null,
      "dockerSecurityOptions": null,
      "memory": 1024,
      "memoryReservation": null,
      "volumesFrom": [],
      "stopTimeout": null,
      "image": "mcr.microsoft.com/windows/servercore/iis",
      "startTimeout": null,
      "firelensConfiguration": null,
      "dependsOn": null,
      "disableNetworking": null,
      "interactive": null,
      "healthCheck": null,
      "essential": true,
      "links": null,
      "hostname": null,
      "extraHosts": null,
      "pseudoTerminal": null,
      "user": null,
      "readonlyRootFilesystem": null,
      "dockerLabels": null,
      "systemControls": null,
      "privileged": null,
      "name": "windows_sample_app"
    }
  ],
  "placementConstraints": [],
  "memory": "1024",
  "taskRoleArn": null,
  "compatibilities": [
    "EXTERNAL",
    "EC2"
  ],
  "taskDefinitionArn": "arn:aws:ecs:ap-northeast-1:123456789012:task-definition/windows-simple-iis:5",
  "family": "windows-simple-iis",
  "requiresAttributes": [],
  "pidMode": null,
  "requiresCompatibilities": [],
  "networkMode": null,
  "runtimePlatform": null,
  "cpu": "1024",
  "revision": 5,
  "status": "ACTIVE",
  "inferenceAccelerators": null,
  "proxyConfiguration": null,
  "volumes": []
}

タスク起動

ネットワークモードの修正のみでタスク起動に成功しました。

補足

ホストのEC2のパブリックIPに対して8080ポートでアクセスすると、Windows コンテナの80へマッピングされるタスク定義です。

動的なポートマッピングではないため2個以上のタスク(コンテナ)を起動すると、ECSのサービスのイベントに以下のエラーメッセージが記録されます。今回のタスク定義は Windows コンテナを1個起動し ECS on EC2 の動作確認用の参考としてご利用ください。

service bridge-service was unable to place a task because no container instance met all of its requirements. The closest matching container-instance 0d2070493b5c4672a104dd493dd0bed2 is already using a port required by your task. For more information, see the Troubleshooting section.

おわりに

AWS CLI、CloudFormation を利用して、ECS on EC2 の構築を含め、タスク定義を作成するときは誤って指定しない限りは遭遇しないと思います。考えられるのは Windows コンテナの実行環境をマネージメントコンソールから一度試しに作ってみようといった不慣れ方が遭遇し、尚の事困るのではないかなと思いました。どなたかのお役にたてれば光栄です。