Skip to content

Image and Container Management

Tanveer Alam edited this page Jan 26, 2019 · 1 revision
  1. Removing images - (docker rmi image:tag or ID) Multiple name:tag or IDs can be passed in a single cmd.

All the containers either in running or exited state.

[tan@localhost run]$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                         PORTS               NAMES
617c11f434dc        nginx:latest         "nginx -g 'daemon of…"   About an hour ago   Exited (0) 20 minutes ago                          lifeCycle1
1804d0919acc        nginx:latest         "nginx -g 'daemon of…"   About an hour ago   Exited (0) About an hour ago                       myweb1
afbbf9af9348        nginx:latest         "nginx -g 'daemon of…"   2 hours ago         Exited (0) 2 hours ago                             elegant_gauss
058cba702d08        centos:latest        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             nervous_kirch
774b39c9beef        centos:latest        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             nostalgic_albattani
fc99f6cd41ed        fce289e99eb9         "/hello"                 5 hours ago         Exited (0) 5 hours ago                             nifty_jackson
e3fb69492bd7        hello-world:latest   "/hello"                 5 hours ago         Exited (0) 5 hours ago                             tender_hoover

So if will try to delete any image on which any container depends(running or exited state) then it will not allow to delete that image.

First we will try delete an image on which no container(running or exited) depends.

[tan@localhost run]$ docker rmi centos:centos6
Untagged: centos:centos6
Untagged: centos@sha256:935c45b2e2c4f01aa5a6ff8625632390c7685835a8dfcfdd50790aabc9c24d11
Deleted: sha256:0cbf37812bff083eb2325468c10aaf82011527c049d66106c3c74298ed239aaf
Deleted: sha256:5d574ede99e4892f96319a3899212029e69e0b9d28c9a84f20bc628497e931db

It deleted without any interrupt.

Now will try to delete an image on which a container depends.

[tan@localhost run]$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                         PORTS               NAMES
617c11f434dc        nginx:latest         "nginx -g 'daemon of…"   About an hour ago   Exited (0) 20 minutes ago                          lifeCycle1
1804d0919acc        nginx:latest         "nginx -g 'daemon of…"   About an hour ago   Exited (0) About an hour ago                       myweb1
afbbf9af9348        nginx:latest         "nginx -g 'daemon of…"   2 hours ago         Exited (0) 2 hours ago                             elegant_gauss
058cba702d08        centos:latest        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             nervous_kirch
774b39c9beef        centos:latest        "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                             nostalgic_albattani
fc99f6cd41ed        fce289e99eb9         "/hello"                 5 hours ago         Exited (0) 5 hours ago                             nifty_jackson
e3fb69492bd7        hello-world:latest   "/hello"                 5 hours ago         Exited (0) 5 hours ago                             tender_hoover


[tan@localhost run]$ docker rmi hello-world:latest
Error response from daemon: conflict: unable to remove repository reference "hello-world:latest" (must force) - container e3fb69492bd7 is using its referenced image fce289e99eb9

So we can either delete this image using force(-f) flag but the right approach would be to delete the dependent container then delete the image.

[tan@localhost run]$ docker rm fc99f6cd41ed
fc99f6cd41ed
[tan@localhost run]$ docker rmi hello-world:latest
Error response from daemon: conflict: unable to remove repository reference "hello-world:latest" (must force) - container e3fb69492bd7 is using its referenced image fce289e99eb9
[tan@localhost run]$ docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                    PORTS               NAMES
617c11f434dc        nginx:latest         "nginx -g 'daemon of…"   6 hours ago         Exited (0) 5 hours ago                        lifeCycle1
1804d0919acc        nginx:latest         "nginx -g 'daemon of…"   6 hours ago         Exited (0) 6 hours ago                        myweb1
afbbf9af9348        nginx:latest         "nginx -g 'daemon of…"   7 hours ago         Exited (0) 6 hours ago                        elegant_gauss
058cba702d08        centos:latest        "/bin/bash"              7 hours ago         Exited (0) 7 hours ago                        nervous_kirch
774b39c9beef        centos:latest        "/bin/bash"              7 hours ago         Exited (0) 7 hours ago                        nostalgic_albattani
e3fb69492bd7        hello-world:latest   "/hello"                 10 hours ago        Exited (0) 10 hours ago                       tender_hoover
[tan@localhost run]$ docker rm e3fb69492bd7
e3fb69492bd7
[tan@localhost run]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS               NAMES
617c11f434dc        nginx:latest        "nginx -g 'daemon of…"   6 hours ago         Exited (0) 5 hours ago                       lifeCycle1
1804d0919acc        nginx:latest        "nginx -g 'daemon of…"   6 hours ago         Exited (0) 6 hours ago                       myweb1
afbbf9af9348        nginx:latest        "nginx -g 'daemon of…"   7 hours ago         Exited (0) 6 hours ago                       elegant_gauss
058cba702d08        centos:latest       "/bin/bash"              7 hours ago         Exited (0) 7 hours ago                       nervous_kirch
774b39c9beef        centos:latest       "/bin/bash"              7 hours ago         Exited (0) 7 hours ago                       nostalgic_albattani
[tan@localhost run]$ docker rmi hello-world:latest
Untagged: hello-world:latest
Untagged: hello-world@sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3

But again containers which are not required one by one is time consuming so we can do:

[tan@localhost run]$ docker ps -a -q
617c11f434dc
1804d0919acc
afbbf9af9348
058cba702d08
774b39c9beef
[tan@localhost run]$ docker rm `docker ps -a -q`
617c11f434dc
1804d0919acc
afbbf9af9348
058cba702d08
774b39c9beef
[tan@localhost run]$ docker ps -a 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[tan@localhost run]$ 

Does deleting an image using which a container is running and currently in Up state. The answer is no. Once a container is ran using an image it no longer requires that image for running because it holds a complete snapshot of the base image.

[tan@localhost run]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              42b4762643dc        3 days ago          109MB
centos              latest              1e1148e4cc2c        7 weeks ago         202MB

[tan@localhost run]$ docker run nginx service nginx status
nginx is not running ... failed!
[tan@localhost run]$ 
[tan@localhost run]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
986e6561b5a8        nginx               "service nginx status"   41 seconds ago      Exited (3) 40 seconds ago                       youthful_ramanujan
[tan@localhost run]$ 
[tan@localhost run]$ docker rmi -f 42b4762643dc
Untagged: nginx:latest
Untagged: nginx@sha256:56bcd35e8433343dbae0484ed5b740843dd8bff9479400990f251c13bbb94763
Deleted: sha256:42b4762643dcc9bf492b08064b55fef64942f055f0da91289a8abf93c6d6b43c
[tan@localhost run]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              1e1148e4cc2c        7 weeks ago         202MB
[tan@localhost run]$ 
[tan@localhost run]$ docker start 986e6561b5a8
986e6561b5a8

Container's World



Clone this wiki locally