Skip to content

Intermediate · Containers in the cloud · topic 31 of 50 · 30 min

Containers, images and registries

Explanation

A container packages an app with everything it needs and runs it as an isolated process that shares the host's kernel, so it is lighter and starts faster than a virtual machine. You build an image from a Dockerfile, store it in a registry such as Amazon ECR, Azure Container Registry or Google Artifact Registry, and run it anywhere. Tags are movable labels; digests identify one exact image.

The problem containers solve

An app needs more than its code. It needs the right language version, libraries, system packages and settings. When any of these differ between a laptop and a server, the app breaks, and you hear the famous line "but it works on my machine". A container packages the app together with all of that, so the same package runs the same way everywhere.

Containers versus virtual machines

A virtual machine (VM) pretends to be a whole computer. It runs its own full operating system, including its own kernel (the core of the operating system that talks to the hardware). A container does not. It is a normal process on the host that shares the host's kernel, and Linux features called namespaces and cgroups give it its own view of files, network and processes, and limits on CPU and memory.

Virtual machineContainer
What is insideA full guest operating system plus the appThe app, its libraries and a minimal file system
Typical sizeGigabytesTens to hundreds of megabytes
Start timeTens of seconds to minutesUsually under a few seconds
IsolationStrong: separate kernelsLighter: shared kernel
How many per serverA handful to tensTens to hundreds
Think of it like this: A VM is like renting a separate flat for each family, each with its own kitchen and water tank. Containers are like rooms in a well-run PG hostel: everyone shares the building's water and electricity (the kernel), but each room has its own lock, its own things and a fair share of the supply.

Because containers share a kernel, their isolation is weaker than a VM's. That is why cloud services often run containers inside lightweight VMs. AWS Fargate, for example, gives each task its own isolation boundary that does not share a kernel with other customers' tasks.

Images and layers

An image is a read-only template for a container: a small file system, your app and the command to start it. A container is a running instance of an image, with a thin writable layer on top. You can start many containers from one image.

An image is made of layers. Each instruction in a Dockerfile that changes files (such as COPY or RUN) adds a layer. Layers are cached and shared: if ten images use the same python:3.12-slim base, a server downloads that base only once, and a rebuild reuses every layer that did not change.

A small Dockerfile

dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN useradd --create-home appuser
USER appuser
EXPOSE 8080
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "app:app"]
Dependencies are installed before the code is copied, so a code change does not reinstall every library.

Registries

A registry stores and serves images. You push an image to it once, and every server or cloud service pulls from it. The image reference says where the image lives.

RegistryExample image reference
Docker Hub (public images)docker.io/library/nginx:1.27, usually written just nginx:1.27
Amazon ECR123456789012.dkr.ecr.ap-south-1.amazonaws.com/orders-api:1.0.0
Azure Container Registrymyshop.azurecr.io/orders-api:1.0.0
Google Artifact Registryasia-south1-docker.pkg.dev/shop-prod/apps/orders-api:1.0.0

Google Artifact Registry replaced the older Container Registry, so new Google Cloud projects use Artifact Registry.

Build and push to Amazon ECR

bash
aws ecr create-repository --repository-name orders-api --region ap-south-1
aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-south-1.amazonaws.com
docker build -t orders-api:1.0.0 .
docker tag orders-api:1.0.0 123456789012.dkr.ecr.ap-south-1.amazonaws.com/orders-api:1.0.0
docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/orders-api:1.0.0
Create a repository, log Docker in with a short-lived token, build, tag with the registry address, and push.

Tags versus digests

A tag such as 1.0.0 or latest is a label that points to an image. Tags can be moved: someone can push a different image with the same tag. A digest such as sha256:3f9c... is a hash of the image content. It always points to exactly one image and can never change. You can write both: orders-api:1.0.0@sha256:3f9c...

Tip: In production, deploy a specific version tag or a digest, never latest. Amazon ECR can make tags immutable, so a tag like 1.0.0 can never be overwritten. That makes rollbacks safe: you know exactly what each tag contains.

Why containers suit microservices

A microservice is a small service that does one job, such as payments or notifications, and talks to others over the network. Containers fit them well: each service has its own image with its own language and libraries, can be released on its own schedule, and can be scaled on its own. The same image moves unchanged from a developer laptop to testing to production.

Careful: Common mistakes: deploying the latest tag (you cannot tell what is running; use versions or digests); baking secrets into the image (anyone who pulls it can read them; pass them at run time); huge images (use slim bases, a .dockerignore file and multi-stage builds); running as root (add a USER line); and building on an Apple Silicon or other ARM laptop, then seeing "exec format error" on an x86 server (build for the right platform, for example docker buildx build --platform linux/amd64).

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Containers, images and registries · 1/8

Containers

A container packages your app with everything it needs, so it runs the same everywhere.

Prefer a video by a teacher? Find free videos on “containers vs virtual machines docker images registry tutorial” on YouTube ↗

In real life: Ticket booking during a sale

A movie ticket booking company in Mumbai splits its system into containers: search, seat locking, payments and notifications. Each team builds its image in CI, tags it with the Git commit, and pushes it to Amazon ECR with immutable tags. Before a big Friday release, the seat-locking service needs more copies, so it scales from 4 to 20 containers in under a minute without touching the payment service. When a bad release slips through, the team redeploys the previous tag in two minutes, because that tag still points to exactly the same image.

Practice (0/3 done)

Task 1 · Code · Python

Write is_pinned(ref) that returns True when an image reference is pinned to an exact image by a digest (it contains "@sha256:"), and False otherwise.

Task 2 · Code · Python

Write parse_image_ref(ref) that splits an image reference into a dict with "registry", "repository", "tag" and "digest". Rules: (1) a digest comes after "@". (2) The part before the first "/" is the registry only if it contains "." or ":" or is "localhost"; otherwise the registry is "docker.io". (3) A tag comes after the last ":" that is AFTER the last "/" (so the port in localhost:5000 is not a tag). (4) For docker.io, a repository with no "/" gets "library/" in front. (5) If there is neither a tag nor a digest, the tag is "latest". Missing parts are None.

Task 3 · Practice

Write a Dockerfile for a small Node.js or Python web app, build it locally, and push it to a registry you can use for free (for example a private repository in Amazon ECR within the free tier, or Docker Hub). Note the image size and its digest.

Quick quiz

Question 1

1.Why does a container usually start faster than a virtual machine?

Question 2

2.What is Google Cloud's current registry for container images?

Question 3

3.Which reference is guaranteed to always mean exactly the same image?

Question 4

4.Why copy requirements.txt and install dependencies BEFORE copying the rest of the code?

Question 5

5.An image built on an ARM laptop fails on an x86 server with "exec format error". What is the fix?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Why does a container usually start faster than a virtual machine?

Answer: It shares the host kernel, so no whole operating system has to boot

A VM boots a full guest OS; a container is just an isolated process on an already running kernel.

What is Google Cloud's current registry for container images?

Answer: Artifact Registry

Artifact Registry replaced Container Registry.

Which reference is guaranteed to always mean exactly the same image?

Answer: orders-api@sha256:3f9c...

A digest is a content hash; a tag can be moved to another image (unless the registry makes tags immutable).

Why copy requirements.txt and install dependencies BEFORE copying the rest of the code?

Answer: So a code change can reuse the cached dependency layer and build faster

Layers are cached; if requirements.txt did not change, the slow install step is reused.

An image built on an ARM laptop fails on an x86 server with "exec format error". What is the fix?

Answer: Build for the right platform, for example with docker buildx build --platform linux/amd64

The binary inside was built for the wrong CPU architecture.

Why does a container start much faster than a virtual machine?Related

Answer: It does not boot an operating system kernel; it is just an isolated process

The host kernel is already running, so starting a container is close to starting a process.

From Containers, images and registries

What do containers share with the host that virtual machines do not?Related

Answer: The kernel

Containers run on the host kernel.

From Containers with Podman: images, run, ports and logs

What is the difference between an image and a container?Related

Answer: An image is the package (recipe); a container is a running copy of it

You build an image once and start one or many containers from it.

From Docker: packaging your app

What does pinning an image by digest (image@sha256:...) give you?Related

Answer: The exact same image content every time, because a digest cannot be moved like a tag

A digest is a content hash. Remember to update pins on a schedule so patches still arrive.

From Image and container security

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “Containers in the cloud” before its exam: Amazon ECS and AWS Fargate, Managed Kubernetes: EKS, AKS and GKE, Choosing where to run a container.
  2. Module 8 exam: Containers in the cloud · 12 questions · 20 min, without help. 4 topics of this module still to finish first.
  3. Then the next module, Infrastructure as code. 50 topics left in the course before the final exam.

← Previous: The command line on all three clouds