Top tips to manage docker containers from command line

DockerOperating SystemOpen Source

It’s true that the use of docker has skyrocketed in recent times and it will continue to increase in the coming years. Most organizations are now shifting their projects to docker containers if they have not already. Thus, only acquiring basic knowledge regarding creating and maintaining docker containers and images is not enough to keep up with the pace of such a huge technological shift.

Managing a large number of containers and images through a single command line interface (CLI) seems to be a tedious task, but with proper set of management skills and hands-on experience with docker CLI commands, this becomes a piece of cake.

Here, in this article, let’s discuss some of the tools, that will help you manage all your docker containers and images all at a single site very easily and will definitely save you loads of time.

Listing out images and containers

With a number of images and containers running in a single machine, it becomes important to keep track of their latest tags, sizes, IDs and other features frequently which would help you to manage them easily.

The following command helps you to do exactly the same.

sudo docker images

The above command lists all the images along with their latest tags, sizes, IDs and date of creation.

After creating the images, if you want to know the list of containers that currently exists in your system, you can use the following command.

sudo docker ps

The above command displays the list of all running container IDs, their corresponding images, creation dates, status etc.

If you want a list of all containers whether running or not, you can simply add an option to the above command.

sudo docker ps -a

Using aliases to save time

Some of the docker commands can be too tricky to remember because they can be very lengthy, You can create your own aliases and define them into your ~/.bashrc file.


alias dl=’docker ps -l -q’
alias drm=’docker rm’
alias dps=’docker ps’

Deleting containers and images

To remove a particular container, you have the rm option. The syntax for this command is

sudo docker rm YOUR_CONTAINER_ID

To find out the container ID of the container you want to delete, you can use the command which is also discussed above.

sudo docker ps -a

In case, a container is running, you need to stop the container before trying to remove it using the following command.

sudo docker stop YOUR_CONTAINER_ID

To remove a docker image, you first need to remove all the containers of that particular image. After that, you can remove the image using the following command.

sudo docker rmi YOUR_IMAGE_ID

You can get the image ID of a particular image using the following command which is also discussed above.

sudo docker images

Running a container in interactive shell

Suppose you have pulled an ubuntu image from docker hub, using the following command.

sudo docker pull ubuntu

Now, you want to run the ubuntu container in an interactive shell. You can use the following command to achieve this.

sudo docker run -it ubuntu

You can now access the ubuntu image using a separate interactive command shell.

Try removing all the dangling volumes

When you try to delete a container, it’s always likely that the volume associated with the container also gets deleted with it. Such a volume is called dangling volume. To find out a list of all dangling volumes, use the following command.

sudo docker volume ls -f dangling=true

After issuing the above command, you will find a list of all dangling volumes with their driver name and volume name if they exist.

To avoid dangling volumes, delete a container using the -v flag.

sudo docker rm -v YOUR_CONTAINER_ID

Some important commands

Here is a curated list of some of the most important commands that are related to images and docker containers that would come very handy if you handle large numbers of such objects.

Container commands

  • To list all the containers that are currently running in your system, use the command docker container ls

  • To stop a running container forcefully, use docker container kill container_id

  • To get a detailed information of a particular running container, use docker inspect container_id

Image commands

  • To get a list of all local images, use docker image ls

  • To remove all the dangling images, use docker image prune

To conclude, these were a list of top tips which would come very handy when you work with huge numbers of images and containers and would surely help you to efficiently manage them, saving loads of time.

Published on 01-Oct-2020 18:52:45