You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Serverless Framework部署Dockerfile与源码分离的AWS Lambda

解决Serverless Framework部署跨目录Docker镜像Lambda的问题

要解决Dockerfile无法找到src目录的问题,你需要在Serverless配置中明确指定Docker构建上下文和Dockerfile路径,逻辑和docker-compose的配置一致:

修改serverless.yml的ECR镜像配置

将原来的path参数替换为context和dockerfile,让Docker以项目根目录作为构建上下文,从而访问到src文件夹:

service: MySelector
provider:
  name: aws
  profile: personal-profile
  region: eu-west-1
  runtime: python3.9
  ecr:
    images:
      instagramdockerimage:
        context: ../  # 从serverless.yml所在的build目录,指向包含src的项目根目录
        dockerfile: build/instagram/Dockerfile  # 相对于项目根目录的Dockerfile路径

functions:
  instagram:
    image:
      name: instagramdockerimage
    timeout: 900 # Max out timeout of 15 mins
    memorySize: 200
    events:
      - schedule: rate(2 hours)
    environment:
     ...

配置说明

  • context: ../:设置Docker构建上下文为项目根目录(包含src和build的父目录),确保Docker能访问上下文内的所有文件/文件夹。
  • dockerfile: build/instagram/Dockerfile:指定相对于构建上下文的Dockerfile路径,告诉Docker去哪里读取构建脚本。

验证Dockerfile命令

你的Dockerfile中COPY src ${LAMBDA_TASK_ROOT}和COPY pyproject.toml .命令现在可以正常执行,因为构建上下文是项目根目录,src和pyproject.toml(假设位于根目录)都在上下文范围内。

内容的提问来源于stack exchange,提问作者Alistair Hughes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 03:25:20