aws-sam-cli: Bug: Error: Docker is not reachable (even though it is!)

Description:

Hi, I’m new to AWS SAM and I’m facing issue trying to run sam local start-api. I’ve docker perfectly installed and running. I can also run docker run -d -p 80:80 docker/getting-started I’ve followed each and every line of the documentation and am still getting this issue.

I would really appreciate if you can help and let me know if it’s a bug or there is something I’m doing wrong.

Thanks!

Observed result:

sudo sam local start-api --debug
2022-10-21 12:58:01,423 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2022-10-21 12:58:01,423 | Using config file: samconfig.toml, config environment: default
2022-10-21 12:58:01,423 | Expand command line arguments to:
2022-10-21 12:58:01,423 | --template_file=/Users/dhyeyshah/Documents/vStory/links-vstory-co-backend/.aws-sam/build/template.yaml --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=/Users/dhyeyshah/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1 
2022-10-21 12:58:01,561 | local start-api command is called
2022-10-21 12:58:01,564 | No Parameters detected in the template
2022-10-21 12:58:01,572 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction is HelloWorldFunction
2022-10-21 12:58:01,572 | 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 12:58:01,573 | 0 stacks found in the template
2022-10-21 12:58:01,573 | No Parameters detected in the template
2022-10-21 12:58:01,578 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction is HelloWorldFunction
2022-10-21 12:58:01,578 | 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 12:58:01,578 | 2 resources found in the stack 
2022-10-21 12:58:01,578 | Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
2022-10-21 12:58:01,578 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to /Users/dhyeyshah/Documents/vStory/links-vstory-co-backend/.aws-sam/build/template.yaml
2022-10-21 12:58:01,580 | 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 12:58:01,620 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': '62545fa7-9156-4ff1-91da-769acb294fdd', 'installationId': 'b741dcb0-1046-4c7e-a4d6-307c96f765df', 'sessionId': '396f5fcf-13d9-418b-b13f-914d27cb3fe0', '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': '72decab683f6a077f3e3fc1129e7a9faade6cf0cb8fed680bcdafd9f0629b77b', 'initialCommit': None}, 'duration': 197, 'exitReason': 'DockerIsNotReachableException', 'exitCode': 1}}]}
2022-10-21 12:58:02,734 | 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?

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

Device: Macbook air M1 model ProductName: macOS ProductVersion: 12.6 BuildVersion: 21G115

SAM CLI, version 1.60.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 8
  • Comments: 46 (5 by maintainers)

Most upvoted comments

We were able to find the root cause of the issue. I think Docker has changed its behavior with its latest release in terms of setting up the default context for its client.

If you run docker info please check what is the Context value for client configuration. For instance, this one is using desktop-linux which is breaking our use case;

$ docker info
Client:
 Context:    desktop-linux
...

And if you run docker context ls it will show you the context information;

$ docker context ls
NAME                TYPE                DESCRIPTION                               DOCKER ENDPOINT                                  KUBERNETES ENDPOINT   ORCHESTRATOR
default             moby                Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                                            swarm
desktop-linux *     moby                                                          unix:///Users/ec2-user/.docker/run/docker.sock

Unfortunately, docker library can’t find out which context is been used without connecting to the docker daemon. So in order to fix the issue you need to provide DOCKER_HOST environment variable. So running command with following way will resolve the issue for now (please replace actual host endpoint with what you see from the docker context ls command);

$ DOCKER_HOST=unix:///Users/ec2-user/.docker/run/docker.sock sam build -u

After running this way, SAM CLI commands will be succeeding. Same issue is reported in docker-py repository where we are discussing with them to resolve this issue. (See: https://github.com/docker/docker-py/issues/3059)

In the docker settings go to Advanced -> Allow the default Docker socket to be used (requires password) and restart docker or Mac. It works for me

Same problem SAM CLI, version 1.80.0 Docker version 20.10.24 OS: macOS 13.3.1

work around: sudo ln -sf “$HOME/.docker/run/docker.sock” /var/run/docker.sock

I am facing this issue on a daily basis. Have to run sudo ln -sf "$HOME/.docker/run/docker.sock" /var/run/docker.sock daily, or I get ‘Error: Running AWS SAM projects locally requires Docker. Have you got it installed and running?’

SAM CLI version 1.75.0, Docker desktop engine 20.10.22, Apple M1 Pro

Even though I updated the Docker Desktop to the latest version, I still face the same error!

Basically, I got a brand new installation (everything from scratch), but I only managed to solve it by running: sudo ln -sf "$HOME/.docker/run/docker.sock" /var/run/docker.sock as recommended in this answer https://github.com/docker/docker-py/issues/3059#issuecomment-1294369344

I had the same issues using an Apple M1 Pro chipset and I was able to get it working by downgrading my version of Docker Desktop to v4.7.0, which I did via this link . I just downloaded that version, ensured Docker Desktop wasn’t running on my Macbook and replacing the app in my applications and then restarting Docker Desktop.

Docker Desktop fixed this issue with their latest release: https://docs.docker.com/desktop/release-notes/#docker-desktop-4131

Added back the /var/run/docker.sock symlink on Mac by default, to increase compatibility with tooling like tilt and docker-py. Fixes docker/for-mac#6529.

We recommend upgrading your installation so that you can continue using without the workaround provided in this or in this comment.

Again, thanks for reporting this issue in the first place!

Installing Docker version 20.10.14, build a224086 and upgrading manually fixes my issues but it’s still quite weird that we cannot directly install the latest docker version.

In the docker settings go to Advanced -> Allow the default Docker socket to be used (requires password) and restart docker or Mac. It works for me

Thank you !. This is the fix for for Docker version 24.0.6, build ed223bc on Mac M1

We were able to find the root cause of the issue. I think Docker has changed its behavior with its latest release in terms of setting up the default context for its client.

If you run docker info please check what is the Context value for client configuration. For instance, this one is using desktop-linux which is breaking our use case;

$ docker info
Client:
 Context:    desktop-linux
...

And if you run docker context ls it will show you the context information;

$ docker context ls
NAME                TYPE                DESCRIPTION                               DOCKER ENDPOINT                                  KUBERNETES ENDPOINT   ORCHESTRATOR
default             moby                Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                                            swarm
desktop-linux *     moby                                                          unix:///Users/ec2-user/.docker/run/docker.sock

Unfortunately, docker library can’t find out which context is been used without connecting to the docker daemon. So in order to fix the issue you need to provide DOCKER_HOST environment variable. So running command with following way will resolve the issue for now (please replace actual host endpoint with what you see from the docker context ls command);

$ DOCKER_HOST=unix:///Users/ec2-user/.docker/run/docker.sock sam build -u

After running this way, SAM CLI commands will be succeeding. Same issue is reported in docker-py repository where we are discussing with them to resolve this issue. (See: docker/docker-py#3059)

Thanks a lot. This helped

@ghoshpushpendu Check this out.

It worked for me, my issue was with catalina, but in your case if you modify the path to get Docker Desktop’s context (probably unix:///Users/{username}/.docker/run/docker.sock) it might work for you.