Docker is an open-source containerization platform that allows you to quickly build, test, and deploy applications as portable containers that can run virtually anywhere.

When working with Docker, you can quickly accumulate a large number of unused objects that consume significant disk space and clutter the output produced by the Docker commands. Docker doesn’t remove unused objects such as containers, images, volumes, and networks unless you explicitly tell it to do so.

This guide serves as a “cheat sheet” to help Docker users keep their system organized, and to free disk space by removing unused Docker containers, images, volumes, and networks.

Removing All Unused Objects

The docker system prune command removes all stopped containers, dangling images, and unused networks:

docker system prune

This command will prompt you to confirm the operation:

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N]

Use the -f or --force option to bypass the prompt.

By default, the command doesn’t remove unused volumes to prevent losing important data. If you want to remove all unused volumes, pass the --volumes option:

docker system prune --volumes
WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all volumes not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

Removing Docker Containers

Docker containers are not automatically removed when you stop them unless you start the container using the --rm flag.

Removing one or more containers

To remove one or more Docker containers, use the docker container rm command, followed by the IDs of the containers you want to remove.

You can get a list of all containers bu invoking the docker container ls command with the -a option:

docker container ls -a

The output should look something like this:

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                      PORTS               NAMES
cc3f2ff51cab        centos                  "/bin/bash"              2 months ago        Created                                         competent_nightingale
cd20b396a061        solita/ubuntu-systemd   "/bin/bash -c 'exec …"   2 months ago        Exited (137) 2 months ago                       systemd
fb62432cf3c1        ubuntu                  "/bin/bash"              3 months ago        Exited (130) 3 months ago                       jolly_mirzakhani

Once you know the CONTAINER ID of the containers you want to delete, pass it to the docker container rm command. For example, to remove the first two containers listed in the output above you would run:

docker container rm cc3f2ff51cab cd20b396a061

If you get an error message similar to the one shown below, it means that the container is running. You’ll need to stop the container before removing it.

Error response from daemon: You cannot remove a running container fc983ebf47

#docker #docker containers #neural networks

How To Remove Docker Containers, Images, Volumes, and Networks
1.45 GEEK