#建立dockefile-cron檔案,請自行完成腳本內容 kite@kite-virtual-machine:~/Desktop/genDockerImagesFolder$ touch dockerfile-cron #建立my-cron檔案,請自行完成腳本內容 kite@kite-virtual-machine:~/Desktop/genDockerImagesFolder$ touch my-cron #建立鏡像檔 kite@kite-virtual-machine:~/Desktop/genDockerImagesFolder$ sudo docker image build -f dockerfile-cron -t my-cron . Sending build context to Docker daemon 892.4kB Step 1/9 : FROM alpine:latest ... ... Successfully built a5105bc596ec Successfully tagged my-cron:latest #查看鏡像檔清單 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker images REPOSITORY TAG IMAGE ID CREATED SIZE my-cron latest a6e1e40dd6bb 15 minutes ago 45MB #啟動並創建容器 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker run -d my-cron 97d956e771f6a833368d6ecc693aa79fb300eb04cfc0b435bc665652013401ec #查看容器清單 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker container ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 97d956e771f6 my-cron "/usr/local/bin/supe…" 5 seconds ago Up 4 seconds tender_mahavira
2.2.2. 確認crontab執行狀況
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#進入容器 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker exec -it 97d9 sh #查詢檔案 /app # ls my-cron test.txt #查看test.txt檔案 /app # more test.txt Wed Feb 23 02:11:00 UTC 2022 Wed Feb 23 02:12:00 UTC 2022 #離開容器 /app # exit
#查看volume 清單 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker volume ls DRIVER VOLUME NAME local 05b3074b5770b1a24b36493a3b1153a1070206d4600d19a1508714c9912dc5f5 #查看volume映射資料 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker volume inspect 05b3074b5770b1a24b36493a3b1153a1070206d4600d19a1508714c9912dc5f5 [ { "CreatedAt": "2022-02-23T10:10:59+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/05b3074b5770b1a24b36493a3b1153a1070206d4600d19a1508714c9912dc5f5/_data", "Name": "05b3074b5770b1a24b36493a3b1153a1070206d4600d19a1508714c9912dc5f5", "Options": null, "Scope": "local" } ] #使用ls 加 mountpoint值的路徑,可以發現在host上也有test.txt檔案 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# ls /var/lib/docker/volumes/05b3074b5770b1a24b36493a3b1153a1070206d4600d19a1508714c9912dc5f5/_data my-cron test.txt #使用more查看host的test.txt檔案,資料與容器內部一致 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# more /var/lib/docker/volumes/05b3074b5770b1a24b36493a3b1153a1070206d4600d19a1508714c9912dc5f5/_data/test.txt Wed Feb 23 02:11:00 UTC 2022 Wed Feb 23 02:12:00 UTC 2022
2.2.4. 驗證停止container後,host檔案是否仍存在
1 2 3 4 5 6 7
#停止容器 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker container stop 97d #驗證,就算停止容器host檔案還是存在的 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# more /var/lib/docker/volumes/05b3074b5770b1a24b36493a3b1153a1070206d4600d19a1508714c9912dc5f5/_data/test.txt Wed Feb 23 02:11:00 UTC 2022 Wed Feb 23 02:12:00 UTC 2022
2.2.5. 發現再啟用新的容器,volume重建一個新的區域
1 2 3 4 5 6 7 8 9
#再啟動一個新的container root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker container run -d my-cron 17f1ffa9999a697540fb4aa81a695b66d96b9f57ac05db6af8f9026da4bdb6a7 #查看volume清單,會發現多了一個b49bbb,代表這個容器使用了一個新的硬碟空間 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker volume ls DRIVER VOLUME NAME local 05b3074b5770b1a24b36493a3b1153a1070206d4600d19a1508714c9912dc5f5 local b49bbb9e19dedb0bb36dc3b734ec354eb25e477212dbb5a5156416c63693a25d
#查看目前所有container包含停止的container root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker container ps -a CONTAINER ID IMAGE COMMAND ... 17f1ffa9999a my-cron "/usr/local/bin/supe…" ... 97d956e771f6 my-cron "/usr/local/bin/supe…" ... #強制停止container root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker container rm -f 17f1 97d9 #刪除所有volume,會詢問是否繼續 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker volume prune WARNING! This will remove all local volumes not used by at least one container. Are you sure you want to continue? [y/N] y Deleted Volumes: 05b3074b5770b1a24b36493a3b1153a1070206d4600d19a1508714c9912dc5f5 b49bbb9e19dedb0bb36dc3b734ec354eb25e477212dbb5a5156416c63693a25d
Total reclaimed space: 1.402kB #查看volume 都清空了 root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker volume ls DRIVER VOLUME NAME
此項任務**-v 我是主角**:
1
docker container run -d -v [自定義volume名稱]:[dockerfile內必須有的設定值] [image name]
#先刪除之前的container root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker container rm -f 6150 6150 #再次創建container root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker container run -d -v cron-data:/app my-cron f61223479947f23bb6750d25aa22427e3b8dbb92875af9d4c3dbac761703ea29 #進入container root@kite-virtual-machine:/home/kite/Desktop/pythonworkspace# docker exec -it f6122 sh /app # ls my-cron test.txt /app # more test.txt Wed Feb 23 03:25:00 UTC 2022 Wed Feb 23 03:26:00 UTC 2022 Wed Feb 23 03:27:00 UTC 2022 Wed Feb 23 03:28:00 UTC 2022 Wed Feb 23 03:29:00 UTC 2022 Wed Feb 23 03:30:00 UTC 2022 /app # exit
註記:
1 2 3 4
#刪除所有鏡像 docker system prune
docker volume rm $(docker volume ls -qf dangling=true)
#查看目前目錄檔案,目前只有一個hello.c root@kite-virtual-machine:/home/kite/Desktop/bindMountArea# ls hello.c #創建並運行container $(pwd) 指的是當前目錄 root@kite-virtual-machine:/home/kite/Desktop/bindMountArea# docker container run -it -v $(pwd):/app gcc:9.4 #已進入容器,查看列表 root@e058f803400b:/# ls app bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@e058f803400b:/# cd app #查看的確在/app資料夾底下有跟host一樣的hello.c檔案 root@e058f803400b:/app# ls hello.c #進行編譯 root@e058f803400b:/app# gcc -o hello hello.c #編完譯後多出一個hello檔 root@e058f803400b:/app# ls hello hello.c #小玩一下 root@e058f803400b:/app# ./hello abc hello abc