Introducing Containers


The Docker Hub


Figure 1

Dockerhub_landing

Figure 2

Dockerhub_search

Figure 3

Dockerhub_python

Figure 4

Dockerhub_python

Figure 5

rocker/tidyverse:latest

Figure 6

mathworks/matlab

Figure 7

continuumio/anaconda3

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: Docker Desktop being opened for the first time.


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! Search window.


Figure 3

In Docker Desktop you can either search by name only, Search by name.


Figure 4

or include the owner. You can then select the tag from the dropdown menu. Search with owner and select a tag.


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. Images list, hello-world, getting-started and alpine.


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. Inspecting image hello-world.


Figure 7

If we now inspect the docker/getting-started image, for example, we can see that it detects some vulnerabilities: Inspecting image getting-started.


Figure 8

You can even further inspect the vulnerable layers by looking at the command Inspecting image command in getting-started.


Figure 9

Let’s run the hello-world image by either clicking the ‘Run’ button in the ‘Actions’ column, from the Images tab. Run button from Images tab.


Figure 10

A prompt will ask you to confirm ‘Run’ or modify some optional settings. Run confirmation prompt.


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. Run optional settings.


Figure 12

Logs tab in container from hello-world image.

Figure 13

Inspect tab in container from hello-world image.

Figure 14

Terminal tab in container from hello-world image.

Figure 15

Stats tab in container from hello-world image.

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. Clickling Start on the already run hello-world container.


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. Running hello-world image for a second time.


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’. Containers list.


Figure 19

Logs tab in container from getting-started image.

Figure 20

Inspect tab in container from getting-started image.

Figure 21

Terminal tab in container from getting-started image.

Figure 22

Stats tab in container from getting-started image.

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. Containers list, getting-started still running.


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. Terminal, mkdir and ls inside getting-started container.


Figure 25

Terminal, installing htop inside getting-started container.

Figure 26

Terminal, running htop inside getting-started container.

Figure 27

Terminal tab in container from stopped getting-started image.

Figure 28

Stats tab in container from stopped getting-started image.

Figure 29

Container list after stopping getting-started image.

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. Terminal in fresh getting-started image.


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). Containers after new run of getting-started image.


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. Reviving container getting-started.


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. Failing to delete image.


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. Deleting containers.


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. Successfully deleting images.


Figure 36

Let’s go ahead and run the only image we have already pulled, alpine. Logs tab in container from alpine image.


Figure 37

Logs tab in container from alpine image.

Figure 38

Inspect tab in container from alpine image.

Figure 39

Terminal tab in container from alpine image.

Figure 40

Stats tab in container from alpine image.

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. Search of cowsay image.


Figure 42

Logs tab in container from cowsay image.

Figure 43

Inspect tab in container from cowsay image.

Figure 44

Terminal tab in container from cowsay image.

Figure 45

Stats tab in container from cowsay image.

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! Optional settings for cowsay.


Using the Docker command line


Figure 1

So, what happens when we run a Docker container? A flowchart showing the lifecycle of a Docker container.


Figure 2

A flowchart showing the lifecycle of a Docker container. with an example

Figure 3

A flowchart showing the lifecycle of a Docker container. with an example

Figure 4

A diagram showing the syntactic structure of a Docker command

Figure 5

Docker calls these bind mounts and they take the following form: A breakdown of the command for mounting a file into a container.


Creating Your Own Container Images


Creating More Complex Container Images