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>
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Using CLI to Manage Docker Volumes
- Docker - Search Image in Dockerhub through CLI
- Copying Files to and from Docker Containers
- Running Docker Containers as Non-Root User
- Launching an EC2 instance using AWS CLI
- How to Run a Python Script using Docker?
- Docker - Using Image Tags
- How to Install and Configure Docker in Ubuntu?
- Setup Web Server Over Docker Container in Linux
- How to create your own Docker Customised Image ?
- Docker compose tool to run multi container applications
- Introduction to Docker
- How to Build a Web Server Docker File?
- How to Map Ports in Docker?
- Running GUI Applications on Docker in Linux
- Difference between RUN vs CMD vs ENTRYPOINT Docker Commands
- Creating a Network in Docker and Connecting a Container to That Network
- Docker - USER Instruction
- Docker - HEALTHCHECK Instruction
- Docker - WORKDIR Instruction
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.