Loading...

How to use docker rm

Jonas Scholz
3 min read 21. Dez. 2024

When you're working with Docker, you might find yourself needing to clean up after your containers. That's where the docker rm command comes in handy. It's a simple way to remove one or more containers that you no longer need.

Let's break down how to use docker rm and look at some examples.

Basic Usage

To remove a container, the basic command you'll use is:

docker container rm [OPTIONS] CONTAINER [CONTAINER...]

Or, you can use the shorter alias:

docker rm [OPTIONS] CONTAINER [CONTAINER...]

Here, CONTAINER is the name or ID of the container you want to remove. You can specify multiple containers if you need to remove more than one at a time.

Removing a Container

Imagine you have a container named /redis that you no longer need. To remove it, you would run:

docker rm /redis

This command will remove the /redis container from your system.

If you've set up a link between containers using the --link option on the default bridge network, you can remove this link without deleting the containers. For example, if you have a /webapp container linked to a /redis container, you can remove the link with:

docker rm --link /webapp/redis

This command will break the network communication between /webapp and /redis, but both containers will remain intact.

Force-Removing a Running Container

Sometimes, you might need to remove a container that's still running. To do this, you can use the --force or -f option. For example, if you want to force-remove a running container named redis, you would run:

docker rm -f redis

This command sends a SIGKILL signal to the main process inside the container, stopping it immediately, and then removes the container.

Removing All Stopped Containers

If you want to clean up all your stopped containers, you can use the docker container prune command:

docker container prune

This command will remove all stopped containers. Alternatively, if you want to remove not just stopped containers but also other unused Docker resources like images and networks, you can use:

docker system prune

Another way to remove all stopped containers is by combining docker ps and docker rm. You can list the IDs of all stopped containers with:

docker ps -q --filter status=exited

Then, you can pipe these IDs into docker rm to remove them:

docker ps -q --filter status=exited | docker rm

On Linux, you can also use the xargs utility to achieve the same result:

docker ps -q --filter status=exited | xargs docker rm

Removing a Container and Its Volumes

When you remove a container, any volumes associated with it are not removed by default. If you want to remove the container and its volumes, you can use the --volumes or -v option. For example:

docker rm -v my_container

This command will remove my_container and any anonymous volumes associated with it. Note that named volumes are not removed with this command.

Selectively Removing Volumes

If you want to remove some volumes but not others, you'll need to remove them separately. For example, if you have a container with volumes at /foo and /bar, and you want to keep /foo but remove /bar, you would first remove the container:

docker rm my_container

Then, you would remove the /bar volume separately:

docker volume rm my_container_bar

The same principle applies if you've used --volumes-from to inherit volumes from another container.

By using docker rm with its various options, you can effectively manage and clean up your Docker environment.

Welcome to the container cloud

Sliplane makes it simple to deploy containers in the cloud and scale up as you grow. Try it now and get started in minutes!