ติดตั้งและตรวจสอบ Docker
output ในหน้านี้มาจาก Docker Desktop 4.81.0 (Engine 29.6.1) บน macOS Apple Silicon เมื่อ 2026-09-30 ขั้นตอนติดตั้งบน Linux คัดลอกจากเอกสารทางการและ ไม่ได้ รันที่นี่ (Mac เครื่องนี้ไม่มี apt) สิ่งที่ตรวจสอบไว้จะระบุในจุดที่เกี่ยวข้อง
ตัวเลือกการติดตั้ง
หัวข้อที่มีชื่อว่า “ตัวเลือกการติดตั้ง”Docker แจกจ่ายเป็นสองแพ็กเกจหลัก ขึ้นอยู่กับระบบปฏิบัติการ:
- Docker Desktop — แอปพลิเคชัน GUI ที่รวม Docker Engine, Docker Compose, Buildx, Kubernetes single-node (เลือกเปิดได้) และ dashboard สำหรับ macOS, Windows และ Linux engine รันใน VM ขนาดเล็ก
- Docker Engine — daemon และ CLI สำหรับ Linux ติดตั้งจากแพ็กเกจ ไม่มี GUI เบากว่า และเป็นตัวเลือกปกติสำหรับ server และ CI engine รันบน kernel ของ host โดยตรง
ดาวน์โหลด Docker Desktop จาก docs.docker.com/desktop/setup/install/mac-install (URL เก่า /desktop/install/mac-install/ redirect มาที่นี่) เลือกชิป (Apple Silicon หรือ Intel) แล้วลาก Docker.app ไปที่ Applications และเปิดใช้งาน
Docker พร้อมใช้เมื่อไอคอนปลาวาฬในเมนูบาร์แสดง “Docker Desktop is running.”
Windows
หัวข้อที่มีชื่อว่า “Windows”ดาวน์โหลดตัวติดตั้ง Docker Desktop จาก docs.docker.com/desktop/setup/install/windows-install Docker Desktop บน Windows ใช้ WSL 2 (Windows Subsystem for Linux) เป็น backend ได้ ซึ่งตัวติดตั้งช่วยตั้งค่าให้หากยังไม่ได้เปิดใช้
รีสตาร์ทเมื่อระบบแจ้ง จากนั้น Docker Desktop จะทำงานจาก system tray
บน Linux คุณติดตั้ง Docker Engine โดยตรงจาก apt repository ของ Docker เอง ขั้นตอนด้านล่างทำตาม Install Docker Engine on Ubuntu และ on Debian ณ วันที่ 2026-09-30 URL ของ repository ขึ้นกับ distribution: download.docker.com/linux/ubuntu สำหรับ Ubuntu และ download.docker.com/linux/debian สำหรับ Debian การใช้ path ของ Ubuntu บน Debian ทำให้ apt ชี้ไปยังแพ็กเกจที่ build สำหรับ distribution อื่น
#!/bin/shset -eu# Set DISTRO to ubuntu or debianDISTRO=ubuntu
# Add Docker's official GPG keysudo apt-get updatesudo apt-get install -y ca-certificates curlsudo install -m 0755 -d /etc/apt/keyringssudo curl -fsSL "https://download.docker.com/linux/$DISTRO/gpg" -o /etc/apt/keyrings/docker.ascsudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to apt sources (deb822 format used by the current docs)# Ubuntu derivatives report UBUNTU_CODENAME; Debian only has VERSION_CODENAMEsudo tee /etc/apt/sources.list.d/docker.sources <<EOFTypes: debURIs: https://download.docker.com/linux/$DISTROSuites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")Components: stableArchitectures: $(dpkg --print-architecture)Signed-By: /etc/apt/keyrings/docker.ascEOFsudo apt-get update
# Install Docker Engine, CLI, containerd, Buildx and Compose pluginssudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginสิ่งที่ตรวจสอบในเครื่อง: สคริปต์ผ่าน bash -n และเมื่อ 2026-09-30 ทั้ง https://download.docker.com/linux/ubuntu/gpg และ https://download.docker.com/linux/debian/gpg (รวมถึงไดเรกทอรี dists/) ตอบ HTTP 200 การรันสคริปต์ต้องใช้ host Ubuntu หรือ Debian ที่มี sudo สำหรับ distribution อื่น (Fedora, RHEL, Arch) ชื่อแพ็กเกจและ package manager ต่างกัน แต่รูปแบบเหมือนกัน: เพิ่ม repository ของ Docker แล้วติดตั้ง engine, CLI และแพ็กเกจ containerd — ดู docs.docker.com/engine/install
บน Debian testing และ derivative อย่าง Kali อาจต้องแทนที่ codename ด้วยชื่อ release ของ Debian ที่ตรงกัน (เอกสารใช้ trixie เป็นตัวอย่าง)
หลังติดตั้ง ให้ start daemon และเปิดให้ทำงานตอน boot ได้ (แพ็กเกจ DEB มักจะ start ให้เอง):
sudo systemctl start dockersudo systemctl enable dockerหากต้องการรันคำสั่ง Docker โดยไม่ใช้ sudo ให้เพิ่ม user ของคุณเข้ากลุ่ม docker:
sudo usermod -aG docker $USERnewgrp dockernewgrp docker ทำให้การเปลี่ยนกลุ่มมีผลกับ shell ปัจจุบันทันที; ออกจากระบบแล้วเข้าใหม่ก็ได้ผลเหมือนกัน
ตรวจสอบการติดตั้ง
หัวข้อที่มีชื่อว่า “ตรวจสอบการติดตั้ง”docker version
หัวข้อที่มีชื่อว่า “docker version”docker versionoutput จริงจาก Mac เครื่องนี้ (Docker Desktop):
Client: Version: 29.6.1 API version: 1.55 Go version: go1.26.4 Git commit: 8900f1d Built: Fri Jun 26 11:39:35 2026 OS/Arch: darwin/arm64 Context: desktop-linux
Server: Docker Desktop 4.81.0 (232925) Engine: Version: 29.6.1 API version: 1.55 (minimum version 1.40) Go version: go1.26.4 Git commit: 8ec5ab3 Built: Fri Jun 26 11:39:58 2026 OS/Arch: linux/arm64 Experimental: false containerd: Version: v2.2.5 GitCommit: e53c7c1516c3b2bff98eb76f1f4117477e6f4e66 runc: Version: 1.3.6 GitCommit: v1.3.6-0-g491b69ba docker-init: Version: 0.19.0 GitCommit: de40ad0ต้องมีทั้งส่วน Client และ Server ปรากฏ หากไม่มีส่วน Server แสดงว่า daemon ไม่ทำงาน บน Linux Engine บรรทัดแรกของ server block คือ Server: Docker Engine - Community, OS/Arch เป็น linux/amd64 หรือ linux/arm64 ทั้งสองฝั่ง และ Context เป็น default บน Mac เครื่องนี้ client เป็น darwin/arm64 และ server เป็น linux/arm64: CLI เป็น native ส่วน engine อยู่ใน VM
เมื่อเข้าถึง daemon ไม่ได้ ส่วน Client ยังพิมพ์ออกมา แล้วคำสั่งจึงล้มเหลว (exit code 1):
$ DOCKER_HOST=unix:///nonexistent.sock docker version | tail -2failed to connect to the docker API at unix:///nonexistent.sock; check if the path is correct and if the daemon is running: dial unix /nonexistent.sock: connect: no such file or directorydocker info
หัวข้อที่มีชื่อว่า “docker info”docker infoคำสั่งนี้พิมพ์การตั้งค่าทั้งหมดของ daemon field ที่ควรดูก่อน (ค่าจริงจาก Mac เครื่องนี้):
Server Version: 29.6.1Storage Driver: overlay2Logging Driver: json-fileCgroup Driver: cgroupfsCgroup Version: 2Plugins: Network: bridge host ipvlan macvlan null overlayRuntimes: io.containerd.runc.v2 runcKernel Version: 6.12.76-linuxkitOperating System: Docker DesktopArchitecture: aarch64CPUs: 11Total Memory: 8.721GiBDocker Root Dir: /var/lib/dockerOperating System: Docker Desktop และ Kernel Version: …-linuxkit คือสัญญาณว่าคุณอยู่บน Desktop VM บน Linux server จะแสดง distribution และ kernel ของคุณ Total Memory คือที่ VM ได้รับ ไม่ใช่ RAM ของ Mac docker info ยังแสดง network driver ทั้งหกตัว (bridge host ipvlan macvlan null overlay) ที่โมดูล networking ใช้
docker run hello-world
หัวข้อที่มีชื่อว่า “docker run hello-world”docker run hello-worldoutput จริงบนเครื่องที่ไม่เคย pull image นี้ (สั่ง docker rmi hello-world ก่อน):
Unable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-world58dee6a49ef1: Pulling fs layer58dee6a49ef1: Verifying Checksum58dee6a49ef1: Download complete58dee6a49ef1: Pull completeDigest: sha256:5e23090353324d887c48ad5e5c56d294eab81588df9605b07d1afe895f9cc8f8Status: Downloaded newer image for hello-world:latest
Hello from Docker!This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (arm64v8) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.หมายเหตุ (arm64v8) คือ platform ที่เลือกจาก multi-platform index ของ image layer ID และ Digest จะเปลี่ยนตามเวลา แต่รูปแบบไม่เปลี่ยน การเห็น “Hello from Docker!” ยืนยันว่า pipeline ทำงานครบ: CLI, daemon, การ pull image จาก Docker Hub และการรัน container
docker version
docker run hello-worldReading AI-generated code
หัวข้อที่มีชื่อว่า “Reading AI-generated code”snippet สำหรับ provisioning build server ที่ใช้ร่วมกัน ซึ่ง AI เขียน:
🐛 หา bug ในโค้ดนี้
#!/bin/sh# AI: "let the deploy user run docker without sudo.# This is just a convenience: it does not grant any extra privileges."sudo usermod -aG docker deployเฉลย
การเป็นสมาชิกกลุ่ม docker คือ สิทธิพิเศษ: ใครก็ตามที่เข้าถึง socket ได้สามารถสั่งให้ engine mount path ใดของ engine host เข้า container และอ่านในฐานะ root พิสูจน์บน Mac เครื่องนี้โดย mount root ของ engine host แบบ read-only จาก container ชั่วคราวที่ไม่มี network:
$ docker run --rm --network none -v /:/host:ro alpine:3.24 sh -c 'head -c 60 /host/etc/os-release; ls /host | head -5'PRETTY_NAME="Docker Desktop"UsersVolumesbinboot...ข้อเฉพาะของ Docker Desktop สองข้อ: / ตรงนี้คือ root ของ VM (ไม่ใช่ macOS) และ file sharing ของ Desktop แมปไฟล์ใต้ home ของคุณกลับเป็น macOS user ของคุณเอง demo นี้จึงไม่ได้ให้ root ของ macOS บน Linux Engine daemon รันเป็น root บน host จริง การ mount แบบเดียวกันจึงเปิดเผยไฟล์จริงของ host เช่น /etc/shadow (ไม่ได้รันที่นี่ — Mac เครื่องนี้ไม่มี Linux Engine) นั่นคือสิ่งที่เอกสารหมายถึง “root-level privileges” ทางเลือกที่ให้สิทธิ์น้อยที่สุดคือ rootless mode ที่ตัว daemon รันเป็น user ของคุณ ตามเอกสาร rootless:
#!/bin/sh# Run as the non-root user that should own the daemon (docs.docker.com/engine/security/rootless/)dockerd-rootless-setuptool.sh installexport DOCKER_HOST="unix:///run/user/$(id -u)/docker.sock"rootless มีข้อจำกัดของตัวเอง (เช่น port ต่ำและ network driver บางตัว) มันลดรัศมีความเสียหาย ไม่ได้ตัด trade-off ออกไป
Mental Model
หัวข้อที่มีชื่อว่า “Mental Model”การติดตั้ง Docker คือการติดตั้งสองอย่าง: client ที่ user ไหนก็รันได้ และ engine ที่รันด้วยสิทธิ์สูง (เป็น root บน Linux หรืออยู่ใน VM บน Desktop) การตรวจสอบจึงมีสองครึ่ง — client รันได้ไหม และมันเข้าถึง engine ที่ทำงานได้ ที่ pull และ start อะไรบางอย่างได้ไหม docker version พิสูจน์ครึ่งแรก ส่วน Server block และ hello-world พิสูจน์ครึ่งหลัง
Trade-offs
หัวข้อที่มีชื่อว่า “Trade-offs”| ตัวเลือก | ข้อดี | ข้อเสีย |
|---|---|---|
| Docker Desktop | ติดตั้งง่าย มี GUI รวม Compose, Buildx และ Kubernetes (เลือกได้) | ใช้ทรัพยากรเครื่องมากขึ้น (VM) และองค์กรขนาดใหญ่ต้องสมัครสมาชิกแบบเสียเงิน (เงื่อนไข subscription ของ Docker) |
| Docker Engine | เบา เหมาะกับ server และ CI networking และ path เป็น native | ไม่มี GUI — จัดการทุกอย่างผ่าน CLI และคุณดูแลการอัปเกรดเอง |
สิทธิ์กลุ่ม docker | รันคำสั่งได้โดยไม่ต้อง sudo | สมาชิกทุกคนเข้าถึง host ได้เทียบเท่า root |
| Rootless mode | daemon และ container รันโดยไม่ต้องเป็น root | ฟีเจอร์ถูกจำกัด (port, network driver บางตัว) และตั้งค่ายุ่งกว่า |
Common mistakes
หัวข้อที่มีชื่อว่า “Common mistakes”- รัน
docker runโดยไม่เช็คก่อนว่า daemon ทำงานอยู่ (ส่วน Server หายไปในdocker version) แล้วเจอ error ที่สับสน - ติดตั้ง Docker Engine บน Linux แล้วลืมเพิ่ม user เข้ากลุ่ม
dockerทุกคำสั่งจึงต้องใช้sudoหรือ fail ด้วย permission error ที่/var/run/docker.sock— แล้วเพิ่มทั้งทีมเข้ากลุ่มโดยไม่รู้ว่ามันคือ root - คิดว่า Docker Desktop จำเป็นบนทุก OS ทั้งที่ Linux ควรใช้ Docker Engine โดยตรงเพราะเบากว่า
- วาง URL repository ของ Ubuntu บน host Debian (หรือกลับกัน)
Common Misconceptions
หัวข้อที่มีชื่อว่า “Common Misconceptions”- “
docker versionสำเร็จ แปลว่า Docker ใช้ได้” ส่วน Client พิมพ์ออกมาแม้ไม่มี daemon ให้ตรวจว่ามีส่วน Server หรือรันhello-world - “กลุ่ม docker เหมือนกลุ่ม
staff” ใกล้เคียงsudoแบบไม่ถามรหัสผ่านมากกว่า - “Docker Desktop กับ Docker Engine คือสิ่งเดียวกัน” Desktop เป็นผลิตภัณฑ์ที่รวม engine ใน VM พร้อมเครื่องมือ ส่วน Engine คือแพ็กเกจ daemon ที่ติดตั้งบน Linux