尝试运行命令
sam build --use-container
和 sam local start-api
,均返回有关 docker 无法访问的错误,尽管它正在运行且处于活动状态。我正在使用配备 M1 处理器的 iMac,这可能是问题所在,但我找不到其他人专门在 Apple Silicon 上遇到此问题。
项目目录位于/Users/下,因此默认包含在docker共享目录中。
docker info
的输出:
Client:
Context: desktop-linux
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc., v0.9.1)
compose: Docker Compose (Docker Inc., v2.12.0)
dev: Docker Dev Environments (Docker Inc., v0.0.3)
extension: Manages Docker extensions (Docker Inc., v0.2.13)
sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
scan: Docker Scan (Docker Inc., v0.21.0)
Server:
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 1
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: 2
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: v1.1.4-0-g5fd4c4d
init version: de40ad0
Security Options:
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.49-linuxkit
Operating System: Docker Desktop
OSType: linux
Architecture: aarch64
CPUs: 4
Total Memory: 3.84GiB
Name: docker-desktop
ID: 5UH7:HBEP:RUEN:WDIA:O57J:K5I2:IPTO:UW5A:W7D4:7IJD:IZJJ:4QRB
Docker Root Dir: /var/lib/docker
Debug Mode: false
HTTP Proxy: http.docker.internal:3128
HTTPS Proxy: http.docker.internal:3128
No Proxy: hubproxy.docker.internal
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
hubproxy.docker.internal:5000
127.0.0.0/8
Live Restore Enabled: false
SAM版本:
{
"version": "1.60.0"
}
下面是
sam local start-api
命令的调试输出。
2022-10-21 21:46:53,547 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-10-21 21:46:53,547 | Using config file: samconfig.toml, config environment: default
2022-10-21 21:46:53,547 | Expand command line arguments to:
2022-10-21 21:46:53,547 | --template_file=/Users/ignas/Projects/cv-de-task/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/Users/ignas/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2022-10-21 21:46:53,605 | local start-api command is called
2022-10-21 21:46:53,608 | No Parameters detected in the template
2022-10-21 21:46:53,617 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-10-21 21:46:53,617 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-10-21 21:46:53,618 | 0 stacks found in the template
2022-10-21 21:46:53,618 | No Parameters detected in the template
2022-10-21 21:46:53,624 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-10-21 21:46:53,624 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-10-21 21:46:53,624 | 2 resources found in the stack
2022-10-21 21:46:53,624 | Found Serverless function with name='HelloWorldFunction' and CodeUri='hello_world/'
2022-10-21 21:46:53,624 | --base-dir is not presented, adjusting uri hello_world/ relative to /Users/ignas/Projects/cv-de-task/template.yaml
2022-10-21 21:46:53,626 | Docker is not reachable
Traceback (most recent call last):
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/urllib3/connectionpool.py", line 398, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1256, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1302, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1251, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1011, in _send_output
self.send(msg)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 951, in send
self.connect()
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/docker/transport/unixconn.py", line 43, in connect
sock.connect(self.unix_socket)
FileNotFoundError: [Errno 2] No such file or directory
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/urllib3/connectionpool.py", line 787, in urlopen
retries = retries.increment(
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/urllib3/util/retry.py", line 550, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/urllib3/packages/six.py", line 769, in reraise
raise value.with_traceback(tb)
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/urllib3/connectionpool.py", line 398, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1256, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1302, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1251, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1011, in _send_output
self.send(msg)
File "/opt/homebrew/Cellar/[email protected]/3.8.15/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 951, in send
self.connect()
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/docker/transport/unixconn.py", line 43, in connect
sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/samcli/local/docker/utils.py", line 91, in is_docker_reachable
docker_client.ping()
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/docker/client.py", line 187, in ping
return self.api.ping(*args, **kwargs)
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/docker/api/daemon.py", line 166, in ping
return self._result(self._get(self._url('/_ping'))) == 'OK'
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/docker/utils/decorators.py", line 46, in inner
return f(self, *args, **kwargs)
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/docker/api/client.py", line 230, in _get
return self.get(url, **self._set_request_timeout(kwargs))
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/requests/sessions.py", line 555, in get
return self.request('GET', url, **kwargs)
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/requests/sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "/opt/homebrew/Cellar/aws-sam-cli/1.60.0/libexec/lib/python3.8/site-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
2022-10-21 21:46:53,663 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': 'a2779315-7fbf-4fde-8283-bddfa27b32f7', 'installationId': '82f40a94-c925-4ca9-9100-5436acfc489d', 'sessionId': '2252259d-ee1f-4881-84a1-90b4a884b272', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.15', 'samcliVersion': '1.60.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam local start-api', 'metricSpecificAttributes': {'projectType': 'CFN', 'gitOrigin': None, 'projectName': 'd067e474cfd36d5302b727ce111f9a64cd5415b562d46dc558bdc05173eea29b', 'initialCommit': None}, 'duration': 115, 'exitReason': 'DockerIsNotReachableException', 'exitCode': 1}}]}
2022-10-21 21:46:54,376 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: Running AWS SAM projects locally requires Docker. Have you got it installed and running?
出现错误的最可能原因是您使用的 Docker 版本与配备 M1 处理器的 iMac 不兼容。您尝试运行的 sam 命令需要 Docker 在本地构建和运行您的无服务器应用程序,但它们可能无法与您安装的 Docker 版本很好地配合。
根据 AWS SAM CLI 的官方文档,sam build --use-container 命令会在模拟 Lambda 执行环境的 Docker 容器内构建您的应用程序。 sam local start-api 命令使用 Docker 网络在本地运行您的应用程序。这两个命令都依赖于 Docker 才能正常运行。
但是,并非所有版本的 Docker 都与 iMac M1 处理器兼容,该处理器使用与基于 Intel 的 Mac 不同的架构。 M1处理器采用ARM64架构,效率更高、功能更强大,但也需要与Intel使用的x86_64架构不同的二进制文件和库。
截至目前,Docker 已经发布了支持 Apple Silicon 的 Docker Desktop for Mac 预览版,您可以从此处下载。此版本仍处于实验阶段,可能存在一些错误或限制,但它应该允许您运行 sam 命令而不会出现错误。或者,您可以尝试使用不同的工具在本地运行无服务器应用程序,例如LocalStack,它不依赖于 Docker。
我的公司发展因为这个问题已经被搁置了一个多星期了。我可以确认最新补丁 4.13.1 解决了该问题。
更新到4.13.1后,请务必完全重新启动docker。
Docker Desktop 通过最新的 release 解决了这个问题:
默认在 Mac 上添加了 /var/run/docker.sock 符号链接,以提高与tilt和docker-py等工具的兼容性。修复 docker/for-mac#6529。
确保您的 Docker 桌面已更新。