A DevOps engineer is tasked with automating the deployment of a microservices architecture. Each service is packaged as a Docker container. The team wants to use AWS CodePipeline and AWS CodeBuild to build Docker images and push them to Amazon ECR, then deploy to Amazon ECS. What should the CodeBuild buildspec file include to push the image to ECR?
The correct approach is to authenticate the local Docker daemon to the private ECR registry using 'aws ecr get-login-password' piped to 'docker login', then build your image with 'docker build', tag it with the ECR repository URI, and run 'docker push'. This satisfies ECR's token-based authentication and uploads Docker layers directly to the registry's S3-backed storage. It is the only way among the choices that actually moves image data into ECR.
Why this answer
To push a Docker image to Amazon ECR, the buildspec must first authenticate Docker to the ECR registry using the AWS CLI's `aws ecr get-login-password` command piped to `docker login`, then build the image with `docker build`, tag it with the ECR repository URI, and finally push it with `docker push`. CodeBuild does not have a built-in 'ecr-push' action; it relies on executing these standard Docker and AWS CLI commands in the build phases.
Exam trap
The trap here is that candidates may assume CodeBuild has a native 'ecr-push' action or that ECS APIs are involved in image pushing, when in fact the process relies on standard Docker commands and AWS CLI authentication within the buildspec.
How to eliminate wrong answers
Option A is wrong because the AWS CodeDeploy API is used for deploying applications to EC2, on-premises, or Lambda, not for pushing Docker images to ECR; pushing images is a registry operation, not a deployment action. Option B is wrong because the ECS RunTask API is used to run a standalone task in ECS, not to push images to ECR; pushing images must happen before any ECS task can reference them. Option C is wrong because CodeBuild does not have a built-in 'ecr-push' action or phase; the buildspec phases are 'install', 'pre_build', 'build', 'post_build', and custom commands must be written to perform Docker operations.