Docker prevents removing containers that are still running to avoid data loss. Stop the container first with 'docker stop' or use the '-f' flag to force removal.
This error occurs when you attempt to remove a Docker container using `docker rm` while the container is still running. Docker's daemon explicitly blocks this operation as a safety measure to prevent accidental data loss or service interruption. The full error message reads: "Error response from daemon: You cannot remove a running container. Stop the container before attempting removal or force remove." This is Docker's way of ensuring you consciously decide to terminate an active process before deleting its container. Unlike stopped containers which are essentially just filesystem layers waiting to be cleaned up, running containers have active processes, network connections, and potentially uncommitted state. Removing them without stopping first could corrupt data or leave dependent services in an inconsistent state.
The recommended approach is to stop the container gracefully before removing it:
# Stop the container (sends SIGTERM, then SIGKILL after timeout)
docker stop <container_name_or_id>
# Now remove the stopped container
docker rm <container_name_or_id>You can combine both commands:
docker stop <container_name_or_id> && docker rm <container_name_or_id>This gives the container time to shut down gracefully, allowing processes to clean up and save state.
If you don't need graceful shutdown, use the -f (force) flag:
docker rm -f <container_name_or_id>This sends SIGKILL to the main process immediately and removes the container. The container won't have a chance to clean up, so use this option when:
- The container is stuck and won't respond to stop
- You don't care about graceful shutdown
- You're cleaning up test/development containers
Note: Force removal is equivalent to running docker kill followed by docker rm.
To clean up multiple stopped containers at once:
# Remove all stopped containers
docker container prune
# Or remove all stopped containers (older syntax)
docker rm $(docker ps -aq -f status=exited)To stop and remove all containers (use with caution):
# Stop all running containers
docker stop $(docker ps -q)
# Remove all containers
docker rm $(docker ps -aq)Containers with restart: always or restart: unless-stopped may respawn before you can remove them:
# Disable restart policy first
docker update --restart=no <container_name_or_id>
# Then stop and remove
docker stop <container_name_or_id>
docker rm <container_name_or_id>For Docker Swarm services, remove the service instead:
# List services
docker service ls
# Remove the service
docker service rm <service_name>When using Docker Compose and encountering this error during up or down:
# Stop and remove containers, networks, volumes
docker-compose down
# Or with newer Compose V2
docker compose down
# Force recreate containers
docker-compose up -d --force-recreateIf you're seeing race conditions with Compose V2 (experimental), try:
# Use the legacy docker-compose command
docker-compose down && docker-compose up -d
# Or restart Docker Desktop and try againAutomation best practices: In scripts and CI/CD pipelines, always handle the case where a container might or might not be running:
# Safe removal that works whether container is running or not
docker rm -f <container_name> 2>/dev/null || true
# Or check status first
if docker ps -q -f name=<container_name> | grep -q .; then
docker stop <container_name>
fi
docker rm <container_name> 2>/dev/null || trueContainer cleanup with volumes: To also remove anonymous volumes attached to the container:
docker rm -v <container_name>
# Or with force
docker rm -fv <container_name>Named volumes are preserved: The -v flag only removes anonymous volumes. Named volumes must be explicitly removed with docker volume rm.
Race conditions in Compose V2: Docker Compose V2 had known issues (#8619, #8446) where run --rm would fail with this error. The container would start successfully but removal would fail because the stop operation hadn't completed. Upgrading to a newer version of Docker Compose typically resolves this.
Exit codes: When force removing, be aware that:
- Exit code 0: Container removed successfully
- The container process receives SIGKILL (exit code 137) before removal
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec