ข้ามไปยังเนื้อหาหลัก
คู่มือระบบ เนื้อหาผ่านการตรวจสอบแล้ว

ใช้งาน Docker Container ครั้งแรก: Hello World!

บทความนี้เป็นการแนะนำการใช้งาน Docker เบื้องต้น โดยใช้ Image `hello-world` เพื่อแสดงให้เห็นขั้นตอนการรัน Container ตั้งแต่การดาวน์โหลด Image จาก Docker Hub, การ...

ใช้งาน Docker Container ครั้งแรก: Hello World!

หลังจากที่เราติดตั้ง Docker เรียบร้อยแล้ว ในบทนี้เราจะมาลองใช้งาน Docker Container เป็นครั้งแรกด้วย Image ที่ชื่อว่า hello-world ซึ่งเป็น Image พื้นฐานที่ Docker เตรียมไว้ให้ เพื่อให้คุณได้เห็นภาพรวมการทำงานของ Docker อย่างง่ายที่สุด

ทำไมต้อง Hello World?

โปรแกรม "Hello World" เป็นธรรมเนียมปฏิบัติในการเริ่มต้นเรียนรู้ภาษาโปรแกรมมิ่งหรือเทคโนโลยีใหม่ๆ มันเป็นวิธีที่ง่ายและรวดเร็วในการตรวจสอบว่าทุกอย่างทำงานได้อย่างถูกต้อง และช่วยให้คุณคุ้นเคยกับขั้นตอนพื้นฐานต่างๆ

การใช้คำสั่ง 'docker run'

คำสั่ง docker run คือหัวใจหลักในการสร้างและรัน Container จาก Image ที่เราต้องการ รูปแบบคำสั่งพื้นฐานคือ:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

ในกรณีของเรา เราจะใช้คำสั่งง่ายๆ ดังนี้:

docker run hello-world

เปิด Terminal หรือ Command Prompt ของคุณ แล้วพิมพ์คำสั่งนี้ลงไป:

  1. เปิด Terminal หรือ Command Prompt
  2. พิมพ์ docker run hello-world แล้วกด Enter

คุณจะเห็น Output คล้ายๆ กับด้านล่างนี้:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:c3b53206a52b1a8786a8ccf4c596b6e4ca7ed15025e660a501c2d6b313cd79c4
Status: 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.
    (amd64)
 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 your client.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

ความหมายของ Output ที่ได้

มาทำความเข้าใจ Output ที่เราได้รับกันทีละส่วน:

  • Unable to find image 'hello-world:latest' locally: Docker พยายามหา Image hello-world:latest ในเครื่องของคุณก่อน แต่ไม่พบ
  • latest: Pulling from library/hello-world: Docker จึงทำการดาวน์โหลด Image hello-world:latest จาก Docker Hub ซึ่งเป็น Registry สาธารณะที่เก็บ Image ของ Docker
  • 2db29710123e: Pull complete: Docker ดาวน์โหลด Layer ต่างๆ ของ Image จนครบ
  • Status: Downloaded newer image for hello-world:latest: Docker ดาวน์โหลด Image สำเร็จ
  • Hello from Docker! ...: ข้อความที่แสดงว่า Container hello-world ทำงานได้ถูกต้อง

โดยสรุป สิ่งที่เกิดขึ้นคือ Docker ไม่พบ Image hello-world ในเครื่องของคุณ จึงทำการดาวน์โหลดจาก Docker Hub จากนั้นสร้าง Container จาก Image นั้น และรันโปรแกรมที่อยู่ใน Container นั้น ซึ่งแสดงข้อความ "Hello from Docker!"

การจัดการ Container

เมื่อ Container ทำงานเสร็จสิ้น มันจะหยุดทำงานโดยอัตโนมัติ ในกรณีของ hello-world มันเป็นโปรแกรมที่ทำงานเพียงครั้งเดียวแล้วจบ เราสามารถตรวจสอบ Container ที่เคยรันได้ด้วยคำสั่ง:

docker ps -a

Output ที่ได้จะคล้ายๆ กับด้านล่างนี้:

CONTAINER ID   IMAGE         COMMAND    CREATED        STATUS                      PORTS     NAMES
a1b2c3d4e5f6   hello-world   "/hello"   3 minutes ago   Exited (0) 3 minutes ago              determined_hodgkin

จาก Output คุณจะเห็น:

  • CONTAINER ID: ID เฉพาะของ Container
  • IMAGE: Image ที่ใช้สร้าง Container
  • COMMAND: คำสั่งที่รันใน Container
  • CREATED: เวลาที่ Container ถูกสร้าง
  • STATUS: สถานะของ Container (ในที่นี้คือ Exited (0) หมายถึง Container ทำงานเสร็จสมบูรณ์)
  • NAMES: ชื่อที่ Docker สร้างให้ Container โดยอัตโนมัติ

ถ้าคุณต้องการดู Log ของ Container ที่เคยรัน คุณสามารถใช้คำสั่ง:

docker logs [CONTAINER ID]

โดยแทนที่ [CONTAINER ID] ด้วย ID ของ Container ที่คุณต้องการดู Log เช่น:

docker logs a1b2c3d4e5f6

Output ที่ได้จะเป็นข้อความ "Hello from Docker!" ซึ่งเป็น Output ที่ Container สร้างขึ้น

กรณีใช้งานจริงและข้อผิดพลาดที่พบบ่อย

กรณีใช้งานจริง:

แม้ว่า hello-world จะเป็นเพียงตัวอย่างง่ายๆ แต่หลักการเดียวกันนี้สามารถนำไปใช้กับ Application ที่ซับซ้อนกว่าได้ เช่น การรัน Web Server, Database หรือ Microservice ต่างๆ ใน Container

ข้อผิดพลาดที่พบบ่อย:

  • "docker: command not found": ปัญหานี้มักเกิดจากการที่ Docker ยังไม่ได้ถูกติดตั้งอย่างถูกต้อง หรือ Path ของ Docker ไม่ได้ถูกเพิ่มเข้าไปใน Environment Variables ของระบบ
  • "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?": ปัญหานี้มักเกิดจากการที่ Docker Daemon ไม่ได้ถูก Start หรือมีปัญหาในการเชื่อมต่อกับ Docker Daemon ตรวจสอบให้แน่ใจว่า Docker Desktop (บน Windows/macOS) หรือ Docker Engine (บน Linux) กำลังทำงานอยู่
  • "permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock": ปัญหานี้มักเกิดจากสิทธิ์การเข้าถึง Docker Socket บน Linux ลองรันคำสั่ง sudo usermod -aG docker $USER แล้ว Logout/Login หรือ Restart เครื่อง

สรุป

ในบทนี้ เราได้เรียนรู้การใช้งาน Docker Container เป็นครั้งแรกด้วย Image hello-world เราได้เรียนรู้การใช้คำสั่ง docker run, การทำความเข้าใจ Output ที่ได้, และการจัดการ Container ที่เคยรัน ในบทถัดไป เราจะมาเรียนรู้เกี่ยวกับการจัดการ Container ในรายละเอียดมากขึ้น เช่น การ Start, Stop, Restart, และ Remove Container

Office.in.th Custom Service

ต้องการพัฒนาระบบหรือปรับแต่งจากโครงสร้างนี้?

Office.in.th รับพัฒนาและออกแบบ Enterprise Web Application, API Integration และระบบบริหารงานภาครัฐ/เอกชน พร้อมการสนับสนุนระยะยาว

ดูบริการรับพัฒนาซอฟต์แวร์