修改現有映像檔

  1. 目前有一 centos 映像檔。
    [root@dyw219 ~]# docker images centos
    REPOSITORY     TAG         IMAGE ID         CREATED          VIRTUAL SIZE
    centos         latest      ee2526f4865b     5 months ago     201.8 MB
    
  2. 使用 centos image 執行 /bin/bash,進入 centos 系統,容器 ID 是 0d30277f64dd。
    [root@dyw219 ~]# docker run -it centos /bin/bash
    
  3. 建立 myfile 檔案後退出。
    [root@0d30277f64dd /]# echo 'test' > myfile
    [root@0d30277f64dd /]# cat myfile
    test
    [root@0d30277f64dd /]# exit
    exit
    
  4. centos 容器已經改了,使用 docker commit 產生更新後的映像檔,-m 提定說明訊息,0d30277f64dd 是修改映像檔時的容器 ID。
    [root@dyw219 ~]# docker commit -m "Added myfile" 0d30277f64dd centos:myfile
    25f7f65b6de07afa47fd0bbd058615f84d80ab8e18b4a17b8c24ec8e62d26a46
    
  5. 再查看 centos image,有兩個版本。
    [root@dyw219 ~]# docker images centos
    REPOSITORY     TAG         IMAGE ID         CREATED          VIRTUAL SIZE
    centos         myfile      25f7f65b6de0     4 minutes ago    201.8 MB
    centos         latest      ee2526f4865b     5 months ago     201.8 MB
    
  6. 使用 centos:myfile image 執行 /bin/bash,進入 centos 系統。
    [root@dyw219 ~]# docker run -it centos:myfile /bin/bash
    
  7. 查看 myfile 檔案確實存在。
    [root@b78979b48165 /]# cat myfile 
    test
    [root@b78979b48165 /]# exit
    exit