Tips to Manage Docker Containers using CLI
If you are managing numerous Docker Containers simultaneously, you might have noticed that this is indeed a very hefty task. In this article, we will learn some top tips that can be very handy for you to manage Docker Containers straight from the Command Line Interface. We will see practical examples of the most commonly used yet very effective and efficient Docker Commands that would surely make your Docker experience seamless.
1. Running a Docker Container in Interactive Shell
After you have pulled out a Docker Ubuntu Image from the official Docker registry, you might want to access the bash of the Ubuntu OS to manipulate the file system or install packages and libraries. You can do so by running the Docker Container in the interactive mode using the -i flag.
sudo docker pull ubuntu
sudo docker run -it ubuntu
2. Remove all the Dangling Volumes
Docker allows you to mount Docker Volumes with Docker Containers so that you can share files and directories among multiple Docker Containers. However, when you delete the Docker Containers, the Docker Volumes associated with it remains there. Such Volumes are called Docker Volumes. To find out a list of all the Dangling Docker Volumes, you can use the following command.
sudo docker volume ls -f dangling=true
To avoid leaving behind Dangling Volumes, while you are deleting Docker Volumes, you can use the -v flag.
sudo docker rm -v <container-id>
Use the following set of commands, to list the running Docker Containers, Stop the particular Container and Remove it using the -v flag to avoid leaving behind Dangling Volumes.
sudo docker container ls
sudo docker stop my-container-01
sudo docker rm -v my-container-01
3. Removing Docker Containers and Images
You can use the rm command to remove Docker Containers. However, before removing a Docker Container, you need to make sure that the Docker Container is not running. To stop and remove Docker Containers, you can use the following set of commands.
sudo docker ps -a
sudo docker stop <container-name>
sudo docker rm <container-name>
The first command displays a list of all the Containers in your system. You can check the status of the Container under the Status column. If it is not exited, you need to stop the Container before removing it.
To remove a Docker Image, first, remove all the Container associated with that Image.
sudo docker rmi <image-id>
4. Using Aliases
Most of the Docker commands are too lengthy to remember. You can simply create and use aliases and mention them in your ~/.bashrc file.
alias dockrm='docker rm'
alias docklist='docker ps -a'
5. Inspecting Docker Containers
You can get the details of a particular Docker Container using the Docker Inspect Command. It gives you each and every detail of the Container such as path, date of creation, status, driver, etc. You will need the Container Name to inspect the Container.
sudo docker container ls
sudo docker container <container-name>