M1芯片iMac上AWS SAM CLI无法连接Docker的问题
M1处理器iMac上AWS SAM CLI无法连接Docker的问题排查请求
在M1处理器的iMac上运行sam build --use-container和sam local start-api命令时,遇到AWS SAM CLI无法连接Docker的问题。Docker已处于运行状态,但两个命令均返回Docker不可达的错误。怀疑与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/python@3.8/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/python@3.8/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/python@3.8/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/python@3.8/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/python@3.8/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/python@3.8/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/python@3.8/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/python@3.8/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/python@3.8/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/python@3.8/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?
排查方案
从错误日志看,核心问题是SAM CLI找不到Docker的Unix套接字文件,导致连接失败。针对M1 Mac的情况,可以尝试以下步骤:
检查Docker套接字路径配置
确认Docker Desktop的套接字路径默认是/var/run/docker.sock,在终端执行echo $DOCKER_HOST,如果输出为空,手动设置环境变量:export DOCKER_HOST=unix:///var/run/docker.sock或者尝试使用TCP连接(需先在Docker Desktop设置中开启"Allow the default Docker socket to be used"选项):
export DOCKER_HOST=tcp://localhost:2375重启Docker Desktop与SAM CLI
完全退出Docker Desktop后重新启动,再重启终端窗口确保环境变量生效,之后尝试运行SAM命令。升级或重装SAM CLI
当前使用的是Homebrew安装的SAM CLI 1.60.0,尝试升级到最新版本:brew upgrade aws-sam-cli若升级无效,卸载后重新安装:
相关产品推荐
相关产品推荐

