Trying to run commands sam build --use-container
and sam local start-api
, both return errors about docker being unreachable, although it is running and active. I am using iMac with M1 processor, this could be the issue, but I could not find anyone else having this issue specifically on Apple Silicon.
The directory of the project is under /Users/, thus it is by default included in docker shared directories.
The output of 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:
{
"version": "1.60.0"
}
Below is the debug output of the sam local start-api
command.
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?
3
Answers
The most likely cause of the errors you are getting is that you are using an incompatible version of Docker for your iMac with M1 processor. The sam commands you are trying to run require Docker to build and run your serverless application locally, but they may not work well with the Docker version you have installed.
Explanation
According to the official documentation of AWS SAM CLI, the sam build –use-container command builds your application inside a Docker container that mimics the Lambda execution environment. The sam local start-api command runs your application locally by using a Docker network. Both of these commands depend on Docker to function properly.
However, not all versions of Docker are compatible with the iMac M1 processor, which uses a different architecture than the Intel-based Macs. The M1 processor uses the ARM64 architecture, which is more efficient and powerful, but also requires different binaries and libraries than the x86_64 architecture used by Intel.
As of now, Docker has released a preview version of Docker Desktop for Mac with Apple Silicon support, which you can download from here. This version is still experimental and may have some bugs or limitations, but it should allow you to run the sam commands without errors. Alternatively, you can try to use a different tool to run your serverless application locally, such as LocalStack, which does not rely on Docker.
Docker Desktop fixed this issue with their latest release:
Make sure your Docker Desktop is updated.
My company development has been on hold due to this issue for over a week now. I can confirm that the latest patch 4.13.1 addresses the issue.
Once updated to 4.13.1, be sure to restart docker fully.