Skip to content

Containers and images: what you actually deploy

A new version of a service is more than code. It needs a runtime, libraries, and some default settings, and a machine that has a different runtime or an older library behaves differently. Every other lesson in this module is about how to put a new version in front of users. This one is about what the new version is: the single, fixed thing that you hand to the machines.

Docker describes an image as a standardized package that includes all of the files, binaries, libraries and configuration needed to run a container. Kubernetes’ documentation says the same in other words: a ready-to-run software package with the code, the runtime it needs, the libraries, and default values for essential settings. The Open Container Initiative’s image specification names the parts: a manifest, a set of filesystem layers, and a configuration, where the manifest identifies each layer by its content.

A container is what you get when you start an image. Docker calls an image a read-only template for creating a container and a container a runnable instance of an image. One image can start any number of containers, in the way that one class can make any number of objects. Your fleet of instances in the next lessons is a number of containers started from the same image.

Mental model. An image is a sealed package with a version. A container is a running copy. You deploy a version by starting copies of its package and stopping copies of the old one, not by editing anything that is already running.

Docker’s documentation says that once an image is created it cannot be modified: you can only make a new image, or add changes on top of it. Kubernetes goes one step further and says what that means for running containers. Containers are meant to be stateless and immutable, and you should not change the code of a container that is already running. If you want to make a change, the correct process is to build a new image that includes it, and then recreate the container from the updated image.

This is the reason a deployment is a replacement. There is no step where a person logs in to a machine and patches it, so two machines that run the same version are the same, and a machine cannot drift from the rest. It is also what makes the next lessons possible: to go back to the old version you start containers from the old image again. There is nothing to undo, because nothing was changed.

Naming the image matters. A tag such as shop-api:2.4 is a label, and Kubernetes’ documentation warns that tags can be moved to point to a different image. A digest is a hash of the image’s content, so it identifies one exact image and cannot move. The same documentation advises against the latest tag in production, because it is harder to track which version is running and harder to roll back properly. So the unit that you deploy, and that you go back to, is best written as an image name and a digest.

It is not a virtual machine. A virtual machine includes a full copy of an operating system, and a hypervisor lets several of them share one machine. Docker describes a container as an isolated process with all the files it needs to run. Containers on a machine share its operating system kernel, each as an isolated process in user space, and so a container does not carry an operating system of its own. Docker’s overview describes containers as lightweight.

It is not a place to keep data. Kubernetes’ documentation says that on-disk files in a container are ephemeral: when a container crashes or is stopped, the files it created or changed are lost, and a restart begins from a clean state. A replaced container loses them the same way. State belongs outside, in a database, a cache or a volume that outlives the container. This is the stateless service of the scalability module again, and it is what lets a deployment stop any instance at any time.

It is not a way of rolling out a change. An image says what to run. It does not say how many copies to start first, how long to wait before sending them traffic, or when to stop the old ones. That is the job of a deployment strategy, and it is what the rest of this module compares.

From image to serving: a start has a price

Section titled “From image to serving: a start has a price”

An instance does not serve the moment it is started. The image has to be available on the machine, the process has to start, and then the instance has to be able to take requests. Kubernetes separates the last step into probes: a readiness probe tells when a container is ready to accept traffic, and a failing one removes the instance from the services that route to it, whereas a liveness probe tells when to restart a container. The health checks lesson has the same idea on a load balancer.

The deployment lab that the next lessons use turns all of this into two settings. Time to start an instance is 30 s by default and Readiness check is 20 s, so a new instance starts serving after 30 + 20, which is 50 s, and is paid for in that time. Every strategy you will see pays this price at least once, and the differences between the strategies are largely about what the users get while it is being paid. An autoscaling group pays the same price whenever it adds an instance.

Suppose two machines each pull shop-api:latest for a new instance, a few minutes apart, and in between the registry’s latest was moved to a newer image. Kubernetes’ documentation names this risk: if the registry changes the code that a tag represents, you might end up with a mix of instances running old and new code. Nothing in your deployment decided to run two versions, and the dashboards would say “version latest” for both. Pinning the digest means every start runs exactly the same bytes, and a rollback goes to a digest that you can name.

Put each statement under the thing it describes.

Challenge

Image or container?

An image is the fixed package. A container is a process started from it. Put each statement under the one it describes.

Sorting needs JavaScript.

Categories

Image
A read-only package of files, libraries and settings.
Container
A running, isolated process started from an image.
  1. Line 1 of 8It is a read-only template: a change means building a new one.
  2. Line 2 of 8It is identified exactly by a digest, a hash of its content.
  3. Line 3 of 8It is made of layers, each adding, removing or changing files.
  4. Line 4 of 8A rollback goes back to the previous one of these.
  5. Line 5 of 8It is an isolated process that shares the host's kernel.
  6. Line 6 of 8Files written to its own disk are lost when it is replaced.
  7. Line 7 of 8Ten of them can run at once from the same package.
  8. Line 8 of 8It can fail its readiness check and be taken out of the traffic.

Check your answers at least once before looking at the answers.

  • An image is a fixed, read-only package of the files, libraries and settings a service needs. A container is a running copy started from it.
  • Images are immutable: a change is a new image, and a deployment replaces containers instead of changing them. That is what makes a rollback a matter of starting the old image again.
  • A tag can be moved; a digest is a hash of the content and cannot. The latest tag is a poor way to name a version in production.
  • A container is not a virtual machine (it shares the host’s kernel), not a place for data, and not a rollout strategy.
  • A new instance serves nothing while it starts and while its readiness check passes: 30 s and 20 s, which is 50 s, in the lab. The strategies differ in what the users get in that time.

Next: rolling deployments and zero downtime, the first way to use that time.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.