
Running a random trivia question container
For the subsequent sections of this chapter, we need a container that runs continuously in the background and produces some interesting output. That's why we have chosen an algorithm that produces random trivia questions. The API that produces that free random trivia can be found at http://jservice.io/.
Now the goal is to have a process running inside a container that produces a new random trivia question every five seconds and outputs the question to STDOUT. The following script will do exactly that:
while :
do
wget -qO- http://jservice.io/api/random | jq .[0].question
sleep 5
done
Try it in a Terminal window. Stop the script by pressing Ctrl + C. The output should look similar to this:
"In 2004 Pitt alumna Wangari Maathai became the first woman from this continent to win the Nobel Peace Prize"
"There are 86,400 of these in every day"
"For $5 million in 2013 an L.A. movie house became TCL Chinese Theatre, but we bet many will still call it this, after its founder"
^C
Each response is a different trivia question.
You may need to install jq first on your macOS or Windows computer. jq is a handy tool often used to nicely filter and format JSON output, which increases the readability of it on the screen.
Now, let's run this logic in an alpine container. Since this is not just a simple command, we want to wrap the preceding script in a script file and execute that one. To make things simpler, I have created a Docker image called fundamentalsofdocker/trivia that contains all of the necessary logic, so that we can just use it here. Later on, once we have introduced Docker images, we will analyze this container image further. For the moment, let's just use it as is. Execute the following command to run the container as a background service. In Linux, a background service is also called a daemon:
$ docker container run -d --name trivia fundamentalsofdocker/trivia:ed2
In the preceding expression, we have used two new command-line parameters, -d and --name. Now, -d tells Docker to run the process running in the container as a Linux daemon. The --name parameter, in turn, can be used to give the container an explicit name. In the preceding sample, the name we chose is trivia.
If we don't specify an explicit container name when we run a container, then Docker will automatically assign the container a random but unique name. This name will be composed of the name of a famous scientist and an adjective. Such names could be boring_borg or angry_goldberg. They're quite humorous, our Docker engineers, aren't they?
We are also using the tag ed2 for the container. This tag just tells us that this image has been created for the second edition of this book.
One important takeaway is that the container name has to be unique on the system. Let's make sure that the trivia container is up and running:
$ docker container ls -l
This should give us something like this (shortened for readability):
CONTAINER ID IMAGE ... CREATED STATUS ...
0ff3d7cf7634 fundamentalsofdocker/trivia:ed2 ... 11 seconds ago Up 9 seconds ...
The important part of the preceding output is the STATUS column, which in this case is Up 9 seconds. That is, the container has been up and running for 9 seconds now.
Don't worry if the last Docker command is not yet familiar to you, we will come back to it in the next section.
To complete this section, let's stop and remove the trivia container with the following command:
$ docker rm -f trivia
Now it is time to learn how to list containers running or dangling on our system.