iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 6
0
DevOps

讓我們來玩玩Docker吧~系列 第 6

Day 6 關於 Save 與 Export 對 Image 之間的差異

我們在製作 Docker Container 時,都是希望我們以後可以持續使用這個 Container (~除非你這 Container 是拋棄式的~),對 Docker 來說建個 Container 是一件 So Easy 的事,但如何多次使用就一起來看看。

從官方 Docker Command Document 來看

Save - Save one or more images to a tar archive
Ecport - Export a container’s filesystem as a tar archive

從語意 (如果看不懂 去問 google 大神吧) 來看,好像 Export 才是我們想要的 ? 我們繼續看下去 ~

首先,pull 一個 ubuntu 的 Image 下來

root@ubuntu:~# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
50aff78429b1: Pull complete
f6d82e297bce: Pull complete
275abb2c8a6f: Pull complete
9f15a39356d6: Pull complete
fc0342a94c89: Pull complete
Digest: sha256:ec0e4e8bf2c1178e025099eed57c566959bb408c6b478c284c1683bc4298b683
Status: Downloaded newer image for ubuntu:latest

看一下 Image 是否有被 pull 下來

root@ubuntu:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              00fd29ccc6f1        10 days ago         111MB

建個 Container 起來

root@ubuntu:~# docker run -itd --name ubuntu ubuntu bash
10fe4f9a01edbe3cdba8af9dc66ab8cec6e05418548a7cdd5ac53050bc547fcd

進去 Container 內

root@ubuntu:~# docker exec -it ubuntu bash
root@10fe4f9a01ed:/#

執行 apt-get update 並下載 nginx

apt-get update
apt-get install nginx

觀看 etc 底下的目錄會有個 nginx 的資料夾

root@10fe4f9a01ed:/# cd /etc/
root@10fe4f9a01ed:/etc# ls
X11                     cron.weekly     fonts      init.d          ld.so.conf     mke2fs.conf     pam.d      rc4.d        sgml         terminfo
adduser.conf            dbus-1          fstab      inputrc         ld.so.conf.d   modules-load.d  passwd     rc5.d        shadow       timezone
alternatives            debconf.conf    gai.conf   insserv         legal          mtab            profile    rc6.d        shells       tmpfiles.d
apt                     debian_version  group      insserv.conf    libaudit.conf  networks        profile.d  rcS.d        skel         ucf.conf
bash.bashrc             default         gshadow    insserv.conf.d  localtime      nginx           rc.local   resolv.conf  subgid       udev
bash_completion.d       deluser.conf    host.conf  issue           login.defs     nsswitch.conf   rc0.d      rmt          subuid       ufw
bindresvport.blacklist  dhcp            hostname   issue.net       logrotate.d    opt             rc1.d      securetty    sysctl.conf  update-motd.d
binfmt.d                dpkg            hosts      kernel          lsb-release    os-release      rc2.d      security     sysctl.d     xdg
cron.daily              environment     init       ld.so.cache     machine-id     pam.conf        rc3.d      selinux      systemd      xml
root@10fe4f9a01ed:/etc# cd n
networks       nginx/         nsswitch.conf
root@10fe4f9a01ed:/etc# cd nginx/

exit 離開 Container

root@10fe4f9a01ed:/etc# exit
exit
root@ubuntu:~#

先來做 Save 動作

root@ubuntu:~# docker save ubuntu > ubuntu_save.tar
root@ubuntu:~# ls
ubuntu_save.tar

再來做 Export 動作

docker export ubuntu > ubuntu_export.tar

這時,看目錄底下的檔案應該會有兩個 tar 檔

root@ubuntu:~# ls
ubuntu_export.tar  ubuntu_save.tar
root@ubuntu:~#

把之前的 Container 和 Image 全清掉

docker stop ubuntu
docker rm ubuntu
docker rmi ubuntu

這時的應該沒有任何 Image 存在

root@ubuntu:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

載入 ubuntu_save.tar

root@ubuntu:~# docker load < ubuntu_save.tar
48e0baf45d4d: Loading layer [==================================================>]  114.5MB/114.5MB
d2f8c05d353b: Loading layer [==================================================>]  15.87kB/15.87kB
5a876f8f1a3d: Loading layer [==================================================>]  14.85kB/14.85kB
6458f770d435: Loading layer [==================================================>]  5.632kB/5.632kB
f17fc24fb8d0: Loading layer [==================================================>]  3.072kB/3.072kB
Loaded image: ubuntu:latest
root@ubuntu:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              00fd29ccc6f1        10 days ago         111MB

建立 Container

root@ubuntu:~# docker run -itd --name ubuntu ubuntu bash
8fe3b78d91d4bf77142aed558466539bd438cf8cfe38562e709b7a90c8cfc4f6

進去 Container 內

root@ubuntu:~# docker exec -it ubuntu bash
root@8fe3b78d91d4:/#

觀看 etc 底下的目錄

root@8fe3b78d91d4:/# cd /etc/
root@8fe3b78d91d4:/etc# ls
X11                     cron.weekly     fstab      inputrc         ld.so.conf.d   modules-load.d  profile    rc6.d        skel         udev
adduser.conf            dbus-1          gai.conf   insserv         legal          mtab            profile.d  rcS.d        subgid       update-motd.d
alternatives            debconf.conf    group      insserv.conf    libaudit.conf  networks        rc.local   resolv.conf  subuid       xdg
apt                     debian_version  gshadow    insserv.conf.d  localtime      nsswitch.conf   rc0.d      rmt          sysctl.conf
bash.bashrc             default         host.conf  issue           login.defs     opt             rc1.d      securetty    sysctl.d
bash_completion.d       deluser.conf    hostname   issue.net       logrotate.d    os-release      rc2.d      security     systemd
bindresvport.blacklist  dhcp            hosts      kernel          lsb-release    pam.conf        rc3.d      selinux      terminfo
binfmt.d                dpkg            init       ld.so.cache     machine-id     pam.d           rc4.d      shadow       timezone
cron.daily              environment     init.d     ld.so.conf      mke2fs.conf    passwd          rc5.d      shells       tmpfiles.d
root@8fe3b78d91d4:/etc#

exit 離開 Container

root@8fe3b78d91d4:/etc# exit
exit
root@ubuntu:~#

載入 ubuntu_export.tar

root@ubuntu:~# cat ubuntu_export.tar | docker import - ubuntu
sha256:7dbfd4a258743f2a0bb39122622c57a8239bc804892df715fb0ab6b2e0da4beb
root@ubuntu:~#

建立 Container

root@ubuntu:~# docker run -itd --name ubuntu ubuntu bash
9c510ab5fb1f8acdfbd797f5b769b49d87e22a44ae2da0fe90737f7ba1439c78

進去 Container 內

root@ubuntu:~# docker exec -it ubuntu bash
root@9c510ab5fb1f:/#

觀看 etc 底下的目錄

root@9c510ab5fb1f:/# cd /etc/
root@9c510ab5fb1f:/etc# ls
X11                     cron.weekly     fonts      init.d          ld.so.conf     mke2fs.conf     pam.d      rc4.d        sgml         terminfo
adduser.conf            dbus-1          fstab      inputrc         ld.so.conf.d   modules-load.d  passwd     rc5.d        shadow       timezone
alternatives            debconf.conf    gai.conf   insserv         legal          mtab            profile    rc6.d        shells       tmpfiles.d
apt                     debian_version  group      insserv.conf    libaudit.conf  networks        profile.d  rcS.d        skel         ucf.conf
bash.bashrc             default         gshadow    insserv.conf.d  localtime      nginx           rc.local   resolv.conf  subgid       udev
bash_completion.d       deluser.conf    host.conf  issue           login.defs     nsswitch.conf   rc0.d      rmt          subuid       ufw
bindresvport.blacklist  dhcp            hostname   issue.net       logrotate.d    opt             rc1.d      securetty    sysctl.conf  update-motd.d
binfmt.d                dpkg            hosts      kernel          lsb-release    os-release      rc2.d      security     sysctl.d     xdg
cron.daily              environment     init       ld.so.cache     machine-id     pam.conf        rc3.d      selinux      systemd      xml
root@9c510ab5fb1f:/etc#

exit 離開 Container

root@9c510ab5fb1f:/etc# exit
exit
root@ubuntu:~#

透過上述的觀看 etc 底下的檔案,你發現了什麼 ?

沒錯,save 沒有 nginx 的資料夾可 export 有 (真相只有一個)

export 會將容器內所改變的東西全部打包匯出,可 save 單純只儲存 Image ,所以為了持續使用 Container 要使用 export 這個指令

好啦,那今天的內容就到這邊結束囉。 我們下期再會 ~ ~


上一篇
Day 5 關於 Container 的那些大小事
下一篇
Day 7 Nginx 建置
系列文
讓我們來玩玩Docker吧~30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言