การจัดการ Docker image ในเครื่อง

Docker เบื้องต้น

Tim Sangster

Software Engineer @ DataCamp

Docker Hub คือแหล่งรวมและชุมชน container image ที่ใหญ่ที่สุดในโลก มี image มากกว่า 100,000 รายการจากผู้ให้บริการซอฟต์แวร์ โปรเจกต์โอเพนซอร์ส และชุมชน ตัวอย่าง image ที่ถูกดาวน์โหลดมากที่สุด ได้แก่ Nginx, mongoDB, alpine linux, nodejs, redis, busybox และอื่นๆ อีกมาก

Docker เบื้องต้น

การดึง image

docker pull <image-name>
docker pull postgres
docker pull ubuntu
repl@host:/# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
7050e35b49f5: Pull complete 
Digest: sha256:e18f0a777aefabe047a671ab3ec3eed05414477c951ab1a6f352a06974245fe7
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
Docker เบื้องต้น

เวอร์ชันของ image

Docker Image มีหลายเวอร์ชันหรือแท็ก ตัวอย่างในภาพแสดง ubuntu 6 เวอร์ชัน เช่น 18.04 ซึ่งมีแท็กว่า bionic

docker pull <image-name>:<image-version>
docker pull ubuntu:22.04
docker pull ubuntu:jammy
Docker เบื้องต้น

การแสดงรายการ image

docker images
repl@host:/# docker images
REPOSITORY    TAG               IMAGE ID       CREATED         SIZE
hello-world   latest            46331d942d63   7 months ago    9.14kB
ubuntu        bionic-20210723   7c0c6ae0b575   15 months ago   56.6MB
postgres      12.7              f076c2fa35f5   15 months ago   300MB
postgres      10.3              cbb7481ff9d5   4 years ago     232MB
...
Docker เบื้องต้น

การลบ image

docker image rm <image-name>
repl@host:/# docker image rm hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:e18f0a777aefabe047a671ab3ec3eed05414477c951ab1a6f35..
Deleted: sha256:46331d942d6350436f64e614d75725f6de3bb5c63e266e236e04389820a234c4
Deleted: sha256:efb53921da3394806160641b72a2cbd34ca1a9a8345ac670a85a04ad3d0e3507
repl@host:/# docker image rm hello-world  
Error response from daemon: conflict: unable to remove repository reference 
"hello-world" (must force) - container 96a7b7b0c535 is using its 
referenced image 46331d942d63
Docker เบื้องต้น

การล้างข้อมูล container

docker container prune
repl@host:/# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
4a7f7eebae0f63178aff7eb0aa39cd3f0627a203ab2df258c1a00b456cf20063
f98f9c2aa1eaf727e4ec9c0283bc7d4aa4762fbdba7f26191f26c97f64090360

Total reclaimed space: 212 B
Docker เบื้องต้น

การล้างข้อมูล image

docker image prune -a
repl@host:/# docker image prune -a
WARNING! This will remove all images without at least one container associated t..
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: alpine:latest
untagged: alpine@sha256:3dcdb92d7432d56604d4545cbd324b14e647b313626d99b889d0626d..
deleted: sha256:4e38e38c8ce0b8d9041a9c4fefe786631d1416225e13b0bfe8cfa2321aec4bba
deleted: sha256:4fe15f8d0ae69e169824f25f1d4da3015a48feeeeebb265cd2e328e15c6a869f

Total reclaimed space: 16.43 MB
Docker เบื้องต้น

Dangling image

docker images
repl@host:/# docker images
REPOSITORY    TAG               IMAGE ID       CREATED         SIZE
testsql       latest            6c49f0cce145   7 months ago    3.73GB
<none>        <none>            a22b8450b88f   7 months ago    3.73GB
<none>        <none>            10dd2d03f59c   7 months ago    3.73GB
<none>        <none>            878bae40320b   7 months ago    3.73GB
<none>        <none>            4ea70583ba54   7 months ago    3.75GB
<none>        <none>            3c64576a3a7d   7 months ago    3.75GB
Docker เบื้องต้น

สรุปคำสั่งใหม่

การใช้งาน คำสั่ง
ดึง image docker pull <image-name>
ดึง image เวอร์ชันที่ระบุ docker pull <image-name>:<image-version>
แสดงรายการ image ทั้งหมดในเครื่อง docker images
ลบ image docker image rm <image-name>
ลบ container ที่หยุดทำงานทั้งหมด docker container prune
ลบ image ทั้งหมด docker image prune -a
Docker เบื้องต้น

มาฝึกกันเถอะ!

Docker เบื้องต้น

Preparing Video For Download...