Geeks logo

How To Remove Docker Images, Containers, and Volumes

In this step-by-step guide, you will learn how to clean up your system by removing unwanted Docker objects, such as unused images, unused stopped containers, and unused volumes.

By SSD NodesPublished about a year ago 8 min read
Like

by Abdelhadi Dyouri

For a complete guide explaining how to install and use Docker Community Edition (CE) on Ubuntu 22.04, you can check our previous tutorial here:https://vocal.media/geeks/how-to-install-and-use-docker-on-ubuntu-22-04

Prerequisites

Basic knowledge of the Linux command line.

An Ubuntu 22.04 server with a non-root user with sudo privileges. You can get affordable, and powerful Ubuntu servers from our website, and you can check out our How to access your server using SSH guide to learn how to access your server and create a sudo user.

Docker installed on your server. See How To Install and Use Docker on Ubuntu 22.04 to learn how to install Docker and use it to download images and run containers.

Pulling a Few Images from Docker Hub

Docker Hub is a cloud-based service that provides a centralized repository for Docker images. It allows users to store, manage, and share Docker images with other users. It provides a secure and reliable way to share and store Docker images, which can be used to create and deploy applications. Docker Hub also provides a wide range of services, such as private repositories, automated builds, and integration with other services.

To test whether you can access Docker Hub, run the hello-world image:

sudo docker run hello-world

You should receive an output that shows that your installation is working correctly:

Unable to find image 'hello-world:latest' locally

latest: Pulling from library/hello-world

2db29710123e: Pull complete

Digest: sha256:c77be1d3a47d0caf71a82dd893ee61ce01f32fc758031a6ec4cf1389248bb833

Status: Downloaded newer image for hello-world:latest

Hello from Docker!

This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

1. The Docker client contacted the Docker daemon.

2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

(amd64)

3. The Docker daemon created a new container from that image which runs the

executable that produces the output you are currently reading.

4. The Docker daemon streamed that output to the Docker client, which sent it

to your terminal.

To try something more ambitious, you can run an Ubuntu container with:

$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:

https://hub.docker.com/

For more examples and ideas, visit:

https://docs.docker.com/get-started/

To demonstrate how to remove images and containers, we will first download a few images and use them to run containers.

For example, run the following command to download the official Ubuntu image:

sudo docker pull ubuntu

You can also download the official Debian image like so:

sudo docker pull debian

Next, to demonstrate how to remove a actively running container, we will download the official nginx image and run a container using it:

sudo docker pull nginx

Run a Nginx container and call it docker-nginx using the following command:

sudo docker run --name docker-nginx -p 80:80 -d nginx

You should receive the container's ID as an output.

Next, run a container using the Ubuntu image you've downloaded previously with an interactive shell using the docker run and the -it switches:

sudo docker run -it ubuntu

Your prompt should now be a root shell like so:

root@242d22e1d9da:/#

Exit from it using the following command:

exit

With this, the Ubuntu container will have a status of Exited. Next, you'll learn how to list and remove images and containers.

Listing Images and Containers

You can list the images you've downloaded so far using the following command:

sudo docker images

You should see a list of the images you've downloaded so far:

REPOSITORY TAG IMAGE ID CREATED SIZE

nginx latest 3964ce7b8458 5 days ago 142MB

ubuntu latest 6b7dfa7e8fdb 10 days ago 77.8MB

debian latest 291bf168077c 13 days ago 124MB

hello-world latest feb5d9fea6a5 15 months ago 13.3kB

To list the containers you have on your system, use the following command:

sudo docker container ls -a

You should receive a table that shows a list of containers and information on each one of them, similar to the following:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

fe965bef3738 ubuntu "bash" 27 hours ago Exited (0) 27 hours ago unruffled_wright

d509c583954b nginx "/docker-entrypoint.…" 27 hours ago Up 27 hours 0.0.0.0:80->80/tcp, :::80->80/tcp docker-nginx

434eb7e61fab ubuntu "bash" 27 hours ago Exited (0) 27 hours ago jolly_jones

c199b4904b1f nginx "/docker-entrypoint.…" 27 hours ago Exited (0) 27 hours ago festive_newton

You will need the container ID to remove a container, so take note of it.

Removing a Docker Container

To remove a docker container, use the docker container rm command and pass it the ID of the container you wish to remove, or multiple container IDs if you wish to remove multiple ones. For example, to delete the first container in the previous list, run the following command:

sudo docker container rm fe965bef3738

You should receive the container ID as an output. Check that the container is properly removed by listing your containers:

sudo docker container ls -a

The Ubuntu container with the ID fe965bef3738 should no longer be in the list.

You can remove multiple containers at once by passing multiple container IDs to the docker container rm command like so:

docker container rm dbc6a1c52dbf 242d22e1d9da

If you attempt to remove a running container, such as the docker-nginx container, you will receive an error message similar to the following:

Error response from daemon: You cannot remove a running container d509c583954b31eb02bd26a9c8f2cf832d5213020362f89d1999c097ef64a98d. Stop the container before attempting removal or force remove

As you can see in the error message, you need to stop any running containers before removing them.

To stop a container, use the docker container stop command:

sudo docker container stop d509c583954b

Once stopped, you can remove it using docker container rm.

To remove all stopped containers at once, use the docker container prune command:

sudo docker container prune

This should output a warning, and a prompt for confirmation, once you confirm, a list of the removed containers will be displayed with the total cleared up space:

WARNING! This will remove all stopped containers.

Are you sure you want to continue? [y/N] y

Deleted Containers:

434eb7e61fabc5707717bc12433615b4bd0b42766f5a51c7d6f506c6fb1f9bbf

c199b4904b1f20509170f56403141db9c3996aa7b6eeb3155837d10a5d8d0b08

dbc6a1c52dbfe7e92bc920dd2565383b30e827782943487b9e00f41435916f57

a7c5f7b98198361524d6f5904ee8b299c7901968327dee87ca76dcc73f79fdf6

242d22e1d9da87f49da95b047eecc6d0f41bb6ba8cb6cec6e5e89835323f5608

4aa674aadcdd96d98ae783c8d85625486ff98e44f9a4b9c99fb6c24de1e4b6e2

Total reclaimed space: 70.15MB

Listing and Removing Docker Images

As previously demonstrated, to list all the downloaded Docker images, you can use the following command:

sudo docker images

This should display a list of the images you've downloaded so far in a table similar to the following:

REPOSITORY TAG IMAGE ID CREATED SIZE

nginx latest 3964ce7b8458 8 days ago 142MB

ubuntu latest 6b7dfa7e8fdb 13 days ago 77.8MB

debian latest 291bf168077c 2 weeks ago 124MB

hello-world latest feb5d9fea6a5 15 months ago 13.3kB

To remove a Docker image, pass its ID to the docker image rm command like so:

sudo docker image rm 6b7dfa7e8fdb

You can also remove multiple images at once by passing multiple IDs to the docker image rm like so:

sudo docker image rm 291bf168077c 3964ce7b8458

To remove all dangling and unused images, use the docker image prune command with the -a flag to remove both untagged and unused images:

sudo docker image prune -a

Listing and Removing Docker Volumes

In a similar way to containers and images, you can use the docker volume ls to list Docker volumes in your system:

sudo docker volume ls

If you have Docker volumes on your system, you should receive a list of volumes like so:

DRIVER VOLUME NAME

local basic

local faa5ac67cf1325d03b01c72f56749d3f1a8010fd6db24d8683b490c8819d775e

To remove a volume, pass its VOLUME NAME to the docker volume rm command like so:

sudo docker volume rm faa5ac67cf1325d03b01c72f56749d3f1a8010fd6db24d8683b490c8819d775e

If a container is actively using the volume you wish to remove, you'll receive an error message similar to the following:

Error response from daemon: remove faa5ac67cf1325d03b01c72f56749d3f1a8010fd6db24d8683b490c8819d775e: volume is in use - [da5fdeaad32aa66b6f7f2a8da8b21b5477dd217fc46340d711476f96707ede58]

To fix this, make sure to remove the container that uses the volume you wish to remove first.

To remove all unused volumes, use the docker volume prune command like so:

sudo docker volume prune

Removing Unused Docker Containers, Images, and Networks

To remove all unused Docker objects at once, such as all stopped containers, dangling images, unused networks, and all build cache, use the following command:

docker system prune -a

This should display the following warning:

WARNING! This will remove:

- all stopped containers

- all networks not used by at least one container

- all images without at least one container associated to them

- all build cache

Are you sure you want to continue? [y/N]

This lists all the Docker objects that will be removed, confirm the process by typing y then ENTER.

Note that the previous command does not remove unused volumes to prevent accidental data loss. To also remove unused volumes, use the following command:

docker system prune --volumes

Conclusion

With this, you can now clean up your system by removing unwanted Docker objects, such as unused images, unused stopped containers, and unused volumes. Check out the Docker guides for more information on Docker.

___________________________________________________

#Docker #dockercontainer #offers #promotion #sale #discount #onlineshopping #shopping #discount #vps #virtualprivateserver #serverless #server #ram #hosting #ssd #ssdnodes #hosting #ssd #nvme #vpshosting

Check our portal ssdnodes.com for the latest promotions & offers!

Follow us on Twitter, Facebook, and LinkedIn.

Don’t forget to subscribe to our Blog Serverwise, for the most-updated tech content and interesting step-by-step tutorials!

Strasmore, Inc., 2522 Chambers Road Suite 100, Tustin, CA 92780

how to
Like

About the Creator

SSD Nodes

Serverwise: the SSD Nodes blog!

Provides the most up-to-date articles & tutorials covering VPS cloud computing, hosting & the latest in tech news!

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.