Hello, and welcome to this series covering using Podman.
Today we will briefly explore how layers are used to build a container image,
Along the way we'll also learn the difference between an image and a container.
Before finishing up with a look at the layers within an image that we have created.
Let's start by outlining a key aspect of working with containers, namely that means understanding
how the layers are used to build the images from which containers are created.
In a little more detail,
Layers are the building blocks of images
Layers created as a read-only copy of the result of each instruction within a dockerfile
These layers are then collated together into an image
Once created the image is used as template which can then be instantiated to create a
container.
Podman uses this image to create a container adding a read/write layer in the process so
the application can work properly.
Using layers in this manner has advantages, because it allows these layers, once downloaded
into the Podman, or Docker, cache to be shared between images and their resulting containers.
Here we see three deployments, all slightly different.
Notice how all three containers use the same initial layer?
Likewise containers #1 & #2 use the same secondary layer.
So perhaps this means that a change in any of these 'shared' layers within a running
container affects all of the other running containers, or possibly even the original
image?
Well, no it doesn't, because each container process is completely isolated from any other,
along with the image layers being read-only any changes (eg: log files, etc.) are made
within the container's own read/write layer.
Another benefit of using layers to create images, is that each layer is unique as well
being read-only.
Meaning the layer only needs to be downloaded once, thereafter it can be shared with any
other image dependant upon it - thereby saving physical storage requirements on the host
computer.
This contrasts with a virtual machine, where each instance requires its own physical copy
to be downloaded.
Anyway let's return to looking at the image layers...
As mentioned previously the files comprising each layer are contained within a read-only
layer.
This affects us in a couple of ways:
Firstly, if anything about the application changes then the image has be recreated from
the dockerfile.
It also means that any changes made whilst the image is running (ie: as a container)
are actually stored within the container's read/write layer.
Those changes, whatever they are, including deleting the file have no effect on the underlying
layers below.
Hence explaining why deleting a file within a container doesn't affect the image's file
size on the host computer.
Anyway, that's enough theory for today, let's take a look at an example...
This shows a simple dockerfile that will construct an image and save it locally.
What this dockerfile shows is that it will carry out these actions:
* Download an Oracle Linux 8 base image * Install Python 3
* Install Flask * Install the Flask-MySQL extension
So now I'll switch to my terminal and demonstrate creating this image
The first step is to build the image by issuing the
podman build -t .
This will take a few seconds to complete, so I've sped it up slightly...
Once it is built, let's confirm Podman shows it by listing the available images like so:
podman images
Notice two images are listed, an Oracle Linux 8 image and the image we just created.
This is because the first step in our dockerfile was to use an Oracle Linux 8 image as our
base.
We can confirm the steps completed by looking at our image's history via this command:
podman history
Notice that it return five lines - the upper three directly correspond to the dockerfile
actions to install Python 3 and Flask
While the bottom two lines originate with the Oracle Linux 8 image the same dockerfile
started with.
The 'ID' column shows the layer's ID, and the 'CREATED BY' column shows the dockerfile
action that created it.
There is another method to see this in more detail, by using this command:
podman history
So before we leave it may be a good idea to clean up behind ourselves...
Starting with removing the Oracle Linux 8 image using: podman rmi oraclelinux
Notice this is completed in one action
Next, remove the Flask image we just created using: podman rmi flask_test
Notice that this time there are three lines - these represent the three layers created
when the image itself was created earlier via the dockerfile actions.
That completes this short introduction too layers in containers.
Many thanks for taking the time out to watch, and be sure to keep an eye for further instalments
in this series...
For additional information on Oracle Podman please access the Oracle Linux documentation,
or Oracle Linux Learning Library.