# Buggy code
def prepare_execution(self, func_arn, env_vars, runtime, command, handler, lambda_cwd):
    entrypoint = ""
    if command:
        entrypoint = ' --entrypoint ""'
    else:
        command = '"%s"' % handler

    # add Docker Lambda env vars
    network = config.LAMBDA_DOCKER_NETWORK
    network_str = '--network="%s"' % network if network else ""
    if network == "host":
        port = str(self.next_port + self.port_offset)
        env_vars["DOCKER_LAMBDA_API_PORT"] = port
        env_vars["DOCKER_LAMBDA_RUNTIME_PORT"] = port
        self.next_port = (self.next_port + 1) % self.max_port

    env_vars_string = " ".join(
        ['-e {}="${}"'.format(k, k) for (k, v) in env_vars.items()]
    )
    debug_docker_java_port = (
        "-p {p}:{p}".format(p=Util.debug_java_port) if Util.debug_java_port else ""
    )
    docker_cmd = self._docker_cmd()
    docker_image = Util.docker_image_for_runtime(runtime)
    rm_flag = Util.get_docker_remove_flag()

    if config.LAMBDA_REMOTE_DOCKER:
        cmd = (
            'CONTAINER_ID="$(%s create -i'
            " %s"  # entrypoint
            " %s"  # debug_docker_java_port
            " %s"  # env
            " %s"  # network
            " %s"  # --rm flag
            " %s %s"  # image and command
            ')";'
            '%s cp "%s/." "$CONTAINER_ID:/var/task"; '
            '%s start -ai "$CONTAINER_ID";'
        ) % (
            docker_cmd,
            entrypoint,
            debug_docker_java_port,
            env_vars_string,
            network_str,
            rm_flag,
            docker_image,
            command,
            docker_cmd,
            lambda_cwd,
            docker_cmd,
        )
    else:
        lambda_cwd_on_host = Util.get_host_path_for_path_in_docker(lambda_cwd)
        cmd = (
            "%s run -i"
            ' %s -v "%s":/var/task'
            " %s"
            " %s"  # network
            " %s"  # --rm flag
            " %s %s"
        ) % (
            docker_cmd,
            entrypoint,
            lambda_cwd_on_host,
            env_vars_string,
            network_str,
            rm_flag,
            docker_image,
            command,
        )
    return cmd


# Traceback (Exception)
2019-12-20T09:45:49:DEBUG:localstack.services.awslambda.lambda_executors: Using entrypoint "/var/rapid/init --bootstrap /var/runtime/bootstrap" for container "localstack_lambda_arn_aws_lambda_us-east-1_000000000000_function_rb-files-hub-service-dev-rb-files-hub" on network "host".
2019-12-20T09:45:49:DEBUG:localstack.services.awslambda.lambda_executors: Command for docker-reuse Lambda executor:  docker exec -e NODE_ENV="$NODE_ENV" -e AWS_LAMBDA_EVENT_BODY="$AWS_LAMBDA_EVENT_BODY" -e HOSTNAME="$HOSTNAME" -e LOCALSTACK_HOSTNAME="$LOCALSTACK_HOSTNAME" -e AWS_LAMBDA_FUNCTION_NAME="$AWS_LAMBDA_FUNCTION_NAME" -e AWS_LAMBDA_FUNCTION_VERSION="$AWS_LAMBDA_FUNCTION_VERSION" -e AWS_LAMBDA_FUNCTION_INVOKED_ARN="$AWS_LAMBDA_FUNCTION_INVOKED_ARN" -e _LAMBDA_SERVER_PORT="$_LAMBDA_SERVER_PORT" localstack_lambda_arn_aws_lambda_us-east-1_000000000000_function_rb-files-hub-service-dev-rb-files-hub /var/rapid/init --bootstrap /var/runtime/bootstrap index.handler
2019-12-20T09:45:49:DEBUG:localstack.services.awslambda.lambda_executors: Running lambda cmd:  docker exec -e NODE_ENV="$NODE_ENV" -e AWS_LAMBDA_EVENT_BODY="$AWS_LAMBDA_EVENT_BODY" -e HOSTNAME="$HOSTNAME" -e LOCALSTACK_HOSTNAME="$LOCALSTACK_HOSTNAME" -e AWS_LAMBDA_FUNCTION_NAME="$AWS_LAMBDA_FUNCTION_NAME" -e AWS_LAMBDA_FUNCTION_VERSION="$AWS_LAMBDA_FUNCTION_VERSION" -e AWS_LAMBDA_FUNCTION_INVOKED_ARN="$AWS_LAMBDA_FUNCTION_INVOKED_ARN" -e _LAMBDA_SERVER_PORT="$_LAMBDA_SERVER_PORT" localstack_lambda_arn_aws_lambda_us-east-1_000000000000_function_rb-files-hub-service-dev-rb-files-hub /var/rapid/init --bootstrap /var/runtime/bootstrap index.handler
2019-12-20T09:45:49:WARNING:bootstrap.py: Thread run method <function LambdaExecutor.execute.<locals>.do_execute at 0x7f59b11db4d0>(None) failed: Traceback (most recent call last):
File "/opt/code/localstack/localstack/utils/bootstrap.py", line 413, in run
self.func(self.params)
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 70, in do_execute
result, log_output = self._execute(func_arn, func_details, event, context, version)
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 241, in _execute
result, log_output = self.run_lambda_executor(cmd, stdin, environment)
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 161, in run_lambda_executor
(return_code, result, log_output))
Exception: Lambda process returned error status code: 1. Result: . Output:
2019/12/20 09:45:49 listen tcp :9001: bind: address already in use

# Fixed code
def prepare_execution(self, func_arn, env_vars, runtime, command, handler, lambda_cwd):
    entrypoint = ""
    if command:
        entrypoint = ' --entrypoint ""'
    else:
        command = '"%s"' % handler

    # add Docker Lambda env vars
    network = config.LAMBDA_DOCKER_NETWORK
    network_str = '--network="%s"' % network if network else ""
    if network == "host":
        port = get_free_tcp_port()
        env_vars["DOCKER_LAMBDA_API_PORT"] = port
        env_vars["DOCKER_LAMBDA_RUNTIME_PORT"] = port

    env_vars_string = " ".join(
        ['-e {}="${}"'.format(k, k) for (k, v) in env_vars.items()]
    )
    debug_docker_java_port = (
        "-p {p}:{p}".format(p=Util.debug_java_port) if Util.debug_java_port else ""
    )
    docker_cmd = self._docker_cmd()
    docker_image = Util.docker_image_for_runtime(runtime)
    rm_flag = Util.get_docker_remove_flag()

    if config.LAMBDA_REMOTE_DOCKER:
        cmd = (
            'CONTAINER_ID="$(%s create -i'
            " %s"  # entrypoint
            " %s"  # debug_docker_java_port
            " %s"  # env
            " %s"  # network
            " %s"  # --rm flag
            " %s %s"  # image and command
            ')";'
            '%s cp "%s/." "$CONTAINER_ID:/var/task"; '
            '%s start -ai "$CONTAINER_ID";'
        ) % (
            docker_cmd,
            entrypoint,
            debug_docker_java_port,
            env_vars_string,
            network_str,
            rm_flag,
            docker_image,
            command,
            docker_cmd,
            lambda_cwd,
            docker_cmd,
        )
    else:
        lambda_cwd_on_host = Util.get_host_path_for_path_in_docker(lambda_cwd)
        cmd = (
            "%s run -i"
            ' %s -v "%s":/var/task'
            " %s"
            " %s"  # network
            " %s"  # --rm flag
            " %s %s"
        ) % (
            docker_cmd,
            entrypoint,
            lambda_cwd_on_host,
            env_vars_string,
            network_str,
            rm_flag,
            docker_image,
            command,
        )
    return cmd