Whoami
"Whoami" is the name of a simple image that is great for learning Docker. If you're wondering what an image is, that will be explained below.
Whoami is like the “Hello world!” of Docker. It is a minimal, but realistic example of a service someone might run with Docker. We'll use it in this section to show the basic stages of the lifecycle of a container, which will be exactly the same principles involved in running any application - including our Coti node.
On a high level, the steps covered in this page are:
Running a Container
First, I'll introduce the command to use, show you the output, and then explain what just happened. To run a whoami container, use
What does this mean?
docker
refers to Docker's commandrun
tells the docker cli that you want to run a container-p 80:80
tells Docker to map port 80 from the outside of the container, to port 80 on the inside of the container. Ports are closed by default on containers (this is a security feature), which is why we need to tell it which port to open.containous/whoami
refers to the "whoami" Docker image. You can find it on Dockerhub. "containous" is the organization that created the image, and "whoami" is the name of the image. Dockerhub is an example of an image registry. You can think of an image registry as like a library for container images. Dockerhub is not the only image registry, although it is generally the most popular one.
When I ran the command above, this was the output:
Here's a breakdown, line by line, of what happened:
Line 1: Docker checked the local cache for the image, and informs us that it couldn't find the image.
Line 2: Since Docker couldn't find the image locally, it goes to Dockerhub and pulls (downloads) the image.
Lines 3-5: Docker images are made in layers, so each line corresponds to one layer pulled by Docker.
Line 6: This shows a checksum of the download. Checksums are a security feature that allows you to check whether downloaded files have been tampered with.
Line 7: This is a status report that tells us that the download is complete.
Line 8: This is a log from the "whoami" application itself, telling us that it has started, and it is running on port 80.
A lot happened there. The important parts are that Docker downloaded the image we requested, and ran it. It is possible to download and run the image in separate steps, but Docker typically isn't used that way.
Once the image is downloaded, it is saved on the computer, so that it doesn't need to be re-downloaded every time we run the application. The command above will work regardless of whether the image has already been downloaded or not.
Checking a Container is Up
In the last line of the output above, Docker says something is running on port 80? Let's test that out. In a new terminal tab/window, run
The output I received was:
If you received similar output, congratulations! That is a successful response from whoami, and you successfully got a response from your first Docker container 🎉
The Hostname
is the ID of the container that we created. The other information mostly reflects back the HTTP headers that were sent by curl.
I suggest you also try visiting http://localhost:80
in a browser, and you will get back even more information (since the browser sends more headers).
That's pretty cool. We launched an application with just one command, and without writing any code.
Checking Running Containers with Docker
We can also check whether a container is running with the Docker cli. To do this, use
You can think of docker ps
as like the ls
command, in that it lists the running containers.
The output I received was
Notice the container ID matches the Hostname presented to us by Whoami. The command also gives some basic information about the running container (e.g. the port mapping, how long the container has been running).
Stopping a Container
Ending a container's lifecycle is just as important as starting it. Without stopping our containers, we would eventually have huge numbers of containers using up our resources.
There are two ways to stop Docker containers. Since we ran whoami
interactively, we can stop the container simply by entering Ctrl+c
(cancel) in our terminal.
A good way of checking that the container has stopped, is by running docker ps
. The output no longer shows our whoami
container.
You can also check that the container has stopped by visiting http://localhost:80
again.
Although the container is stopped, that does not mean it has been fully deleted. A -a
option (short for "all") can be added to the docker ps
command to ask Docker to show all containers. So, to see our stopped container, use
which shows an output of
Removing a Container
In most cases, stopping a container is enough, but to understand all the steps, we'll demonstrate removing a container too.
Copy the CONTAINER ID from the previous step. Then to remove the container, we run
When it is successful, docker rm
returns the ID of the removed container. If you ever want to remove more than one container at once, you can simply add many container IDs, separated by spaces.
Running docker ps -a
again shows that the container has been removed! Great.
Summary
Although the application we ran, whoami, was very simple, we introduced some key concepts that are part of any application's lifecycle, namely:
Running a container
Checking it is up
Stopping a container
Removing a container
These are the most important concepts to understand when using Docker. If you managed to follow these steps, you are about 80% of the way to becoming a fully fledged Docker user! Awesome.
Last updated