Smart guide to managing Docker container
Publisher: Psychz Networks, June 01,2021Docker technology is an open-source containerization platform allowing you to rapidly develop, test and deploy applications as portable containers that can run virtually anywhere. Docker containers come with all the libraries to provide a self-sufficient environment needed for software deployment and testing. The developer environment can be very chaotic, leading to old or unused or outdated components in the system. With the following guide, we will show you how to organize a Docker environment by removing the images, containers, and volumes.
- Requirement
- Remove Unused Resources
- Remove Docker Images
- Remove Docker Containers
- Remove Docker Volumes
- Remove Docker Networks
- Conclusion
Requirement
- Ubuntu 18 server running Docker
- Terminal/Command prompt
- Root user account or user with Sudo privileges
Removing All Unused Docker Resources
A lot of files, including unused data, are created in a development environment. This can lead to cluttering the disk space. Docker does not remove any unused data by itself, and hence it is a good practice to remove time and unused and unwanted data.
To remove all images, containers, and networks that are not associated with a container.
# docker system prune
This command will remove everything, including dangling images and stopped containers.
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N]
Once you press Y and enter, it should give you the list of deleted containers.
Please note, the above command will only remove dangling Docker images. If you would like to include all unused images, execute the following command:
# docker system prune -a
Output
- all stopped containers
- all networks not used by at least one container
- all images without at least one container associated with them
- all build cache
Are you sure you want to continue? [y/N]
The previous two commands will not touch unused volumes. If you'd like to remove that as well, use the following command:
# docker system prune --volumes
Important note: Prune command is not reversible and hence must be carefully used.
Removing Docker Images
To view all of your Docker images, you can use:
# docker images -a
This will give you a list of all the docker images and their details in the following format.
Output
Using the image id from the output above, you can pass the value to the following command to remove the docker image.
# docker image rm IMAGE_ID
To remove dangling images that are most recent and untagged, we will use the "docker remove all images" command as shown here:
# docker image prune
However, to remove Docker images that are present in existent containers that are also tagged, we can use this:
# docker image prune –a
If you wish to remove all images, for example, that may fall under a specific time frame, use the command:
# docker image prune -a --filter "until=24h"
Note: To forcefully execute any "remove" command at any given time, use the -f or –force flag.
Removing Docker Containers
Similarly to before, to see the list of all Docker containers, you will need to run this command:
# docker container ls -a
If you want to remove a specific container, enter the CONTAINER ID as shown in this example:
# docker container rm CONTAINER_ID
To remove all the containers that stopped in the application, follow this command:
# docker container prune
Take note that this will remove all stopped containers. To view the list of what containers will be deleted using the beforementioned command, use the –filter flag:
# docker container ls -a --filter status=created --filter status=exited
To limit the removal of containers that stopped, for example, according to the time frame, you can use the following command:
# docker container prune --filter "until=24h"
If you want to remove a container once you're done working with it, you start one by adding a –rm flag. Here's an example of how to remove such a container:
# docker run --rm CONTAINER_ID
When you're done, the container will be deleted automatically.
Removing Docker Volumes
Volumes are used for multiple containers, and there will likely be a number of either unused or stopped volume files.
These files are not removed automatically, and neither is there a setting in Docker to do so as that can cause significant loss or damage to data.
First, to get all Docker volume IDs, use the following command:
# docker volume ls
If you want to remove a particular volume, use this command followed by the VOLUME NAME:
# docker volume rm VOLUME_NAME
To remove all unused volumes using a single command, you can use the following:
# docker volume prune
If Docker volumes have labels attached to them, you can also use this:
# docker volume prune --filter "label!=keep"
In this example, the command will only remove those volume files which are not labeled and assigned with the "keep" label.
Removing Docker Networks
Though Docker networks don't take much disk space, they can cause problems if unnecessary files are not cleared from the disk. One problem is that it creates rules for iptables and bridge networks with routing table entries, which can cause some issues in the long run.
For a complete list of NETWORK IDs, use the following command:
# docker network ls
To remove a specific network, you can use:
# docker network rm NETWORK_ID
If you wish to remove all unsued networks, use the following command:
# docker network prune
For a filter based on the time frame, 24 hours in this example, you can enter instead:
# docker network prune --filter "until=24h"
Note: If you get an error when trying to remove a specific network, it means that an existing container uses that particular network and will have to be removed before continuing.
Conclusion
Nowadays, Docker is gaining considerable momentum and is used by many famous companies worldwide. It is one of the best platforms to date that allows more convenient development solutions and allows unlimited testing and experimentation to quickly and efficiently create applications.
In this tutorial, you've learned how to clear unused Docker images, containers, volumes, and networks. If you have any more questions, feel free to leave a comment down below.