การรัน Docker containers

Docker เบื้องต้น

Tim Sangster

Software Engineer @ DataCamp

ข้อกำหนดเบื้องต้น

คำสั่ง การใช้งาน
nano <file-name> เปิด <file-name> ในโปรแกรมแก้ไขข้อความ nano
touch <file-name> สร้างไฟล์เปล่าที่มีชื่อตามที่ระบุ
echo "<text>" แสดง <text> บน console
<command> >> <file> ส่งผลลัพธ์ของ <command> ต่อท้าย <file>
<command> -y ตอบรับอัตโนมัติสำหรับทุก prompt ของ <command>
Docker เบื้องต้น

Docker CLI

  • Docker command line interface จะส่งคำสั่งไปยัง Docker daemon
  • ทุกคำสั่งเริ่มต้นด้วย docker

image ที่ชื่อว่า Ubuntu ซึ่งมีการติดตั้ง Ubuntu OS แบบสมบูรณ์ สามารถเริ่มต้นเพื่อสร้าง Ubuntu OS ที่กำลังทำงานและโต้ตอบได้ผ่าน shell

Docker เบื้องต้น

ผลลัพธ์ของ Docker container

docker run <image-name>
docker run hello-world
Hello from Docker!

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon created a new container from the hello-world image which runs
 the executable that produces the output you are currently reading.
3. The Docker daemon streamed that output to the Docker client, which sent it to 
 your terminal.
Docker เบื้องต้น

การเลือกผลลัพธ์ของ Docker container

docker run <image-name>
docker run ubuntu
repl@host:/# docker run ubuntu

repl@host:/#
Docker เบื้องต้น

Docker container แบบ interactive

การเพิ่ม -it ใน docker run จะเปิด interactive shell ใน container ที่เริ่มต้นขึ้น

docker run -it <image-name>
docker run -it ubuntu
docker run -it ubuntu
repl@container:/#
repl@container:/# exit
exit
repl@host:/#
Docker เบื้องต้น

การรัน container แบบ detached

การเพิ่ม -d ใน docker run จะรัน container ในพื้นหลัง และคืนการควบคุม shell กลับมา

docker run -d <image-name>
docker run -d postgres
repl@host:/# docker run -d postgres
4957362b5fb7019b56470a99f52218e698b85775af31da01958bab198a32b072
repl@host:/#
Docker เบื้องต้น

การดูรายการและหยุด container ที่กำลังทำงาน

docker ps
repl@host:/# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED  
4957362b5fb7   postgres   "docker-entrypoint.s…"   About a minute ago
STATUS              PORTS      NAMES
Up About a minute   5432/tcp   awesome_curie
docker stop <container-id>
repl@host:/# docker stop cf91547fd657
cf91547fd657
Docker เบื้องต้น

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

การใช้งาน คำสั่ง
เริ่มต้น container docker run <image-name>
เริ่มต้น container แบบ interactive docker run -it <image-name>
เริ่มต้น container แบบ detached docker run -d <image-name>
ดูรายการ container ที่กำลังทำงาน docker ps
หยุด container docker stop <container-id>
Docker เบื้องต้น

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

Docker เบื้องต้น

Preparing Video For Download...