Docker and Container Images
Build small, secure container images with Dockerfiles, run multi-container apps locally with Compose and push images to Amazon ECR.
Intermediate · about 8 hours · difficulty 2/5
Learn first: Linux Command Line for Cloud Servers, AWS CLI, CloudShell and SDKs
Containers package an app with its runtime and dependencies so it runs the same on your laptop, in CI and in the cloud. Every orchestrator on AWS starts from a container image.
Core concepts
- An image is a read-only template built in layers from a Dockerfile. A container is a running instance of it.
- Registries store images: Docker Hub, GitHub Container Registry and Amazon ECR (Azure Container Registry, Google Artifact Registry).
- Volumes keep data outside the container's writable layer; port mapping exposes services.
Writing good Dockerfiles
- Start from small, official base images (alpine, distroless, slim).
- Use multi-stage builds so build tools do not ship to production.
- Order instructions so dependency installs are cached before copying source code.
- Run as a non-root user and add a .dockerignore.
- Build for linux/arm64 too if you plan to run on Graviton or Fargate ARM.
Docker Compose
Compose runs an app plus its PostgreSQL and Redis locally with one command, perfect for development.
Amazon ECR
- Private repositories with IAM access control.
- Image scanning for known vulnerabilities, lifecycle policies to delete old images.
- Log in with aws ecr get-login-password piped into docker login.
Pitfalls
- Baking secrets or .env files into images.
- 1 GB images built from full OS bases.
- Using the latest tag in production, so you never know what is running.
You are ready to move on when you can containerize a small API with a multi-stage Dockerfile under 150 MB, run it with Compose alongside a database, and push it to ECR.
Key concepts
- Images, containers and layers
- Dockerfile instructions
- Multi-stage builds
- Non-root containers
- Docker Compose
- Amazon ECR
- Image scanning and lifecycle policies
- Multi-arch builds with buildx
Mini project
Containerize a small Node or Python API with a multi-stage Dockerfile, run it with Postgres using Docker Compose, then push the image to a private ECR repository with scanning on push. Keep one or two images only (ECR free tier storage is limited), add a lifecycle policy, and delete the repository when you finish this section.
Check yourself
Why does a multi-stage build produce a smaller and safer image than a single-stage one?
Free resources
- Docker documentation (Docker)
- Amazon ECR documentation (AWS)
- Docker roadmap (roadmap.sh)