Removing Container from Docker via Command Line

Posted on

To be able to remove container running and managed by docker is actually easy. But first of all, it need to be checked first whether the running container which is going to be removed is really exist or already run. Below is the command which can be be used :

docker run rm -f image_name

But, we can remove the container by adding several optional parameter as shown in the command execution below :

[root@hostname ~]# docker run --rm -d --name php4 -h php4 -p 80:80 -v /document_root_path_on_host/:/document_root_path_on_container/ cmfatih/php4 /usr/local/apache2/bin/apachectl -D FOREGROUND
Conflicting options: --rm and -d
[root@hostname ]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
cd80c72aa858        cmfatih/php4        "/usr/local/apache2/b"   2 minutes ago       Up 2 minutes>80/tcp   php4
[root@hostname ~]# 

But the above command has failed, since the detached mode of container cannot be remove only with the –rm optional parameter as shown in the above output :

Conflicting options: --rm and -d

Below is another command execution example :

[root@hostname ~]#docker --rm php4
flag provided but not defined: --rm
See 'docker --help'.
[root@hostname ~]#

This is also another attempt which is resulted on a failed result because using rm optional parameter to remove a container cannot be done if it is in a detached or background mode :

[root@hostname ~]# docker rm php4
Error response from daemon: You cannot remove a running container cd80c72aa858bf085a515963ffaf654ae7135846b678986bf2819e072306ea38. Stop the container before attempting removal or use -f
[root@hostname ~]#

Description : 
docker : The command to call docker app
rm : The additional command to remove container
php4 : The name of the container  

Another example of removing php4 container is shown below :

[root@hostname ~]# docker -f rm php4
flag provided but not defined: -f
See 'docker --help'.
[root@hostname ~]#

But, the correct form of the command pattern to remove a container which is already run in daemon service mode or detached mode in background is shown as follow :

[root@hostname ~]# docker -rmf php4
flag provided but not defined: -rmf
See 'docker --help'.
[root@hostname ~]#

[root@hostname ~]# docker rm -f php4
[root@hostname ~]#

The above command to remove the container named php4 using ‘docker rm -f’ has already successfull.

Finally, check again whether the container has already removed or not by executing the following command :

[root@hostname opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@hostname opt]#

One thought on “Removing Container from Docker via Command Line

Leave a Reply