Book Image

Mastering Docker

By : Scott Gallagher
Book Image

Mastering Docker

By: Scott Gallagher

Overview of this book

<p><span id="description" class="sugar_field">Docker has been a game-changer when it comes to virtualization – it has now grown to become a key driver of innovation beyond system administration, having an impact on the world of web development and more. But how can you make sure you’re keeping up with the innovations that it’s driving? How can you be sure you’re using it to its full potential? Mastering Docker shows you how – it not only demonstrates how to use Docker more effectively, it also helps you rethink and reimagine what’s possible with Docker.</span></p> <p><span class="sugar_field"><span id="description" class="sugar_field"> Covering best practices to make sure you’re confident with and the basics, such as building, managing and storing containers, before diving deeper into Docker security, you’ll find everything you need to help you extend and integrate Docker in new and innovative ways. You’ll learn how to take greater control over your containers using some of Docker’s most sophisticated and useful tools, such as Docker compose and Docker swarm, before bringing together everything you already know and have learned to put your containers into production and monitor them for safety and performance.</span></span></p> <p><span class="sugar_field"><span class="sugar_field"><span id="description" class="sugar_field"> Beyond this, you’ll also explore even more advanced strategies, as you learn how to extend and integrate Docker with cloud platforms such as Heroku and OpenStack, and how tools such as Kubernetes can improve the way you manage large-scale container orchestration. With further guidance on how you can use configuration management tools such as Puppet, Chef and PowerShell, by the end of the book you’ll have a broad and detailed sense of exactly what’s possible with Docker – and how seamlessly it fits with a range of other platforms and tools.</span></span></span></p>
Table of Contents (20 chapters)
Mastering Docker
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Stopping containers


Now, let's take a look at how we can stop these containers. For various reasons, we would want to do this. There are a few commands we could use; they are docker kill, docker stop, docker pause, and docker unpause. Let's cover them briefly as they are fairly straightforward. First, let's look at the difference between docker kill and docker stop. The docker kill command will do just that—kill the container immediately. For a graceful shutdown of the container, you would want to use the docker stop command. Mostly, when you are testing, you will be using docker kill. When you're in your production environments, you will want to use docker stop to ensure you don't corrupt any data you might have in the Docker volumes. The commands are used exactly like the docker logs command, where you can use the container ID, the random name given to the container, or the one you might specify with the --name= switch.

Now, let's take a dive into how we can execute some commands, view information on our running containers, and manipulate them in a small sense. We will cover more about container manipulation in the later chapters as well. The first thing we want to take a look at, which will make things a little easier with the upcoming commands, is the docker rename command. With the docker rename command, we can change the name that has been randomly generated for the container. When we performed the docker run command, a random name was assigned to our container; most times, these names are fine. But if you are looking for an easy way to manage the containers, a name can be sometimes easier to remember. For this, you can use the docker rename command as follows:

$ docker rename <current_container_name> <new_container_name>

Now that we have an easily recognizable and rememberable name, let's take a peek inside our containers with the docker stats and docker top commands, taking them in order:

$ docker stats <container_name>

CONTAINER           CPU %               MEM USAGE/LIMIT     MEM %               NET I/O
web1                        0.00%               1.016 MB/2.099 GB   0.05%                  0 B/0 B

The other command docker top provides a list of all running processes inside the container. Again, we can use the name of the container to pull the information:

$ docker top <container_name>

We will receive an output similar to the following one based on what processes are running inside the container:

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                8057                1380                0                   13:02               pts/0               00:00:00            /bin/bash

We can see who is running the process (in this case, the root user), the command being run (in this case, /bin/bash), as well as the other information that might be useful.

Lastly, let's cover how we can remove the containers. The same way we looked at removing images earlier with the docker rmi command, we can use the docker rm command to remove unwanted containers. This is useful if you want to reuse a name you provided to a container:

$ docker rm <container_name>