AWS Amplify の Fargate 対応で “うまくいかない” docker-compose.yml の書き方

こちらは、 “ゆるWeb勉強会@札幌 Advent Calendar 2020” の 14日目の記事です。

AWS Amplify の API 機能で AWS Fargate をサポートするようになりました。 (2020年12月10日ごろ)

Amplify CLI が、AWS Fargate を使用したサーバーレスコンテナのデプロイを実現 投稿日: Dec 10, 2020
Amplify CLI が、AWS Fargate を使用したサーバーレスコンテナのデプロイを実現 aws.amazon.com
Amplify CLI が、AWS Fargate を使用したサーバーレスコンテナのデプロイを実現

このブログ記事の通りにやれば、試すことができます。

Amplify のドキュメントも更新されて情報が追加になっていますね。

Serverless containers - JavaScript - AWS Amplify DocumentationDeploy containers to AWS. Serverless containers leverage AWS Fargate when building REST or GraphQL APIs in your account. Containers can be deployed via a single Dockerfile definition or by using a Docker Compose file, with a build and deployment pipeline created inside your AWS account. AWS Amplify Documentation
Serverless containers - JavaScript - AWS Amplify Documentation docs.amplify.aws
Serverless containers - JavaScript - AWS Amplify Documentation

私は PHPer なので、最終的に PHP のフレームワークである Laravel を Amplify で動かすことを目指しています。(完成したら、別途記事にします。)

今回は、その過程で docker-compose.yml の書き方に制限があったのでそれを書いておきます。

試す

素直にやる場合

試すのは、こちらのオフィシャルブログに従えばできます。(できました。)

Zero-effort Container deployment for GraphQL and REST APIs and Web Hosting with Amplify CLI | Amazon Web ServicesAWS Amplify is the fastest and easiest way to build cloud-powered mobile and web apps on AWS. Amplify comprises a set of tools and services that enables front-end web and mobile developers to leverage the power of AWS services to build innovative and feature-rich applications. With today’s Amplify CLI release, we’re enabling front-end web and […]
Zero-effort Container deployment for GraphQL and REST APIs and Web Hosting with Amplify CLI | Amazon Web Services aws.amazon.com
Zero-effort Container deployment for GraphQL and REST APIs and Web Hosting with Amplify CLI | Amazon Web Services

こちらで展開されるファイルは、 GitHub のこちらにありますね。

File not found ツキ aws-amplify/amplify-cliThe AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. - File not found ツキ aws-amplify/amplify-cli
File not found ツキ aws-amplify/amplify-cli github.com
File not found ツキ aws-amplify/amplify-cli

まず、基本的な docker-compose.yml の書き方はこうなります。
Custom でも、当然これに従ってやってみることでうまく行きます。

version: "3.8"
 services:
   express:
     build:
       context: ./express
       dockerfile: Dockerfile
     ports:
       - "8080:8080"
     networks:
       - public
       - private
   python:
     build:
       context: ./python
       dockerfile: Dockerfile
     networks:
       - public
       - private
     ports:
       - "5000:5000"
 networks:
   public:
   private:

buildcontextdockerfile の指定があります。context でビルドするディレクトリを、 dockerfile で実際にビルド対象となるファイル名を指定できます。ローカルでやる場合、この dockerfile は自由に指定可能です。

Custom で試す (うまくいかないパターン)

そして、 PHP のフレームワークを動かそうとする場合、一般的なコンテナの組み方として Webサーバ(Nginx) のコンテナと、 PHP-FPM のコンテナを二つ用意します。かつ、その両方に同一のソースコードを格納する必要があります。

ということで、下記のような docker-compose.yml を書いてみました。

version: "3.8"
services:
  web:
    build:
      context: ./php
      dockerfile: Dockerfile_web
    ports:
      - "8080:8080"
    networks:
      - public
      - private
  app:
    build:
      context: ./php
      dockerfile: Dockerfile_app
    ports:
      - "9000:9000"
    networks:
      - public
      - private
networks:
  public:
  private:

それぞれの Dockerfile_* の中で、 Laravel のコードを COPY しています。

(他のファイルは今回は割愛します。)

これで、ローカルの実行はうまくいきますが、 amplify push すると途中で失敗します。

この時、 CodeBuild のログを確認すると、下記のようなログが残っています。

(snip)

[Container] 2020/12/14 09:00:45 Running command docker build -t $web_REPOSITORY_URI:latest ././php 
52
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /codebuild/output/src084676718/src/php/Dockerfile: no such file or directory 
53

(snip)

どうやら、 dockerfile の指定は効かないみたいですね。

まとめ

上記のエラーに気づくまでに結構時間を取られてしまったので、いったんはここまで。

そもそも、 Fargate の元のサービスである Amazon ECS の方で Docker Compose 構文の制限に引っかかっていたのかもしれないです。

Amazon Elastic Container Service
Amazon Elastic Container Service docs.aws.amazon.com
Amazon Elastic Container Service

成功パターンは、別の記事にまとめたいと思います。

tacck
  • tacck
  • 北の大地の普通のソフトウェアエンジニア。
    インフラ・バックエンド・フロントエンドと、色々やります。

    初心者・若手向けのメンターも希望あればお受けします。

    勉強会運営中
    * ゆるWeb勉強会@札幌
    * スマートスピーカーで遊ぼう会@札幌

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください