How to remove a docker container on Linux

Docker comes with its own rm command version to assist with docker container removal. Let’s first list all available docker containers:

# docker ps -a    
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
d1c01c8eb336        ubuntu:14.04        "/bin/bash"            5 seconds ago        Exited (0) 3 seconds ago                            ubuntu
df7834f86c78        debian:stable       "/bin/bash"            10 seconds ago      Up 9 seconds                            debian              
9bdd9d49a75b        mongo:3             "/entrypoint.sh mong   18 minutes ago      Up 18 minutes       27017/tcp           mongodb             
774b02c9c51a        oraclelinux:7       "/bin/bash"            27 minutes ago      Up 27 minutes                           oracle7

Docker’s rm command will by default remove only stopped containers and thus any attempt to remove a running docker container will result in error message:

# docker rm debian
Error response from daemon: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f
FATA[0000] Error: failed to remove one or more containers

To remove a running docker container the actual container must must first stopped or --force option needs to appended to docker’s rm command. Example:

# docker rm --force debian
debian

The container named debian is now removed:

# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
d1c01c8eb336        ubuntu:14.04        "/bin/bash"            6 minutes ago       Exited (0) 6 minutes ago                       ubuntu              
774b02c9c51a        oraclelinux:7       "/bin/bash"            7 minutes ago       Exited (0) 7 minutes ago                       oracle7             
9bdd9d49a75b        mongo:3             "/entrypoint.sh mong   38 minutes ago      Exited (0) 4 seconds ago                       mongodb

To remove a single docker container use its relevant CONTAINER ID or NAME in combination with docker’s rm command. For example let’s remove docker container named mongodb:

# docker rm mongodb
mongodb

Docker’s rm command also allows for a removal of multiple containers with a single command. To remove multiple docker containers simply append any CONTAINER ID or container NAME to a docker’s remove command as an argument. For example lets remove docker container with name ubuntu a container with an ID 774b02c9c51a:

# docker rm ubuntu 774b02c9c51a