Docker Containers
22%
Removing and Pruning Containers
To remove one or more containers, you can use the docker rm command.
docker rm [OPTIONS] CONTAINER [CONTAINER...]
To see this in action, run and remove a container:
# Run a container in detached mode
docker run -d --name my_container nginx
# Get the container ID
container_id=$(docker ps -a --filter "name=my_container" --format "{{.ID}}")
# Stop the container first, then remove it
docker stop $container_id
docker rm $container_id
# or use the name
# docker stop my_container
# docker rm my_container
If the container is stopped, it will be removed. However, you cannot remove a running container. To force the removal of a running container, you should add the -f or --force option. Here are the commands to run if you want to test this:
# Run a container in detached mode
docker run -d --name my_container nginx
# Get the container ID
container_id=$(docker ps -a --filter "name=my_container" --format "{{.ID}}")
docker rm -f $container_id
# or docker rm -f my_container
Painless Docker - 2nd Edition
A Comprehensive Guide to Mastering Docker and its EcosystemEnroll now to unlock all content and receive all future updates for free.
Unlock now  $31.99$25.59Learn More
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
