The Case of the Zombie Code: Why my EC2 refused to update 🕵️♂️
Source: Dev.to
The Culprit? Docker’s local image cache
Docker prefers to reuse an image it already has tagged latest. When you tell it to run the latest image, it checks its local cache first. If it finds an image with the latest tag, it assumes it’s up‑to‑date and skips pulling the new version, even if the contents have changed.
Investigation: How to Force a “Search and Seizure”
Step 1 – Prove who you are (ECR login)
aws ecr get-login-password --region YOUR_REGION \
| docker login --username AWS --password-stdin YOUR_ECR_REGISTRY
Step 2 – Stop the “imposter” container
docker stop backend-app
docker rm backend-app
Step 3 – Delete the cached image (the cache buster)
docker rmi YOUR_ECR_REGISTRY/YOUR_REPO:latest
Step 4 – Pull the fresh image and run it
docker pull YOUR_ECR_REGISTRY/YOUR_REPO:latest
docker run -d --name backend-app -p 8081:8081 \
--restart always YOUR_ECR_REGISTRY/YOUR_REPO:latest
The “Senior Dev” Advice
Use the commit SHA as your tag
- Build and tag the image with the Git commit SHA (e.g.,
my-app:8f2a4b1). - Have your EC2 deployment script request that specific SHA.
- Docker will see that it doesn’t have the exact version in its cache and will pull it automatically, eliminating “zombie” deployments.
Closing Thoughts
Cache issues can waste hours of debugging. Using immutable tags like commit SHAs helps ensure you always run the intended code version.
Tags: #Docker #DevOps #EC2 #ECR