You've installed Docker and have even run your first container. Here's how to manage those containers and images through the command line.
Docker adoption has skyrocketed. In 2015 alone, Docker adoption rose 30%. That translates to the likelihood of your company employing Docker in the near future significantly higher. What does that mean to you, as an administrator? Simple...you need to understand the technology. I've covered the installation of Docker as well as the installation of a containerized version of Wordpress. But what about managing your Docker containers and images? How do you tackle that particular task? One of the more reliable methods is via command line. Thanks to the Docker developers, you have everything you need, to help manage your containers, built in.
Let's take a look at some of the tools you have at the ready, to help you manage your Docker containers.
Show me your images and containers
With Docker, you will have images on your machine that you can then run as containers. How do you find out what images exist on a machine? You issue the command:
sudo docker images
The above command will list out all of your images as well as their tag, ID, created on, and size information (Figure A).
What if you've already created containers from these images? How do you know which containers exist? You can issue the command:
sudo docker ps
The above command will only display the currently running containers (Figure B).
If you have no running containers and want to see all existing containers on your server (Figure C), you can issue the same command with an added option:
sudo docker ps -a
Deleting containers and images
To remove containers, Docker provides the rm option. This can be a bit tricky. The syntax of the command is:
sudo docker rm [OPTIONS] CONTAINER [CONTAINER...]
Say you've already listed your containers and you've found the container you want to delete. Would you just issue the command docker rm wordpress and be done with it? No. That command would return "No such container." Instead, you have to delete containers by their Container ID. Glance back at Figure C, where you'll see each container's ID number in the far left column. Find the ID associated with the container you want to remove and then issue the command:
sudo docker rm CONTAINER_ID
Where CONTAINER_ID is the actual ID of the container to be removed.
If the container is running (you'll get a warning that the container cannot be removed in this case), you must first stop the container. This is done with the command:
sudo docker stop CONTAINER_ID
Where CONTAINER_ID is the name of the ID for the container.
Once you've stopped the container, you can delete it.
Docker also has the ability to remove all containers from the machine. Before you do that, you must first stop all containers. The commands to achieve these tasks are:
sudo docker stop $(docker ps -a -q) sudo docker rm $(docker ps -a -q)
Once you've deleted the containers created from your local images, you can then delete the images themselves. This is done with the docker rmi command, like so:
sudo docker rmi IMAGE_ID
Where IMAGE_ID is the idea of the image.
Remember, you get the IMAGE ID from the docker images command. You can also delete all Docker images with the command:
sudo docker rmi $(docker images -a -q)
Removing dangling volumes
A volume is a specially-designated directory, within one or more containers, that bypasses the standard filesystem. When you delete a container, the associated volume isn't always deleted. These are called dangling volumes.
To see if you have any dangling volumes, issue the command:
sudo docker volume ls -f dangling=true
If any dangling volumes exist, they will be listed by driver and volume name (Figure D).
To remove those dangling volumes, issue the command:
sudo docker volume rm $(docker volume ls -f dangling=true -q)
You now know how to list and delete images, containers, and dangling volumes. This is, of course, only a tiny aspect of Docker. But in combination with the ability to install Docker and run images, you are getting close to having a solid understanding as to how this container technology works. To find out more, one great place to start is the man page. Issue the command man docker and read through the information presented in the manual. You can also check out the official Docker documentation for significantly more information.
- Docker: The smart person's guide (TechRepublic)
- How to install WordPress with Docker (TechRepublic)
- How to install Docker on Fedora 25 (TechRepublic)
- Containers: The smart person's guide (TechRepublic)
- Docker launches Enterprise Edition, courts broader corporate adoption (ZDNet)