9
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.

WSL内に構築したAWS SAMから、ホストのDocker for Windowsを利用する

Posted at

@akym03 さんがポストされてた記事がとても役立ったのですが、404になってしまっているので自分の備忘録として残させて頂きます。

やりたいこと

WLS内にインストールしたAWSのSAM(Serverless Application Model)のCLIから、Docker for Windowsのデーモンにアクセスして、ローカルのLambdaにアクセスしたい

なぜできないの?

現状WLS内のDockerはホストにはなれず、あくまでもクライアントのため、WSLのDockerにアクセスしても意味がないのです。
詳しくはこちらの図がとても参考になりました。(画像リンクしてます)

WSL(Bash on Windows)でDockerを使用する - Qiita

環境

  • Windows 10 Pro
  • バージョン 1809
  • OSビルド 17763.316
  • DISTRIB_DESCRIPTION="Ubuntu 18.04.2 LTS"

Dockerの設定

Settings > General にある Expose daemon on tcp://localhost:2375 without TLS にチェックを入れる

2019-07-17_11h07_09.png

WSLの設定

WSLからDocker for Windowsに接続するために、WSLで環境変数を設定する

$ export DOCKER_HOST=tcp://localhost:2375

SAM local実行

sam init で作成されたプロジェクトを動作させてみます。

$ sam local invoke HelloWorldFunction --event event_file.json
2019-07-17 10:47:53 Invoking app.lambdaHandler (nodejs10.x)
2019-07-17 10:47:53 Found credentials in shared credentials file: ~/.aws/credentials

Fetching lambci/lambda:nodejs10.x Docker container image......
2019-07-17 10:47:55 Mounting /c/opt/box/box-reinforcement-lambda/hello-world as /var/task:ro,delegated inside runtime container
START RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST
END RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72
REPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72  Duration: 9.58 ms       Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 43 MB
{"statusCode":200,"body":"{\"message\":\"hello world\"}"}

ローカルでAPI起動

以下に沿ってStep 2: Test the Application Locallyをやってみます
Quick Start - AWS Serverless Application Model

$ sam local start-api
2019-07-17 11:15:54 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2019-07-17 11:15:54 You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2019-07-17 11:15:54  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
2019-07-17 11:16:00 127.0.0.1 - - [17/Jul/2019 11:16:00] "GET / HTTP/1.1" 403 -
2019-07-17 11:16:00 127.0.0.1 - - [17/Jul/2019 11:16:00] "GET /favicon.ico HTTP/1.1" 403 -
2019-07-17 11:16:11 Invoking app.lambdaHandler (nodejs10.x)
2019-07-17 11:16:11 Found credentials in shared credentials file: ~/.aws/credentials

Fetching lambci/lambda:nodejs10.x Docker container image......
2019-07-17 11:16:13 Mounting /c/opt/box/box-reinforcement-lambda/hello-world as /var/task:ro,delegated inside runtime container
START RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST
END RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72
REPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72  Duration: 7.61 ms       Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 43 MB
2019-07-17 11:16:14 No Content-Type given. Defaulting to 'application/json'.
2019-07-17 11:16:14 127.0.0.1 - - [17/Jul/2019 11:16:14] "GET /hello HTTP/1.1" 200 -

ブラウザからhttp://127.0.0.1:3000/helloにアクセスします
200でjson返ってきました。

{"message":"hello world"}
9
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
9
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?