Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a check to Build command for Docker if the --use-container flag i… #1075

Merged
merged 2 commits into from
Apr 4, 2019

Conversation

gkrizek
Copy link
Contributor

@gkrizek gkrizek commented Mar 24, 2019

If you use the --use-container flag with the sam build command but Docker isn't running you get some nasty output. You get a bunch of errors and stack traces about endpoints being inaccessible and so on. This is really confusing and takes some digging to find out what the problem is. I've been tripped up several times and wasted a bunch of time trying to figure out what went wrong.

This PR adds a check to the build command to make sure Docker is reachable before trying to actually build the project inside of Docker. If Docker isn't reachable, it errors with a clear message indicating what the issue is. It also only tries to check for Docker when the --use-container flag is passed.

How to reproduce problem:

Run sam build --use-container while Docker isn't running.

Description of changes:

Changes include an added 'if' statement in the app_build.py file. This uses the already existing ContainerManager.is_docker_reachable property to determine if SAM can reach Docker. This only checks if the --use-container flag is passed. If it can't reach Docker, then it raises and exception.

@gkrizek
Copy link
Contributor Author

gkrizek commented Mar 24, 2019

Old Output:

2019-03-24 00:07:09 Starting Build inside a container
2019-03-24 00:07:09 Found credentials in shared credentials file: ~/.aws/credentials
2019-03-24 00:07:10 Building resource 'Function'
Traceback (most recent call last):
  File "/Users/AppleUser/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/Users/AppleUser/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/docker/transport/unixconn.py", line 42, in connect
    sock.connect(self.unix_socket)
ConnectionRefusedError: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/Users/AppleUser/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/Users/AppleUser/anaconda3/lib/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/Users/AppleUser/anaconda3/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/AppleUser/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/Users/AppleUser/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/Users/AppleUser/anaconda3/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/docker/transport/unixconn.py", line 42, in connect
    sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionRefusedError(61, 'Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/AppleUser/.local/bin/sam", line 10, in <module>
    sys.exit(cli())
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/samcli/commands/build/command.py", line 94, in cli
    skip_pull_image, parameter_overrides)  # pragma: no cover
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/samcli/commands/build/command.py", line 132, in do_cli
    artifacts = builder.build()
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/samcli/lib/build/app_builder.py", line 129, in build
    lambda_function.runtime)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/samcli/lib/build/app_builder.py", line 201, in _build_function
    runtime)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/samcli/lib/build/app_builder.py", line 250, in _build_function_on_container
    self._container_manager.run(container)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/samcli/local/docker/manager.py", line 75, in run
    is_image_local = self.has_image(image_name)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/samcli/local/docker/manager.py", line 153, in has_image
    self.docker_client.images.get(image_name)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/docker/models/images.py", line 316, in get
    return self.prepare_model(self.client.api.inspect_image(name))
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/docker/api/image.py", line 245, in inspect_image
    self._get(self._url("/images/{0}/json", image)), True
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/docker/utils/decorators.py", line 46, in inner
    return f(self, *args, **kwargs)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/docker/api/client.py", line 225, in _get
    return self.get(url, **self._set_request_timeout(kwargs))
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/requests/sessions.py", line 546, in get
    return self.request('GET', url, **kwargs)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/Users/AppleUser/.local/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(61, 'Connection refused'))
make: *** [build] Error 1

New Output:

$ sam build --use-container
2019-03-24 01:16:47 Starting Build inside a container
2019-03-24 01:16:47 Building resource 'Function'
Build Failed
Error: --use-container flag passed but Docker is not running

@sanathkr
Copy link
Contributor

sanathkr commented Apr 4, 2019

Thanks for the PR. Very useful. I made a minor refactor and also contributed unit tests to make sure this code path is exercised. I will merge the PR once the tests pass

@sanathkr sanathkr merged commit ed12423 into aws:develop Apr 4, 2019
@gkrizek
Copy link
Contributor Author

gkrizek commented Apr 4, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants