Repository: getmoto/moto

Issue:
AttributeError exception in _invoke_lambda
Moto version: https://github.com/spulec/moto/commit/80b64f9b3ff57515db1fc07329bf8e5f519597aa (but from looking at the code the issue exists even on today's HEAD https://github.com/spulec/moto/commit/09b764148cea354c790443a318dabc976a2b59df)

Stack track of the issue:
```
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/lib/python3.6/site-packages/moto/core/utils.py", line 146, in __call__
    result = self.callback(request, request.url, {})
  File "/usr/lib/python3.6/site-packages/moto/core/utils.py", line 239, in _wrapper
    response = f(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/moto/core/utils.py", line 271, in _wrapper
    response = f(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/moto/awslambda/responses.py", line 95, in invoke
    return self._invoke(request, full_url)
  File "/usr/lib/python3.6/site-packages/moto/awslambda/responses.py", line 182, in _invoke
    function_name, qualifier, self.body, self.headers, response_headers
  File "/usr/lib/python3.6/site-packages/moto/awslambda/models.py", line 1074, in invoke
    payload = fn.invoke(body, headers, response_headers)
  File "/usr/lib/python3.6/site-packages/moto/awslambda/models.py", line 479, in invoke
    res, errored, logs = self._invoke_lambda(code=self.code, event=body)
  File "/usr/lib/python3.6/site-packages/moto/awslambda/models.py", line 469, in _invoke_lambda
    [line for line in self.convert(output).splitlines()[:-1]]
AttributeError: 'NoneType' object has no attribute 'splitlines'
```

Code (awslambda/models.py):
```
    def _invoke_lambda(self, code, event=None, context=None):
        ...
        output = None

        try:
            ...
            with _DockerDataVolumeContext(self) as data_vol:
                try:
                    ...
                    container = self.docker_client.containers.run(...)  # <-- something raises, probably here
                finally:
                    if container:  # <-- container is probably None here
                        try:
                            exit_code = container.wait(timeout=300)
                        except requests.exceptions.ReadTimeout:
                            exit_code = -1
                            container.stop()
                            container.kill()
                        else:
                            if docker_3:
                                exit_code = exit_code["StatusCode"]

                        output = container.logs(stdout=False, stderr=True)
                        output += container.logs(stdout=True, stderr=False)
                        container.remove()

            output = output.decode("utf-8")  # <-- this probably raises an exception since output is None
            ...
        except BaseException as e:
            traceback.print_exc()
            logs = os.linesep.join(
                [line for line in self.convert(output).splitlines()[:-1]]  # <-- now output is None and throws exception
            )
            ...
```




Discussion:

Resolving patch:
diff --git a/moto/awslambda/models.py b/moto/awslambda/models.py
--- a/moto/awslambda/models.py
+++ b/moto/awslambda/models.py
@@ -401,6 +401,7 @@ def _invoke_lambda(self, code, event=None, context=None):
             log_config = docker.types.LogConfig(type=docker.types.LogConfig.types.JSON)
             with _DockerDataVolumeContext(self) as data_vol:
                 try:
+                    self.docker_client.ping()  # Verify Docker is running
                     run_kwargs = (
                         dict(links={"motoserver": "motoserver"})
                         if settings.TEST_SERVER_MODE
@@ -463,6 +464,9 @@ def _invoke_lambda(self, code, event=None, context=None):
                 [line for line in self.convert(output).splitlines()[:-1]]
             )
             return resp, False, logs
+        except docker.errors.DockerException as e:
+            # Docker itself is probably not running - there will be no Lambda-logs to handle
+            return "error running docker: {}".format(e), True, ""
         except BaseException as e:
             traceback.print_exc()
             logs = os.linesep.join(


Test patch: