Introduction To Containers and Docker
Introduction To Containers and Docker
By Liam Mooney
Apprentice Engineer III
As you can see in the figure above, the thing that sits between the VMs and the
host is the hypervisor layer. The hypervisor is a piece of software that virtualises
the host’s hardware and acts as the broker: managing the virtualised hardware and
feeding resources to the VMs.
This virtualisation process brings with it substantial computational overhead.
Furthermore, since each VM is basically its own machine, they have their own OS
installed, which typically require tens of gigabytes of storage, and which therefore
takes time to install, which has to be done every time you want to spin up a new
VM.
Containers
Containers take a different approach to producing isolation: like VMs, containers
live on top of a host machine and use its resources, however, instead of
virtualising the underlying hardware, they virtualise the host OS. Meaning
containers don’t need to have their own OS, making them much more lightweight
than VMs, and consequently quicker to spin up.
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 3/18
12/6/23, 10:28 AM Introduction to Containers and Docker
The parallel to the hypervisor layer with containers is the Docker daemon
(assuming you’re using Docker), it acts as the broker between the host OS and
containers. It comes with less computational overhead than hypervisor software
(as depicted by the thinner box in the figure above), again making containers more
lightweight compared to VMs.
VMs suffer from duplication: many of the capabilities and features of the guest
OS(s) are found in the host OS, so why not just use the host OS? This is what
containers aim to do, whilst still providing isolation and decoupling from software
in the host machine. With containers, only the things that the app absolutely
needs are copied into the container, as opposed to VMs were the whole OS is
installed – even the things from the OS that aren’t used by the app.
What containerisation is actually doing under the covers is some clever
misdirection whereby a container only gets to see a virtual view of the host OS; a
view that only contains the things that have been prescribed for the container –
certain things in the file system, for example.
Docker
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 4/18
12/6/23, 10:28 AM Introduction to Containers and Docker
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 5/18
12/6/23, 10:28 AM Introduction to Containers and Docker
Child images are images that build on base images and add additional
functionality, most images you’re likely to make will be child images.
Official and user images – these can both be base and child images
Official images are images that are officially maintained and supported by the
people at Docker. These are typically one word long. Examples include
python, ubuntu, and hello-world.
User images are images created and shared by people who use Docker. They
usually build on base images and add functionality. Typically these are
formatted as user/image-name.
Example: creating an image and building a container
– hello world
Creating an image
I have created a python script that simply prints “Hello world” when ran. In order to
run this app inside a Docker container, I need to first create a Dockerfile. A
Dockerfile is a text file that contains the instructions for how to create an image,
from which a container can be built – a container is an instance of an image.
Remember, an image is like a blueprint, the Dockerfile defines that blueprint. I have
created a Dockerfile in the same directory as where my hello-world.py file lives,
you can see it in the figure below.
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 6/18
12/6/23, 10:28 AM Introduction to Containers and Docker
Now, let’s run through what each of those commands in the Dockerfile mean.
FROM
Use the FROM command to specify the base image that you want your image to
derive from. Here, we’re using the Python version 3.9 base image as our app is
running in Python.
WORKDIR
Sets the current working directory inside the container. Like a cd in Linux. Any
subsequent commands in the Dockerfile will happen inside this directory.
COPY
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 7/18
12/6/23, 10:28 AM Introduction to Containers and Docker
The COPY instruction has the following format: COPY <source> <dest> . It copies
files from <source> (in the host) into <dest> (in the container). So the above
copy instruction is copying all files from the current working directory on my local
machine to the current working directory in the container.
CMD
This is the command instruction, it specifies what to run when the container is
created. Here we’re telling Docker to tell Python to run our hello-world.py app.
Now that we have the Dockerfile we can build an image from it. Do this using the
docker build command, which takes a directory and optional -t , which allows
you to add a tag to the image name.
docker build -t lgmooney98/hello-world:v1 .
The convention for naming Docker images is to use your Docker username
followed by a name, and the tag is usually used to specify the version of the
image.
docker build -t username/image-name:tag directory
So, the above command is telling Docker to look in the current directory for the
Dockerfile and build the image from it.
If you don’t already have the python 3.9 image, the build might take a few minutes
as Docker has to pull that image from the Docker registry.
We can check that our image has been created by using the docker images
command, which lists all images available locally.
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 8/18
Building the container
12/6/23, 10:28 AM Introduction to Containers and Docker
The next thing to do is run the image, which creates an instance of the image – i.e.
builds a container from the image. This is done with the docker run command.
docker run lgmooney98/hello-world
From in here I can write regular Linux commands, such as running our hello-world
python script.
Use the docker stop command to stop a running container, you need to provide
the container ID – the first few characters should suffice, Docker just needs to be
able to distinguish it from the others.
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 9/18
12/6/23, 10:28 AM Introduction to Containers and Docker
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 10/18
12/6/23, 10:28 AM Introduction to Containers and Docker
We have a different Dockerfile now, and so we need to build a new image from it.
Once the image is built, we can create a container from it using the docker run
command. However, this time we need to map the port running on the container
(5000) to a port on the outside, which in this case is my local machine, so that
when we try to connect to the external port using our browser, that port is
connected internally to the port on the container; the container can then serve the
web app to our browser. This is done using the -p flag; here, I’m mapping port
5000 on the inside to port 8888 on the outside.
docker run -p 8888:5000 lgmooney98/hello-world-app
Now, go to your browser and go to localhost:8888, you should get the hello world
web page.
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 11/18
12/6/23, 10:28 AM Introduction to Containers and Docker
I have enjoyed learning about and experimenting with containers and Docker these
past few weeks. Particularly, I'm impressed by how quick and easy it is to get
something running inside a container using Docker, which, as I mentioned, is one
of the major benefits of using containers. Topics that I haven't explored in this
post, that I think would be interesting: running a containerised app in a public
cloud provider, such as Azure, and how this process compares with running it in a
VM in the cloud; investigating the security differences between containers and
VMs; and exploring further how containerisation works under the covers.
@lg_mooney | @endjin
JONATHAN GEORGE
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 13/18
12/6/23, 10:28 AM Introduction to Containers and Docker
MIKE LARAH
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 14/18
12/6/23, 10:28 AM Introduction to Containers and Docker
JAMES DAWSON
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 15/18
Liam Mooney
12/6/23, 10:28 AM Introduction to Containers and Docker
Show Comments →
Contact Us RSS
Let's Chat @endjin
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 17/18
12/6/23, 10:28 AM Introduction to Containers and Docker
https://endjin.com/blog/2022/01/introduction-to-containers-and-docker 18/18