Skip to content

Build docker image for Django project running on lambda

0

Hi, I'm using this docker build -f <myproject>.Dockerfile -t myproject . to build image push to ECR, but the lambda function cannot pull it because of the image index issue. So I find a solution in the AWS forum with DOCKER_BUILDKIT=0 docker build -f <myproject>.Dockerfile -t myproject . to build image push to ECR and running lambda on it which is work!. But it is a Docker legacy builder, is no longer the recommended method for building Docker images.

I wonder if there is another way to adjust ECR or lambda or another way to build docker image running on lambda.

Thank you!

1 Answer
0
Accepted Answer

When building Docker images for Lambda functions, especially on newer architectures like M2 Macbooks, you need to ensure compatibility with Lambda's container image requirements.

The issue you're experiencing is because Lambda doesn't support multi-architecture container images. Even though Lambda provides multi-architecture base images, the image you build must target only one specific architecture.

Instead of using the legacy builder with DOCKER_BUILDKIT=0, you should use the following approach:

docker buildx build --platform linux/amd64 --provenance=false -t myproject .

The key components here are:

  • --platform linux/amd64 - Specifies the target architecture (use x86_64 for Lambda)
  • --provenance=false - This is critical as Lambda doesn't support the default provenance metadata

If you're targeting ARM-based Lambda functions, you would use:

docker buildx build --platform linux/arm64 --provenance=false -t myproject .

After building the image, you can tag and push it to ECR as usual:

docker tag myproject:latest <your-account-id>.dkr.ecr.<region>.amazonaws.com/myproject:latest
docker push <your-account-id>.dkr.ecr.<region>.amazonaws.com/myproject:latest

This approach uses the modern Docker buildx plugin while ensuring compatibility with Lambda's container image requirements.
Sources
How to build container images on M2 Macbook and use them in Lambda functions | AWS re:Post
Deploy Python Lambda functions with container images - AWS Lambda

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.