Normally, after developing and storing the docker container in your hub, you’ll want to remove anything that’s not necessary such as containers, images, volumes you’ve declared on your laptop, or at least that’s what happens to me.

Before that, probably you will want to push your image to hub.docker, right?

Log in first:

docker login --username=yourhubusername

Build and Tag your image:

mvn package dockerfile:build && docker images

docker tag image-id yourhubusername/image-name:latest

docker push yourhubusername/image-name

After that, we can run the following commands in order to clean every local images, containers, volumes…

In case everything is a disaster, there is a Docker command to clean up images, containers, volumes and network devices that are not associated with any container.

docker system prune

If we add the flag -a at the end, we’ll delete it too: (1) all containers stopped and (2) all images not in use at the moment

List images:

docker images -a

docker rmi image-id

Hanged images are the layers of images that are not being used in any container, whether it is working or standing. These images can be deleted most of the time. We can see these images with the command:

docker images -f dangling=true

docker images purge

If we want to erase all the images, we can execute:

docker rmi $(docker images -a -q)

Delete Containers

docker ps -a

And to erase them:

docker rm container-id

Stop and delete all Docker containers
We can also stop and delete all containers with a single command:

docker rm $(docker ps -a -q)

Deleting Volumes
To see the volumes we have in the system, we can use the command:

docker volume ls

When we locate the volume, we can erase it with:

docker volume rm volume-id

Delete dangling volumes
Hanged volumes are those that are not connected to any container. Please note that it is possible that there are volumes in this state and that they do not have to be deleted.

To see them we will use:

docker volume ls -f dangling=true

We will locate the ones we want to delete and we will do it with the same command of the previous point:

docker volume rm volume-id

If we want to erase all the dangling volumes, we will execute:

docker volume prune

that is all.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s