การเลือก start command สำหรับ Docker image

Docker เบื้องต้น

Tim Sangster

Software Engineer @ DataCamp

start command คืออะไร?

image hello-world จะพิมพ์ข้อความแล้วหยุดทำงาน

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 เบื้องต้น

start command คืออะไร?

image ที่มี Python สามารถเริ่ม Python ได้ทันทีเมื่อสตาร์ท

docker run python3-sandbox
Python 3.10.6 (main, Nov  2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
...
....
>>> exit()
repl@host:/#
Docker เบื้องต้น

การรัน shell command เมื่อเริ่มต้น

CMD <shell-command>

คำสั่ง CMD:

  • รันเมื่อเริ่มต้น image
  • ไม่เพิ่มขนาดของ image .
  • ไม่เพิ่มเวลาในการ build
  • หากมีหลายคำสั่ง จะใช้เฉพาะคำสั่งสุดท้าย
Docker เบื้องต้น

การใช้งานทั่วไป

เริ่มต้นแอปพลิเคชันสำหรับรัน workflow หรือรับการเชื่อมต่อจากภายนอก

CMD python3 my_pipeline.py
CMD postgres

เริ่มต้นสคริปต์ที่ทำหน้าที่เปิดหลายแอปพลิเคชันพร้อมกัน

CMD start.sh
CMD python3 start_pipeline.py
Docker เบื้องต้น

จะหยุดเมื่อใด?

$$

  • image hello-world -> หลังพิมพ์ข้อความเสร็จ
  • image ฐานข้อมูล -> เมื่อฐานข้อมูลออกจากระบบ

$$

image ที่มีความทั่วไปมากขึ้นต้องการ start command ที่ยืดหยุ่นกว่า

  • Ubuntu image -> เมื่อปิด shell
Docker เบื้องต้น

การ override start command เริ่มต้น

เริ่มต้น image

docker run <image>

เริ่มต้น image ด้วย start command กำหนดเอง

docker run <image> <shell-command>

เริ่มต้น image แบบ interactive ด้วย start command กำหนดเอง

docker run -it <image> <shell-command>
docker run -it ubuntu bash
Docker เบื้องต้น

สรุป

การใช้งาน คำสั่งใน Dockerfile
เพิ่ม shell command ที่รันเมื่อสร้าง container จาก image CMD <shell-command>

การใช้งาน Shell Command
Override CMD ที่กำหนดใน image docker run <image> <shell-command>
Override CMD ที่กำหนดใน image และรันแบบ interactive docker run -it <image> <shell-command>
Docker เบื้องต้น

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

Docker เบื้องต้น

Preparing Video For Download...