Docker cheatsheet

12 คำสั่ง จาก Dockerfile ไปถึง container ที่รันอยู่และ registry image คือแอปที่แพ็กไว้ container คือสำเนาที่กำลังรันของมัน ชีตจะเล่นทีละคำสั่ง กดตัวไหนก็ได้เพื่อดูซ้ำ

docker build -t shop:1 .อ่าน Dockerfile ในโฟลเดอร์นี้ แล้วสร้าง image ชื่อ shop:1
โปรเจกต์imagescontainersregistryDockerfileapp/shop:1

สร้าง image

รัน container

ส่งต่อและเก็บกวาด

สร้าง image

โปรเจกต์imagescontainersregistryDockerfileapp/shop:1
docker build -t shop:1 .อ่าน Dockerfile ในโฟลเดอร์นี้ แล้วสร้าง image ชื่อ shop:1
โปรเจกต์imagescontainersregistryDockerfileapp/shop:1
docker imagesดู image ที่มีในเครื่อง ไม่เปลี่ยนอะไร

รัน container

โปรเจกต์imagescontainersregistryDockerfileapp/shop:1web
docker run -d --name web shop:1สร้าง container จาก image แล้วรันเบื้องหลัง (-d)
โปรเจกต์imagescontainersregistryDockerfileapp/shop:1web
docker psดู container ที่รันอยู่ (-a รวมตัวที่หยุดแล้ว)
โปรเจกต์imagescontainersregistryDockerfileapp/shop:1webshop startedlistening on 8080
docker logs webดูสิ่งที่ container พิมพ์ออกมา (-f ตามดูต่อ)
โปรเจกต์imagescontainersregistryDockerfileapp/shop:1web$ sh
docker exec -it web shเปิด shell ข้างใน container ที่รันอยู่
โปรเจกต์imagescontainersregistryDockerfileapp/shop:1web
docker stop webหยุด container (ส่ง SIGTERM รอ แล้วค่อย SIGKILL)
โปรเจกต์imagescontainersregistryDockerfileapp/shop:1
docker rm webลบ container ที่หยุดแล้ว image ยังอยู่

ส่งต่อและเก็บกวาด

โปรเจกต์imagescontainersregistryDockerfileapp/shop:1registry.example/shop:1
docker tag shop:1 registry.example/shop:1ตั้งชื่อเพิ่มให้ image เดิม ชื่อบอกว่าจะ push ไปที่ไหน
โปรเจกต์imagescontainersregistryDockerfileapp/shop:1registry.example/shop:1shop:1
docker push registry.example/shop:1ส่ง image ขึ้น registry ให้เครื่องอื่นดึงไปใช้ (ตรวจแค่ว่าเป็นคำสั่งที่ถูก: ต้องมี registry)
โปรเจกต์imagescontainersregistryDockerfileapp/registry.example/shop:1shop:1
docker pull registry.example/shop:1ดึง image จาก registry ลงเครื่องนี้ (ตรวจแค่ว่าเป็นคำสั่งที่ถูก: ต้องมี registry)
โปรเจกต์imagescontainersregistryDockerfileapp/
docker rmi registry.example/shop:1 shop:1ลบ image (ต้องไม่มี container ใช้อยู่)

image หรือ container

image ไม่เปลี่ยน build ครั้งเดียวรันได้หลายครั้ง container คือสำเนาที่รันอยู่ มีชั้นที่เขียนได้ของตัวเอง อะไรที่แก้ข้างในหายไปตอน rm เก็บข้อมูลไว้ใน volume

ตรวจแล้ว

docker.sh
#!/bin/sh
# Every command on the Docker cheatsheet, run for real against a small image built from alpine:3.20 (already here: nothing
# is pulled). After each one, a line saying what Docker holds: images, containers and their state. The page's pictures
# are held to these lines (lib/cheat-docker.test.ts). push and pull need a registry, so they are checked as commands
# only (docker push --help). Everything made here has a mindloop-cs- name and is removed at the end.
# Run: sh docker.sh   (needs docker)
set -eu
P=mindloop-cs-
work=$(mktemp -d)
tidy() { docker rm -f "${P}web" >/dev/null 2>&1 || true; docker rmi -f "${P}shop:1" "registry.example/${P}shop:1" >/dev/null 2>&1 || true; }
trap 'tidy; rm -rf "$work"' EXIT
tidy

cat > "$work/Dockerfile" <<'DOCKERFILE'
FROM alpine:3.20
CMD ["sh", "-c", "echo shop started; echo listening on 8080; sleep 600"]
DOCKERFILE

say() { # what Docker holds now, in the words of lib/cheat-docker.ts summary()
  images=$(docker images --format '{{.Repository}}:{{.Tag}}' | sed -n "s/$P//p" | sort | tr '\n' ' ' | sed 's/ $//')
  ctrs=$(docker ps -a --filter "name=^${P}" --format '{{.Names}}:{{.State}}' | sed "s/$P//" | tr '\n' ' ' | sed 's/ $//')
  echo "$1: images=[$images] containers=[$ctrs]"
}

docker build -q -t "${P}shop:1" "$work" >/dev/null; say build
docker images >/dev/null; say images
docker run -d --name "${P}web" "${P}shop:1" >/dev/null; sleep 1; say run
docker ps >/dev/null; say ps
test "$(docker logs "${P}web")" = "shop started
listening on 8080"; say logs
test "$(docker exec "${P}web" sh -c 'echo inside; hostname' | head -1)" = inside; say exec
docker stop -t 1 "${P}web" >/dev/null; say stop
docker rm "${P}web" >/dev/null; say rm
docker tag "${P}shop:1" "registry.example/${P}shop:1"; say tag
docker push --help >/dev/null; docker pull --help >/dev/null; echo "push, pull: checked as commands only (no registry here)"
docker rmi "registry.example/${P}shop:1" "${P}shop:1" >/dev/null; say rmi
รัน: sh docker.sh (ต้องมี Docker)ผลที่พิมพ์ออกมา:
build: images=[shop:1] containers=[]
images: images=[shop:1] containers=[]
run: images=[shop:1] containers=[web:running]
ps: images=[shop:1] containers=[web:running]
logs: images=[shop:1] containers=[web:running]
exec: images=[shop:1] containers=[web:running]
stop: images=[shop:1] containers=[web:exited]
rm: images=[shop:1] containers=[]
tag: images=[registry.example/shop:1 shop:1] containers=[]
push, pull: checked as commands only (no registry here)
rmi: images=[] containers=[]

ทุกคำสั่งยกเว้น push กับ pull รันกับ Docker จริงบน image เล็ก ๆ ที่สร้างจาก alpine:3.20 (ไม่ดึงอะไรใหม่) แต่ละบรรทัดคือสิ่งที่ Docker มีหลังรัน push กับ pull ต้องมี registry เลยตรวจแค่ว่าเป็นคำสั่งที่ถูก ทั้งหมดลบทิ้งตอนจบ