Error: Running AWS SAM projects locally requires Docker. Have you got it installed and running?
概要
Rancher-Desktopでsam local invokeを実行した際に以下のエラーが発生したので解決してみた。
Error: Running AWS SAM projects locally requires Docker. Have you got it installed and running?
環境
- M1 MacBook
- Apple M1
- Mac 13.2.1(22D68)
- Rancher Desktop
- docker info
Client:
Context: rancher-desktop
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc., v0.9.1)
compose: Docker Compose (Docker Inc., v2.14.0)
Server:
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 19
Server Version: 20.10.20
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
runc version: 5fd4c4d144137e991c4acebb2146ab1483a97925
init version:
Security Options:
seccomp
Profile: default
Kernel Version: 5.15.78-0-virt
Operating System: Alpine Linux v3.16
OSType: linux
Architecture: aarch64
CPUs: 2
Total Memory: 3.829GiB
Name: lima-rancher-desktop
ID: TXAP:7WDE:6NMT:WAIG:HEDQ:DMVX:RJ7W:7UEV:OXCP:6FP3:WTR7:2W4L
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
解決方法
まずはコンテキストを変更します。
docker context use rancher-desktop
環境変数DOCKER_HOSTを宣言します。中身はdocker.sockのパスです。
export DOCKER_HOST="unix://$HOME/.rd/docker.sock"
これでOKです。
検証
sam local invoke
結果
Invoking app.lambda_handler (python3.9)
Local image is up-to-date
Using local image: public.ecr.aws/lambda/python:3.9-rapid-arm64.
Mounting $HOME/Desktop/sam/test_sam/hello_world as /var/task:ro,delegated, inside runtime container
START RequestId: dc051ce3-afbf-47f8-9724-a94bee41e0f7 Version: $LATEST
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}END RequestId: dc051ce3-afbf-47f8-9724-a94bee41e0f7
REPORT RequestId: dc051ce3-afbf-47f8-9724-a94bee41e0f7 Init Duration: 0.09 ms Duration: 46.93 ms Billed Duration: 47 ms Memory Size: 128 MB Max Memory Used: 128 MB
調査時に読んだ資料(関係がないように思える)
いずれもdockerの実行ユーザーに関することになるが、MacBookにおいては関係のない内容でした。
dockerのグループがあってもなくても今回のエラーは発生する可能性があるので解決方法としては十分ではないと思われる。
- Running AWS SAM projects locally get error
- sam localでRunning AWS SAM projects locally requires Docker.
- sam local doesn't work on OSX, get error Error: Running AWS SAM projects locally requires Docker. Have you got it installed?
- Linux post-installation steps for Docker Engine
ただ、Linux上でdockerを使っている場合はユーザーやグループも関係あるので一概に言えないところもある。
※例えば、実行権限がないので実行できないなど
おまけ
環境変数DOCKER_HOSTを宣言するとdocker context lsコマンドで以下のような警告が表示されます。
Warning: DOCKER_HOST environment variable overrides the active context. To use a context, either set the global --context flag, or unset DOCKER_HOST environment variable
警告を表示したくない場合はDOCKER_HOSTとsam invokeの両方を同時に実行します。
DOCKER_HOST=unix://$HOME/.rd/docker.sock sam local invoke
関数を指定する場合
DOCKER_HOST=unix://$HOME/.rd/docker.sock sam local invoke "HelloWorldFunction"