How to Create Multiple Docker Image with only one Image ID using docker command in Microsoft Windows

Posted on

Introduction

This is another article focusing on how to build multiple docker image but those docker image has only one image ID. This is very easy in order to remove those images at once. Actually, it only need one Dockerfile in order to generate multiple images with only one image ID. In order to specify the list of the current docker images available, just execute the following command :

C:\repository\docker\wordpress>docker image list --all
REPOSITORY TAG IMAGE ID CREATED SIZE
C:\repository\docker\wordpress>

How to Create Multiple Docker Image with only one single Image ID

In order to build multiple images, actually it will need a docker-compose.yml file. Besides the ‘docker-compose.yml” file, it will also need another file which has the name of ‘Dockerfile’. So, the following 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: {}

There is a specific line which is very important in the above script. Those are the ‘build : . ‘ line. It is indicating that there will be a build file in the current directory path. In this case, the build file is the file with the name of ‘Dockerfile’. So, below is the content of the ‘Dockerfile’ in order to have multiple docker image with one single image ID :

FROM mysql:5.7
FROM wordpress

As in the above content exist, there is two lines which is reprensenting different images. Since it exist in one file, although it is a different image, it will have one single image ID. So, below is the execution of the command for building those two images :

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>

So, there is two images which exist as the result of the above command execution. It will result on having a docker image of mysql and wordpress. So, check it after by executing the following command :

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>

For further info, the following is the actual content where is showing the ‘docker-compose.yml’ and also the Dockerfile in the tree structure just for more information :

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) 169,937,170,432 bytes free
C:\repository\docker\wordpress>

Leave a Reply