docker - sam build --use-container 失败,但 sam build 成功

标签 docker docker-network aws-sam aws-sam-cli

此问题发生在以下设置中:

  1. 在 OpenStack 中启动虚拟机
  2. 操作系统是 Ubuntu 16.04 LTS
  3. 安装了 virtualenv 的 Python 版本 3.7.6
  4. SAM CLI 版本 0.39.0

要重现问题,您可以使用上述设置并执行以下步骤:

  1. $ sam init
  2. $选择:1
  3. $ Runtime 9 选择 python3.7
  4. $项目名称[sam-app]:sam-app
  5. $ 模板选择:1 选择 Hello World 示例

等待应用程序生成。

  • $ cd sam-app
  • $ python3 -m virtualenv venv
  • $ 源 venv/bin/activate
  • $(venv) 源 venv/bin/activate
  • $(venv) sam build
  • 应出现以下输出:

    Building resource 'HelloWorldFunction'
    Running PythonPipBuilder:ResolveDependencies
    Running PythonPipBuilder:CopySource
    
    Build Succeeded
    
    Built Artifacts  : .aws-sam/build
    Built Template   : .aws-sam/build/template.yaml
    
    Commands you can use next
    =========================
    [*] Invoke Function: sam local invoke
    [*] Deploy: sam deploy --guided
    
    

    但是,如果使用--use-container标志,则会出现以下错误

    Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
    'build' command is called
    Starting Build inside a container
    No Parameters detected in the template
    2 resources found in the template
    Found Serverless function with name='HelloWorldFunction' and CodeUri='hello_world/'
    Building resource 'HelloWorldFunction'
    
    Fetching lambci/lambda:build-python3.7 Docker container image......
    Mounting /home/ubuntu/test/sam-app/hello_world as /tmp/samcli/source:ro,delegated inside runtime container
    Using the request object from command line argument
    Loading workflow module 'aws_lambda_builders.workflows'
    Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
    Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
    Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
    Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
    Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
    Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
    Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
    Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
    Found workflow 'PythonPipBuilder' to support capabilities 'Capability(language='python', dependency_manager='pip', application_framework=None)'
    Running workflow 'PythonPipBuilder'
    Running PythonPipBuilder:ResolveDependencies
    calling pip download -r /tmp/samcli/source/requirements.txt --dest /tmp/samcli/scratch
    PythonPipBuilder:ResolveDependencies failed
    Traceback (most recent call last):
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/actions.py", line 42, in execute
        requirements_path=self.manifest_path,
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 137, in build_dependencies
        self._dependency_builder.build_site_packages(requirements_path, artifacts_dir_path, scratch_dir_path)
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 198, in build_site_packages
        wheels, packages_without_wheels = self._download_dependencies(scratch_directory, requirements_filepath)
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 222, in _download_dependencies
        deps = self._download_all_dependencies(requirements_filename, directory)
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 305, in _download_all_dependencies
        self._pip.download_all_dependencies(requirements_filename, directory)
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 594, in download_all_dependencies
        raise NoSuchPackageError(str(package_name))
    aws_lambda_builders.workflows.python_pip.packager.NoSuchPackageError: Could not satisfy the requirement: requests
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflow.py", line 269, in run
        action.execute()
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/actions.py", line 45, in execute
        raise ActionFailedError(str(ex))
    aws_lambda_builders.actions.ActionFailedError: Could not satisfy the requirement: requests
    Builder workflow failed
    Traceback (most recent call last):
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/actions.py", line 42, in execute
        requirements_path=self.manifest_path,
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 137, in build_dependencies
        self._dependency_builder.build_site_packages(requirements_path, artifacts_dir_path, scratch_dir_path)
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 198, in build_site_packages
        wheels, packages_without_wheels = self._download_dependencies(scratch_directory, requirements_filepath)
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 222, in _download_dependencies
        deps = self._download_all_dependencies(requirements_filename, directory)
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 305, in _download_all_dependencies
        self._pip.download_all_dependencies(requirements_filename, directory)
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/packager.py", line 594, in download_all_dependencies
        raise NoSuchPackageError(str(package_name))
    aws_lambda_builders.workflows.python_pip.packager.NoSuchPackageError: Could not satisfy the requirement: requests
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflow.py", line 269, in run
        action.execute()
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflows/python_pip/actions.py", line 45, in execute
        raise ActionFailedError(str(ex))
    aws_lambda_builders.actions.ActionFailedError: Could not satisfy the requirement: requests
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/__main__.py", line 126, in main
        mode=params.get("mode", None),
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/builder.py", line 125, in build
        return workflow.run()
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflow.py", line 76, in wrapper
        func(self, *args, **kwargs)
      File "/var/lang/lib/python3.7/site-packages/aws_lambda_builders/workflow.py", line 276, in run
        raise WorkflowFailedError(workflow_name=self.NAME, action_name=action.NAME, reason=str(ex))
    aws_lambda_builders.exceptions.WorkflowFailedError: PythonPipBuilder:ResolveDependencies - Could not satisfy the requirement: requests
    Build inside container returned response {"jsonrpc": "2.0", "id": 1, "error": {"code": 400, "message": "PythonPipBuilder:ResolveDependencies - Could not satisfy the requirement: requests"}}
    
    Build Failed
    Sending Telemetry: {'metrics': [{'commandRun': {'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 107282, 'exitReason': 'BuildError', 'exitCode': 1, 'requestId': '62d1fc73-70e5-4592-8c78-8fa273684592', 'installationId': 'ce8ffa14-684f-4628-97fe-288848fcf73d', 'sessionId': '4ca5bb42-5cf1-4ae5-89ad-9ed00366fefb', 'executionEnvironment': 'CLI', 'pyversion': '3.7.6', 'samcliVersion': '0.39.0'}}]}
    HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
    Error: PythonPipBuilder:ResolveDependencies - Could not satisfy the requirement: requests
    
    

    最佳答案

    上述问题的出现是由于PythonPipBuilder无法解决Python的依赖关系。后来我登录docker并手动安装依赖,随后我怀疑这是由于docker网络问题造成的。

    随后,通过添加额外的标志来构建 sam 应用程序,使 docker 能够使用主机网络。

    $(venv) sam build --use-container --docker-network host --debug

    命令的输出:

    Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
    'build' command is called
    Starting Build inside a container
    No Parameters detected in the template
    2 resources found in the template
    Found Serverless function with name='HelloWorldFunction' and CodeUri='hello_world/'
    Building resource 'HelloWorldFunction'
    
    Fetching lambci/lambda:build-python3.7 Docker container image......
    Mounting /home/ubuntu/test/sam-app/hello_world as /tmp/samcli/source:ro,delegated inside runtime container
    Using the request object from command line argument
    Loading workflow module 'aws_lambda_builders.workflows'
    Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
    Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
    Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
    Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
    Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
    Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
    Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
    Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
    Found workflow 'PythonPipBuilder' to support capabilities 'Capability(language='python', dependency_manager='pip', application_framework=None)'
    Running workflow 'PythonPipBuilder'
    Running PythonPipBuilder:ResolveDependencies
    calling pip download -r /tmp/samcli/source/requirements.txt --dest /tmp/samcli/scratch
    Full dependency closure: {requests==2.23.0(wheel), certifi==2019.11.28(wheel), urllib3==1.25.8(wheel), chardet==3.0.4(wheel), idna==2.9(wheel)}
    initial compatible: {requests==2.23.0(wheel), certifi==2019.11.28(wheel), urllib3==1.25.8(wheel), chardet==3.0.4(wheel), idna==2.9(wheel)}
    initial incompatible: set()
    Downloading missing wheels: set()
    compatible wheels after second download pass: {requests==2.23.0(wheel), certifi==2019.11.28(wheel), urllib3==1.25.8(wheel), chardet==3.0.4(wheel), idna==2.9(wheel)}
    Build missing wheels from sdists (C compiling True): set()
    compatible after building wheels (no C compiling): {requests==2.23.0(wheel), certifi==2019.11.28(wheel), urllib3==1.25.8(wheel), chardet==3.0.4(wheel), idna==2.9(wheel)}
    Build missing wheels from sdists (C compiling False): set()
    compatible after building wheels (C compiling): {requests==2.23.0(wheel), certifi==2019.11.28(wheel), urllib3==1.25.8(wheel), chardet==3.0.4(wheel), idna==2.9(wheel)}
    Final compatible: {chardet==3.0.4(wheel), requests==2.23.0(wheel), certifi==2019.11.28(wheel), urllib3==1.25.8(wheel), idna==2.9(wheel)}
    Final incompatible: set()
    Final missing wheels: set()
    PythonPipBuilder:ResolveDependencies succeeded
    Running PythonPipBuilder:CopySource
    PythonPipBuilder:CopySource succeeded
    Build inside container returned response {"jsonrpc": "2.0", "id": 1, "result": {"artifacts_dir": "/tmp/samcli/artifacts"}}
    Build inside container was successful. Copying artifacts from container to host
    Copying from container: /tmp/samcli/artifacts/. -> /home/ubuntu/test/sam-app/.aws-sam/build/HelloWorldFunction
    Build inside container succeeded
    
    Build Succeeded
    
    Built Artifacts  : .aws-sam/build
    Built Template   : .aws-sam/build/template.yaml
    
    Commands you can use next
    =========================
    [*] Invoke Function: sam local invoke
    [*] Deploy: sam deploy --guided
    
    Sending Telemetry: {'metrics': [{'commandRun': {'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 5544, 'exitReason': 'success', 'exitCode': 0, 'requestId': 'bd9e7b0a-82ac-40a8-a172-a2d49f0633ff', 'installationId': 'ce8ffa14-684f-4628-97fe-288848fcf73d', 'sessionId': '48762175-e559-4759-b2fa-6a66822e381e', 'executionEnvironment': 'CLI', 'pyversion': '3.7.6', 'samcliVersion': '0.39.0'}}]}
    

    感谢我的同事 Phetsouvanh Silivanxay 博士一起解决了这个问题。

    关于docker - sam build --use-container 失败,但 sam build 成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60334350/

    相关文章:

    macos - 在Mac OS上运行Clickhouse Docker镜像时出错

    arrays - 如何将字符串拆分为数组并循环遍历它?

    amazon-web-services - AWS CodePipeline - 在 CloudFormation 中部署 : Action execution failed Requires capabilities : [CAPABILITY_AUTO_EXPAND]

    amazon-web-services - 在 AWS SAM/Cloudformation 模板中管理阶段和生产环境以及 CI/CD 支持

    amazon-web-services - 在 SAM 模板中使用 !If 内部函数

    docker - 如何将A类IP子网添加到docker0网桥

    php - Mysql 连接在 docker 中被拒绝

    networking - 如何从外部世界向 AWS EC2 机器内的 docker 容器内运行的 Web 服务器发送请求?

    mysql - 使用 docker --network 连接 2 个容器

    docker - 在Docker网络中跨容器连接