The rise of serverless computing offers a new level of scalability and simplicity for developers. At the same time, Docker containers remain a popular standard for packaging and running applications consistently across environments.

Thanks to AWS Lambda’s container image support, developers can now enjoy the best of both worlds: deploy containerized apps on Lambda with minimal management overhead.

This post explores how to optimize Docker for AWS Lambda, covering image design, performance tuning, and deployment strategies to build fast, lean, and maintainable serverless solutions.


Why Use Docker with AWS Lambda?

AWS Lambda supports container images up to 10 GB in size, offering benefits such as:

  • Use of any base OS, language, or runtime
  • Seamless migration of existing Docker workloads
  • Custom dependencies and binaries
  • Familiar Docker tooling and CI/CD pipelines

Use cases include:

  • ML inference with large models
  • Data processing with custom binaries
  • Migrating microservices to serverless

Step 1: Create a Lambda-Compatible Dockerfile

Lambda requires a specific entry point defined by AWS. Your Dockerfile should:

  • Use the base image amazon/aws-lambda-*
  • Include a CMD that invokes the handler
  • Keep the image size minimal

Example: Python Lambda Dockerfile

FROM public.ecr.aws/lambda/python:3.11

# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt

# Add your function code
COPY app.py .

# Set the Lambda handler
CMD ["app.handler"]

Step 2: Optimize Docker Image Size

Keep container size small to reduce cold start latency:

  • Use slim base images (python:3.11-slim, node:18-alpine)
  • Remove build-time packages after install
  • Use .dockerignore to skip unnecessary files
  • Combine layers to reduce image depth

Tip: Use multi-stage builds when compiling code or binaries.


Step 3: Build and Push to Amazon ECR

AWS Lambda pulls container images from Elastic Container Registry (ECR).

# Authenticate with ECR
aws ecr get-login-password | docker login --username AWS --password-stdin <your-ecr-uri>

# Tag and push
docker build -t lambda-docker-demo .
docker tag lambda-docker-demo <your-ecr-uri>:latest
docker push <your-ecr-uri>:latest

Step 4: Deploy to Lambda Using CLI or Console

Create a Lambda function from the container image:

aws lambda create-function \
--function-name docker-lambda-fn \
--package-type Image \
--code ImageUri=<your-ecr-uri>:latest \
--role arn:aws:iam::<account-id>:role/<lambda-execution-role>

You can also update using:

aws lambda update-function-code \
--function-name docker-lambda-fn \
--image-uri <your-ecr-uri>:latest

Step 5: Reduce Cold Starts

Cold starts in Lambda occur when functions are not warm. To minimize:

  • Keep images under 250 MB compressed
  • Use graviton2-based images for better startup time
  • Use provisioned concurrency for low-latency APIs
  • Keep handler code minimal and avoid unnecessary imports
aws lambda put-provisioned-concurrency-config \
--function-name docker-lambda-fn \
--qualifier '$LATEST' \
--provisioned-concurrent-executions 5

Best Practices for Docker + Lambda

Practice Benefit
Use small, layered images Faster pulls and startup
Run health checks locally Prevent runtime failures in Lambda
Externalize secrets Use AWS Secrets Manager or SSM Parameter
Log to stdout/stderr Automatically captured by CloudWatch Logs
Use CI/CD automation Automate image builds and ECR deployment
Monitor with X-Ray and CloudWatch Gain visibility into cold starts and errors

Debugging and Logs

All container-based Lambdas log to CloudWatch by default.

To debug issues:

  • Check CloudWatch Logs
  • Use docker run -p 9000:8080 locally to simulate Lambda
  • Test locally with curl or Postman
docker run -p 9000:8080 lambda-docker-demo
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" \
-d '{}'

Conclusion

Combining Docker with AWS Lambda provides flexibility, power, and scale. By following container optimization techniques, leveraging ECR, and tuning for cold start performance, you can build efficient serverless applications that are fast, secure, and easy to maintain.

For teams migrating legacy workloads or building modern microservices, Dockerized Lambda functions offer a strategic path to serverless adoption—without compromising control.