Reader small image

You're reading from  Learn Docker - Fundamentals of Docker 19.x - Second Edition

Product typeBook
Published inMar 2020
PublisherPackt
ISBN-139781838827472
Edition2nd Edition
Tools
Right arrow
Author (1)
Dr. Gabriel N. Schenker
Dr. Gabriel N. Schenker
author image
Dr. Gabriel N. Schenker

Dr. Gabriel N. Schenker has more than 25 years of experience as an independent consultant, architect, leader, trainer, mentor, and developer. Currently, Gabriel works as Lead Solution Architect at Techgroup Switzerland. Prior to that, Gabriel worked as Lead Curriculum Developer at Docker and at Confluent. Gabriel has a Ph.D. in Physics, and he is a Docker Captain, a Certified Docker Associate, a Certified Kafka Developer and Operator, and an ASP Insider. When not working, Gabriel enjoys time with his wonderful wife Veronicah and his children.
Read more about Dr. Gabriel N. Schenker

Right arrow

Mastering Containers

In the previous chapter, you learned how to optimally prepare your working environment for the productive and frictionless use of Docker. In this chapter, we are going to get our hands dirty and learn everything that is important to know when working with containers. Here are the topics we're going to cover in this chapter:

  • Running the first container
  • Starting, stopping, and removing containers
  • Inspecting containers
  • Exec into a running container
  • Attaching to a running container
  • Retrieving container logs
  • Anatomy of containers

After finishing this chapter, you will be able to do the following things:

  • Run, stop, and delete a container based on an existing image, such as Nginx, BusyBox, or Alpine.
  • List all containers on the system.
  • Inspect the metadata of a running or stopped container.
  • Retrieve the logs produced by an application running inside a container...

Technical requirements

For this chapter, you should have installed Docker for Desktop on your macOS or Windows PC. If you are on an older version of Windows or are using Windows 10 Home Edition, then you should have Docker Toolbox installed and ready to use. On macOS, use the Terminal application, and on Windows, a PowerShell or Bash console, to try out the commands you will be learning.

Running the first container

Before we start, we want to make sure that Docker is installed correctly on your system and ready to accept your commands. Open a new Terminal window and type in the following command:

$ docker version
If you are using Docker Toolbox then use the Docker Quickstart Terminal that has been installed with the Toolbox, instead of the Terminal on macOS or Powershell on Windows.

If everything works correctly, you should see the version of Docker client and server installed on your laptop output in the Terminal. At the time of writing, it looks like this (shortened for readability):

Client: Docker Engine - Community
Version: 19.03.0-beta3
API version: 1.40
Go version: go1.12.4
Git commit: c55e026
Built: Thu Apr 25 19:05:38 2019
OS/Arch: darwin/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.0-beta3
API version: 1...

Starting, stopping, and removing containers

You have successfully run a container in the previous section. Now, we want to investigate in detail what exactly happened and why. Let's look again at the command we used:

$ docker container run alpine echo "Hello World" 

This command contains multiple parts. First and foremost, we have the word docker. This is the name of the Docker Command-Line Interface (CLI) tool, which we are using to interact with the Docker engine that is responsible to run containers. Next, we have the word container, which indicates the context we are working with. As we want to run a container, our context is the word container. Next is the actual command we want to execute in the given context, which is run.

Let me recap—so far, we have docker container run, which...

Inspecting containers

Containers are runtime instances of an image and have a lot of associated data that characterizes their behavior. To get more information about a specific container, we can use the inspect command. As usual, we have to provide either the container ID or name to identify the container of which we want to obtain the data. So, let's inspect our sample container:

$ docker container inspect trivia 

The response is a big JSON object full of details. It looks similar to this:

[
{
"Id": "48630a3bf188...",
...
"State": {
"Status": "running",
"Running": true,
...
},
"Image": "sha256:bbc92c8f014d605...",
...
"Mounts": [],
"Config": {
"Hostname...

Exec into a running container

Sometimes, we want to run another process inside an already-running container. A typical reason could be to try to debug a misbehaving container. How can we do this? First, we need to know either the ID or the name of the container, and then we can define which process we want to run and how we want it to run. Once again, we use our currently running trivia container and we run a shell interactively inside it with the following command:

$ docker container exec -i -t trivia /bin/sh

The -i flag signifies that we want to run the additional process interactively, and -t tells Docker that we want it to provide us with a TTY (a Terminal emulator) for the command. Finally, the process we run is /bin/sh.

If we execute the preceding command in our Terminal, then we will be presented with a new prompt, ...

Attaching to a running container

We can use the attach command to attach our Terminal's standard input, output, and error (or any combination of the three) to a running container using the ID or name of the container. Let's do this for our trivia container:

$ docker container attach trivia

In this case, we will see every five seconds or so a new quote appearing in the output.

To quit the container without stopping or killing it, we can press the key combination Ctrl P+ Ctrl Q. This detaches us from the container while leaving it running in the background. On the other hand, if we want to detach and stop the container at the same time, we can just press Ctrl + C.

Let's run another container, this time an Nginx web server:

$ docker run -d --name nginx -p 8080:80 nginx:alpine

Here, we run the Alpine version...

Retrieving container logs

It is a best practice for any good application to generate some logging information that developers and operators alike can use to find out what the application is doing at a given time, and whether there are any problems to help to pinpoint the root cause of the issue.

When running inside a container, the application should preferably output the log items to STDOUT and STDERR and not into a file. If the logging output is directed to STDOUT and STDERR, then Docker can collect this information and keep it ready for consumption by a user or any other external system:

  1. To access the logs of a given container, we can use the docker container logs command. If, for example, we want to retrieve the logs of our trivia container, we can use the following expression:
$ docker container logs trivia

This will retrieve the whole log...

Anatomy of containers

Many people wrongly compare containers to VMs. However, this is a questionable comparison. Containers are not just lightweight VMs. OK then, what is the correct description of a container?

Containers are specially encapsulated and secured processes running on the host system. Containers leverage a lot of features and primitives available in the Linux OS. The most important ones are namespaces and cgroups. All processes running in containers only share the same Linux kernel of the underlying host operating system. This is fundamentally different compared with VMs, as each VM contains its own full-blown operating system.

The startup times of a typical container can be measured in milliseconds, while a VM normally needs several seconds to minutes to start up. VMs are meant to be long-living. It is a primary goal...

Summary

In this chapter, you learned how to work with containers that are based on existing images. We showed how to run, stop, start, and remove a container. Then, we inspected the metadata of a container, extracted the logs of it, and learned how to run an arbitrary process in an already-running container. Last but not least, we dug a bit deeper and investigated how containers work and what features of the underlying Linux operating system they leverage.

In the next chapter, you're going to learn what container images are and how we can build and share our own custom images. We'll also be discussing the best practices commonly used when building custom images, such as minimizing their size and leveraging the image cache. Stay tuned!

Questions

To assess your learning progress, please answer the following questions:

  1. What are the states of a container?
  2. Which command helps us to find out what is currently running on our Docker host?
  3. Which command is used to list the IDs of all containers?

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Docker - Fundamentals of Docker 19.x - Second Edition
Published in: Mar 2020Publisher: PacktISBN-13: 9781838827472
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dr. Gabriel N. Schenker

Dr. Gabriel N. Schenker has more than 25 years of experience as an independent consultant, architect, leader, trainer, mentor, and developer. Currently, Gabriel works as Lead Solution Architect at Techgroup Switzerland. Prior to that, Gabriel worked as Lead Curriculum Developer at Docker and at Confluent. Gabriel has a Ph.D. in Physics, and he is a Docker Captain, a Certified Docker Associate, a Certified Kafka Developer and Operator, and an ASP Insider. When not working, Gabriel enjoys time with his wonderful wife Veronicah and his children.
Read more about Dr. Gabriel N. Schenker