Introducing Containers
The Docker Hub
Figure 1
Figure 2
Figure 3
Figure 4
Figure 5
Figure 6
Figure 7
Docker Desktop
Figure 1
Setting up docker in Windows or Mac will have installed Docker Desktop by default. If you open the application you will likely see something like this:
Figure 2
*Note that there are two tabs, one for containers and one for images. Make sure that you select the right tab when you search!
Figure 3
In Docker Desktop you can either search by name only,
Figure 4
or include the owner. You can then select the tag from the dropdown menu.
Figure 5
The ‘Images’ tab on the left panel will show all the images in your system, so you will be able to see them here.
Figure 6
From this tab we can see some information about the images on disk, and run them, but we can also inspect the images. Clicking on the image will open a window with information on how the image is built, and examine its packages and vulnerabilities.
Figure 7
If we now inspect the docker/getting-started
image, for
example, we can see that it detects some vulnerabilities:
Figure 8
You can even further inspect the vulnerable layers by looking at the command
Figure 9
Let’s run the hello-world
image by either clicking the
‘Run’ button in the ‘Actions’ column, from the Images tab.
Figure 10
A prompt will ask you to confirm ‘Run’ or modify some optional settings.
Figure 11
The optional settings allow you to modify the container’s name, so that you can easily identify it afterwards. Lets add an appropriate name and confirm with the ‘Run’ button.
Figure 12
Figure 13
Figure 14
Figure 15
Figure 16
Indeed, if we look carefully, we will find an ‘Exited (0)’ status under the container name, and a ‘Start’ button near the top-right corner. However, if we click on that button we will see the output duplicated in the logs, and the ‘Exited (0)’ status again.
Figure 17
If we go back to the images tab and run the image again (let’s not bother giving it a name this time), we’ll see that the same thing hapens. We get the ‘Hello from Docker!’, and the container exits.
Figure 18
The nature of most containers is ephimeral. They are meant to execute a process, and when the process is completed, they exit. We can confirm this by clicking on the ‘Containers’ tab on the left. This will exit the container inspection and show us all the containers. Both containers in the list have a status ‘Exited’.
Figure 19
Figure 20
Figure 21
Figure 22
Figure 23
Before trying to do anything in the terminal, let’s look at the container list by clicking on the ‘Containers’ tab on the left. You’ll see the green icon of the container indicating that it is still live, and indication of how long it’s been running for.
Figure 24
Clicking on the container name again will take us back to the ‘Logs’
tab in the container. Lets try and interact with the terminal inside the
container. If you print the working directory with pwd
you’ll get the base directory: /
. You can also list the
contents with ls
, and the docker-entrypoint
files are a dead giveaway that we are inside the container. At this
point this container is very much like a VM. We can modify things, like
for example making a directory with mkdir
, and see it has
been created with ls
again.
Figure 25
Figure 26
Figure 27
Figure 28
Figure 29
Figure 30
Now lets say I want to run the getting-started
image
again. So I go to the ‘Images’ tab, and click run. Now lets go to the
‘Terminal’ tab, and try and find our directory with ls
. The
directory is not there. We’d also installed htop
. so lets
have a go at running it. Not there either.
Figure 31
When we re-ran the image, we created a new
container. The new container is created from the template saved
in the image, and so ‘our’ changes have banished. This becomes very
clear when we go back to the ‘Containers’ tab on the left. We can see
that the first container we created from the
getting-started
image is there, next to the new container
(which is still running, by the way).
Figure 32
We can get the old container running again, although this is rarely something we’d want to do. In Docker Desktop, all we need to do is click on the ‘Start’ button from the ‘Containers’ list. The terminal will appear empty, because it is a new session, but you will even be able to ‘recall’ commands.
Figure 33
The hello-world
image was nice and useful to test docker
was working, but it is now rather useless. If I want to delete it, the
‘Images’ tab on the left has a convenient bin icon to do so. Clicking on
it will prompt you for confirmation, but it will fail.
Figure 34
We’ve only been using Docker for 15 minutes though! You may see how this can become a problem… Particularly so because we were a bit sloppy and did not name the containers. Let’s try and get rid of the containers then. We can conveniently select them all with the tickbox at the top, and an option to ‘Delete’ shows up. Clicking on it will prompt for confirmation, and we can go ahead and accept.
Figure 35
On the up-side, the ‘Images’ tab shows both the
hello-world
and the getting-started
images as
‘Unused’ now. For docker, an image is ‘In use’ as long as at least one
container has been created from it. We have just deleted all the
containers created from either of these images. This tells Docker that
they are no longer being used, and can therefore be safely deleted.
Figure 36
Let’s go ahead and run the only image we have already pulled,
alpine
.
Figure 37
Figure 38
Figure 39
Figure 40
Figure 41
Let’s try something different. There’s a program called
cowsay
that lets you print messages as if a cow was saying
them. Searching for that image shows that there is one by
beatrixxx32
with a reasonable number of downloads.
Figure 42
Figure 43
Figure 44
Figure 45
Figure 46
We do get a cow this time, but it is not saying anything. But it does not know what to say. Going back to the cowsay image search, you may notice that in ‘Usage’ the command line asks for “your message”. We are not using a command though, we just clicked run. Maybe we missed something in the optional settings!
Using the Docker command line
Figure 1
So, what happens when we run a Docker container?
Figure 2
Figure 3
Figure 4
Figure 5
Docker calls these bind mounts and they take the following form: