How to Remove Multiple Docker Images using ‘docker image rm’ or ‘docker rmi’ instead of using ‘docker image prune –all’ in Microsoft Windows

Posted on

Introduction

So, this is an article where every previous articles come as a reference. So far, there is an article which is giving information about how to remove unused docker images, either it is exist with a name or a dangling one. In this context, the term dangling refers to the docker image which has no image name at all. Just access the article with the title of ‘How to Remove Dangling Local Docker Image using docker image Command in Microsoft Windows’ in this link. Furthermore, another article is an article with the title of ‘How to Remove dangling and unused Local Docker Image using docker image prune Command in Microsoft Windows’ in this link. The last one, it is an article with the title of ‘How to Remove Dangling or Unused Local Docker Image using docker rmi Command in Microsoft Windows’ in this link.

All of those articles are specifically useful to remove dangling or unused local docker images. In some level, it can also remove multiple of those images at once. But in certain specific docker command, it will need to specify all of the image ID of the images as the parameter of the command. That is why, the article exist with the title of ‘How to Create Multiple Docker Image with only one Image ID using docker command in Microsoft Windows’ in this link can be useful. So, the trick behind removing multiple docker images is by building those images into one single DockerFile. After that, build it using docker-compose command.

How to Remove Multiple Local Docker Images

In this part, there will be a sequence for removing multiple docker images as a simulation. It is in the form of a sequence in order to explain it clearly. So, the following are those sequences action :

  1. As an example in the previous article, just create a docker-compose.yml file also with a Dockerfile. Just create those two files inside one folder with the following tree content structure :

    C:\repository\docker\wordpress>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\repository\docker\wordpress
    
    05/29/2022 10:43 PM <DIR> .
    05/29/2022 11:31 AM <DIR> ..
    05/29/2022 10:15 AM <DIR> build
    05/20/2022 02:41 PM <DIR> data
    05/28/2022 08:35 AM 0 docker-compose-empty.yml
    05/28/2022 09:22 AM 16 docker-compose-version.yml
    05/30/2022 05:35 PM 733 docker-compose.yml
    05/29/2022 10:43 PM 30 Dockerfile
    4 File(s) 779 bytes
    4 Dir(s) 171,643,117,568 bytes free
    
    C:\repository\docker\wordpress>
    

    The content of each file exist below in the following order. First the ‘docker-compose.yml’ file and then ‘Dockerfile’ file. So, below is the content of the ‘docker-compose.yml’ file :

    version: '3.3'
    services:
       db:
         build: .
         image: mysql:5.7
         container_name: db
         volumes:
           - db_data:"C:\\repository\\docker\\wordpress\\data"
         restart: always      
         environment:        
         MYSQL_ROOT_PASSWORD: somewordpress        
         MYSQL_DATABASE: wordpress        
         MYSQL_USER: wordpress        
         MYSQL_PASSWORD: wordpress    
       wordpress:
         build: .
         container_name: wordpress      
         depends_on:
           - db
         image: wordpress:latest
         ports:        
           - "8000:80"      
         restart: always      
         environment:        
           WORDPRESS_DB_HOST: db:3306        
           WORDPRESS_DB_USER: wordpress        
           WORDPRESS_DB_PASSWORD: wordpress        
           WORDPRESS_DB_NAME: wordpress 
    volumes:     
        db_data: {}
    

    On the other hand, below is the content of the ‘Dockerfile’ file :

    FROM mysql:5.7
    FROM wordpress
  2. Next, before executing the command for building multiple docker images with single image ID, just check the existing docker image available by executing the following command :

    C:\repository\docker\wordpress>docker image list --all
    REPOSITORY TAG IMAGE ID CREATED SIZE
    
    C:\repository\docker\wordpress>
    
  3. After that, just execute the following command to start building the multiple docker images with single image ID :

    C:\repository\docker\wordpress>docker-compose build
    Building db
    [+] Building 1.5s (5/5) FINISHED
    => [internal] load build definition from Dockerfile 0.0s
    => => transferring dockerfile: 31B 0.0s
    => [internal] load .dockerignore 0.0s
    => => transferring context: 2B 0.0s
    => [internal] load metadata for docker.io/library/wordpress:latest 1.3s
    => CACHED [stage-1 1/1] FROM docker.io/library/wordpress@sha256:06c850f59d7bd1b96462fc6287e88f0cd08724077e54d9872c9002449b74309c 0.0s
    => => resolve docker.io/library/wordpress@sha256:06c850f59d7bd1b96462fc6287e88f0cd08724077e54d9872c9002449b74309c 0.0s
    => exporting to image 0.1s
    => => exporting layers 0.0s
    => => writing image sha256:c46ad5ea445b8640bdf9d810dfa01679159a22778cf57c64097e4139dea3943f 0.0s
    => => naming to docker.io/library/mysql:5.7 0.0s
    
    Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
    Building wordpress
    [+] Building 0.7s (5/5) FINISHED
    => [internal] load build definition from Dockerfile 0.0s
    => => transferring dockerfile: 31B 0.0s
    => [internal] load .dockerignore 0.0s
    => => transferring context: 2B 0.0s
    => [internal] load metadata for docker.io/library/wordpress:latest 0.5s
    => CACHED [stage-1 1/1] FROM docker.io/library/wordpress@sha256:06c850f59d7bd1b96462fc6287e88f0cd08724077e54d9872c9002449b74309c 0.0s
    => => resolve docker.io/library/wordpress@sha256:06c850f59d7bd1b96462fc6287e88f0cd08724077e54d9872c9002449b74309c 0.0s
    => exporting to image 0.1s
    => => exporting layers 0.0s
    => => writing image sha256:c46ad5ea445b8640bdf9d810dfa01679159a22778cf57c64097e4139dea3943f 0.0s
    => => naming to docker.io/library/wordpress:latest 0.0s
    
    Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
    
    C:\repository\docker\wordpress>
    
  4. After finishing on executing the above command, just check the available docker images again once more. Apparently, new freshly images appearing after the execution of the above command as follows :

    C:\repository\docker\wordpress>docker image list --all
    REPOSITORY TAG    IMAGE ID     CREATED      SIZE
    mysql      5.7    c46ad5ea445b 30 hours ago 609MB
    wordpress  latest c46ad5ea445b 30 hours ago 609MB
    
    C:\repository\docker\wordpress>
    
  5. But the downside effect is that it seems it create two identical images which has the same amount of size. It is not an effective if one of the file is smaller than the other one. Besides, it seems that the size of each images are the total size combination of both are those images. Last but not least, just execute the following command to test. It is to remove the images only by referring to one single image ID as follows :

    C:\repository\docker\wordpress>docker image list --all
    REPOSITORY   TAG           IMAGE ID     CREATED      SIZE
    mysql        5.7           c46ad5ea445b 43 hours ago 609MB
    wordpress    latest        c46ad5ea445b 43 hours ago 609MB
    <none> <none>  26fca947bf02 5 years ago  436MB
    
    C:\repository\docker\wordpress>
    
  6. Last but not least, just execute the command for removing local docker images by using only one image ID reference a follows :
    C:\repository\docker\wordpress>docker rmi c
    Error response from daemon: conflict: unable to delete c46ad5ea445b (must be forced) - image is referenced in multiple repositories
    
    C:\repository\docker\wordpress>docker rmi c -f
    Untagged: mysql:5.7
    Untagged: wordpress:latest
    Deleted: sha256:c46ad5ea445b8640bdf9d810dfa01679159a22778cf57c64097e4139dea3943f
    
    C:\repository\docker\wordpress>
    

    So, as it exist in the above command execution, it only use one image ID reference which is ‘c’ as one single character to refer to both of the images.

Leave a Reply