Familiarizing ourselves with Docker

; Date: Thu Mar 04 2021

Tags: Docker »»»» Docker MAMP

Getting to know Docker by running a few commands.

Installing Docker was easy enough, but there is a whole world to learn about what it does. With Docker, we run containerized applications. There is a command-line interface to Docker with which we can launch containers, and manage our systems. There are several Docker image repositories from which we can discover existing containerized applications. That's just a few high points of what one must learn to become proficient with Docker. The best way to learn a complex system is to start with a simple example to get your feet went, and to build from there.

Now that we have Docker and Docker Compose installed, let's kick the tires. While there is a GUI dashboard application for Docker we can use on macOS or Windows, the primary user interface is at the command line. That's where we must go.

As with so many software tutorials, let's start by saying Hello World:

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:97ce6fa4b6cdc0790cda65fe7290b74cfebd9fa0c9b8c38e979330d547d22ce1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

The docker run command does several things at once. The hello-world portion of the command line specifies the name of the Docker image to run. The first thing the command does is to check whether the image already exists on the local computer, and if not it will download the image. In this case you see Unable to find followed by Pulling from, indicating that docker run did indeed download the hello-world image. Once downloaded, it executed the image which in this case caused the message to be printed.

This particular image is useful for a quick verification that Docker is correctly installed. It also suggests a next step in our exploration:

MacBook-Pro-4:ebooks david$ docker run -it ubuntu bash 
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
d72e567cc804: Pull complete 
0f3630e5ff08: Pull complete 
b6a83d81d1f4: Pull complete 
Digest: sha256:bc2f7250f69267c9c6b66d7b6a81a54d3878bb85f1ebb5f951c896d13e6ba537
Status: Downloaded newer image for ubuntu:latest
root@9c18de0b90a3:/# uname -a
Linux 9c18de0b90a3 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
root@9c18de0b90a3:/# exit
MacBook-Pro-4:ebooks david$ uname -a
Darwin MacBook-Pro-4 17.7.0 Darwin Kernel Version 17.7.0: Mon Aug 31 22:11:23 PDT 2020; root:xnu-4570.71.82.6~1/RELEASE_X86_64 x86_64

In this variation the -it option says to run the image interactively attached to the users terminal. The image name is ubuntu, which packages up a subset of the Ubuntu operating system. The last token, bash, says to execute the Bash command when the Ubuntu container starts. The first uname -a command verifies that indeed we are running in Linux, and then after we exit from that command line the second uname -a indicates that we are on a MacBook Pro running macOS.

Notice that this process is described as first downloading a Docker image, then executing the Container. This refers to two stages of the Docker lifecycle.

  • A Docker image is a bundle containing everything required to build an execution environment. This involves file system images, executable software, configuration files, metadata, and more.
  • A Docker container is what we get when Docker unpacks and executes an image. A container is very similar to a virtual server, but it has a much lighter weight implementation than virtual machine systems like VirtualBox or KVM.

One way to see the difference is with these commands:

$ docker ps
CONTAINER ID   IMAGE    COMMAND  CREATED          STATUS   PORTS              NAMES
$ docker ps -a
CONTAINER ID   IMAGE    COMMAND  CREATED          STATUS   PORTS              NAMES
9c18de0b90a3   ubuntu   "bash"   19 minutes ago   Exited (0) 18 minutes ago   peaceful_meninsky
$ docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
ubuntu            latest    9140108b62dc   11 days ago    72.9MB
...
hello-world       latest    bf756fb1ae65   9 months ago   13.3kB

The first command, docker ps, shows the currently running containers on the local system. In this case there are none. The second command, docker ps -a, shows the containers that executed but have exited. The last, docker images, lists the images which are in the local image cache.

The last shows that we've downloaded both the ubuntu and hello-world images. The second shows that we recently executed the ubuntu image, but it exited a little while ago. The first shows there are no currently executing containers.

The name peaceful_meninsky shown for the ubuntu is the user-friendly name that has been assigned by Docker. The actual identifier is the Container ID column, where the code string shown here is shorted from the actual code string for the container. While peaceful_meninsky is a strange name it's a lot easier to deal with than 9c18de0b90a3.

We can delete the container with this command:

$ docker rm peaceful_meninsky
peaceful_meninsky

After which docker ps -a will show nothing.

To assign a name to a container, use the --name option:

$ docker run -it --name ubuntu ubuntu bash 
root@52aab16afff6:/# 

This again starts an Ubuntu container running Bash in the foreground. To run docker ps we must open another command window and type this:

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS          PORTS      NAMES
52aab16afff6   ubuntu    "bash"    31 seconds ago   Up 30 seconds              ubuntu

This shows that the ubuntu container is currently running, and that we gave it the name ubuntu.

The next thing to try is the command shown in the Docker GUI:

$ docker run -d -p 80:80 docker/getting-started
Unable to find image 'docker/getting-started:latest' locally
latest: Pulling from docker/getting-started
cbdbe7a5bc2a: Already exists 
85434292d1cb: Pull complete 
75fcb1e58684: Pull complete 
2a8fe5451faf: Pull complete 
42ceeab04dd4: Pull complete 
bdd639f50516: Pull complete 
c446f16e1123: Pull complete 
Digest: sha256:79d5eae6e7b1dec2e911923e463240984dad111a620d5628a5b95e036438b2df
Status: Downloaded newer image for docker/getting-started:latest
e01857e87689b0f7c287df52dc0ae6408a9749c46f48b0ca3b17c126c8e3545e

In this case the container does not execute in the foreground, and it does not produce any output for the user to see. This is because it uses the -d option, which is short for --detach, which means to detach the container from the terminal and run it in the background.

The -p option is short for --publish and is how we specify which TCP ports to bind to the container. In this case the container has a service on TCP port 80, which is the standard port for HTTP. We have published the container port 80 to the host machine, our laptop, on port 80. It's possible to use this option to remap the port to any port number. For example, with -p 4080:80 we publish it to port 4080 instead, mapping port 80 to port 4080.

In any case, this means we can visit http://localhost in our browser and see this:

This container is a full website comprising a Docker tutorial. Getting it running simply required downloading and running the container.

With this we start to see the value of Docker. There are tens of thousands of prepackaged applications available through Docker that we can download and execute in the same fashion. For example, do you need a Github clone you run on your own hardware? Both Gitlab and Gitea are available, and Jenkins and other build tools are also available. Installing Gitea and Jenkins can be installed on a spare Mac Mini in a couple hours. Such a small machine can be kept on a shelf in the office to make a local development and build environment.

About the Author(s)

(davidherron.com) David Herron : David Herron is a writer and software engineer focusing on the wise use of technology. He is especially interested in clean energy technologies like solar power, wind power, and electric cars. David worked for nearly 30 years in Silicon Valley on software ranging from electronic mail systems, to video streaming, to the Java programming language, and has published several books on Node.js programming and electric vehicles.