🐳

ECSでHelloWorldしてみた

2025/01/28に公開

ECSの入門として、ECSでHelloWorldをやってみようと思います。

Dockerイメージ

こちらのDockerイメージを利用します。
https://hub.docker.com/_/hello-world

起動すると以下のようにメッセージを出力して終了します。

$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

タスク定義の作成

まずAWSのマネジメントコンソールを開き、ECSの画面から新しいタスクの定義を行います。

タスク定義ファミリー名は適当にhelloworld-taskとしました。

インフラストラクチャの要件はとりあえず動けばいいので最小な感じにしました。

コンテナを定義

コンテナには先ほどのDockerイメージを設定します。
今回は使用しませんが、ECRにあるイメージも使用できます。

また、CloudWatchでログを収集するように設定しておきましょう。

あとはデフォルトで良さそうなので、「作成」をクリックしてタスク作成を完了させましょう。

クラスターの作成

次にクラスターを作成していきます。
こちらもインフラはFargateで設定しておきましょう。

タスクの実行

それではタスクの実行をしていきましょう。
タスク定義から先ほど作成したhelloworld-taskを選択して、タスクの実行をしてみます。

先ほど作成したhelloworld-clusterを指定します。

デプロイ設定もそのままで問題ありません。

「作成」をクリックするとタスクが実行され、以下のように表示されます。

タスクをクリックし、「ログ」のタブを確認してみると、HelloWorldイメージを起動した際のメッセージが出力されていることが確認できます。

参考資料

https://dev.classmethod.jp/articles/ecs-hello-world-task/
https://qiita.com/Maron_T/items/13c85075e14dfcf95024

Discussion