5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Serverless Framework を Docker で動かす Dockerfile テンプレート

Posted at

概要

Serverless Framework のコマンドを Mac で叩きたかったがローカル環境を汚したくなかったので
Docker コンテナ上で叩けるような Dockerfile を作成しました。

環境

Mac
Docker version 18.09.2, build 6247962
docker-compose version 1.23.2, build 1110ad01

作成した Dockerfile

FROM python:3.7-alpine
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY

ENV NODE_PATH /usr/lib/node_modules/
# install nodejs
RUN apk update \
  && apk add --no-cache nodejs npm

# install aws-cli
RUN pip install awscli

# install serverless framework
RUN npm install -g serverless serverless-plugin-existing-s3

# set aws key 
RUN sls config credentials --provider aws --key $AWS_ACCESS_KEY_ID --secret $AWS_SECRET_ACCESS_KEY

# change work directory
RUN mkdir -p /app
WORKDIR /app

作成した docker-compose ファイル

version: '3.5'
services:
  serverless:
    build: 
      context: .
      dockerfile: ./dockerfiles/serverless/Dockerfile
      args: 
        - AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
        - AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
    tty: true
    stdin_open: true
    image: serverless
    working_dir: /app
    volumes:
      - .:/app
    container_name: serverless

解説

Docerfile の設定

まず Dockerfile について解説します

コンテナの設定

FROM python:3.7-alpine
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY

ENV NODE_PATH /usr/lib/node_modules/

aws cli をインストールするために alpine linux の python をインストールします。
OS は何でもいいのですが alpine linux は容量が少ないのでよく採用させてもらってます。
ちなみに python:3.7-alpine は 87MB でした。

次に docker-compose から渡された AWS のアクセスキーとシークレットキーを読み込ませます。
こちらはサーバレスフレームワークを利用する上で必要になります。
最後に NODE_PATH の環境変数を設定します。 NODE_PATH は npm のグローバルインストールを行うための
node_module フォルダパスを指定します。

パッケージのインストール

# install nodejs
RUN apk update \
  && apk add --no-cache nodejs npm

# install aws-cli
RUN pip install awscli

# install serverless framework
RUN npm install -g serverless serverless-plugin-existing-s3

次にパッケージのインストールです。
RUN コマンドを利用して alpine-linux の apk コマンドを叩きます。
サーバレスフレームワークは npm で管理されているので npmnodejs をインストールします。
また、裏で AWS CLI コマンドが動くので awscli を pip コマンドでインストールしています。
npm install コマンドでグローバルに serverless フレームワークと関連するプラグインをインストールしています。
作成済みの S3 バケットに対して後から lambda イベントを付与したかったので serveless-plugin-existing-s3
プラグインも入れてます。

sls コマンドの実行

# set aws key 
RUN sls config credentials --provider aws --key $AWS_ACCESS_KEY_ID --secret $AWS_SECRET_ACCESS_KEY

# change work directory
RUN mkdir -p /app
WORKDIR /app

最後に sls コマンドを実行して アクセスキーとシークレットキーの設定を実行しています。

docker-compose の設定

version: '3.5'
services:
  serverless:
    build: 
      context: .
      dockerfile: ./dockerfiles/serverless/Dockerfile
      args: 
        - AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
        - AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
    tty: true
    stdin_open: true
    image: serverless
    working_dir: /app
    volumes:
      - .:/app
    container_name: serverless

docker-compose は特に複雑なことはせず Mac のローカルに設定されている環境変数を args で Dockerfile に渡してあげています。
dockerfile は先程作成した dockerfile のパスを指定します。
作成した serverless.yml ファイルをコンテナ側でも参照できないと行けないので volume をマウントしてあげましょう。

解説した dockerfile と docker-compose のファイルは github においています。
ご自由にお使いください。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?