Images and Registries
Docker images are read-only templates. You build them from a Dockerfile, tag them for a registry, and push or pull them to share or deploy.
Building Images
Section titled “Building Images”# Build from current directory (looks for Dockerfile)docker build -t myapp:1.0 .
# Build from a different path or Dockerfiledocker build -f Dockerfile.prod -t myapp:prod ./appThe -t flag tags the image (name and optional tag). If you omit the tag, Docker uses latest.
Image Layers
Section titled “Image Layers”Each Dockerfile instruction that changes the filesystem creates a layer. Layers are cached; if a step hasn’t changed, Docker reuses the cached layer. Put rarely changing steps (e.g. COPY package*.json and RUN npm ci) before frequently changing ones (e.g. COPY . .) to improve cache hits.
Tagging
Section titled “Tagging”Tags identify a specific image. Format: [registry/][owner/]name[:tag].
# Local tagdocker tag myapp:1.0 myapp:latest
# Tag for Docker Hub (push to your username)docker tag myapp:1.0 myuser/myapp:1.0
# Tag for Amazon ECRdocker tag myapp:1.0 123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:1.0Registries
Section titled “Registries”| Registry | Use case |
|---|---|
| Docker Hub | Public and private images; docker.io by default. |
| Amazon ECR | AWS; integrates with IAM and ECS/EKS. |
| Google Container Registry (GCR) | GCP; integrates with GKE. |
| GitHub Container Registry (ghcr.io) | Store images next to your repos. |
Pushing and Pulling
Section titled “Pushing and Pulling”# Log in to Docker Hubdocker login
# Push to Docker Hubdocker push myuser/myapp:1.0
# Pull an image (defaults to Docker Hub if no registry in the name)docker pull nginx:alpinedocker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/myapp:1.0For ECR, authenticate with the AWS CLI first: aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com.
Useful Commands
Section titled “Useful Commands”docker image ls # List local imagesdocker image rm <id> # Remove an imagedocker image prune # Remove dangling imagesdocker image inspect <id> # Inspect image metadata and layersKey Takeaways
Section titled “Key Takeaways”- Build with
docker build -t name:tag .; use layer order and .dockerignore for fast, repeatable builds. - Tag images for your registry (e.g.
myuser/myapp:1.0or ECR URL). - Push after
docker login; pull to fetch images. Use ECR/GCR/ghcr.io for private or cloud-native workflows.