Memo Dockerfile
1 min readJan 8, 2019
Base Docker 17.05 and higher (remote build-context)
Instruction ของ Dockerfile ใช้ทำอะไรบ้าง ใช้อย่างไร
- FROM
Dockerfile มักจะเริ่มด้วยคำสั่งนี้ ใช้สร้าง image หรือใช้ build stage เพื่อพื้นที่อิสระของมัน ซึ่งมันทำงานบน image ทำให้ต้องโหลด image มาก่อนแล้วจะใช้งานต่อได้ - WORKDIR
ระบุ directory สำหรับทำงานคำสั่งด้านล่างต่อไป ถ้าหากยังไม่มีจะถูกสร้างขึ้น ตามคำแนะนำเราควร relative path เสมอ สามารถใช้ environment variables ได้ด้วยน้ะ example
ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
RUN pwd - ADD or COPY
เนื่องจากทั้ง 2 คำสั่งมีการทำงานคล้ายกัน เลยขอรวบไว้เลย ส่วนตัวแล้วใช้ COPY แล้วดูเข้าใจง่ายกว่าADD
คำสั่งCOPY
ใช้ copy local file ไปไว้ใน container - RUN
ใช้ execute คำสั่งต่างๆ ซึ่ง on top บน image ในการใช้คำสั่งตัวนี้เราจำเป็นต้องรู้จักรูปแบบของมัน ซึ่งมี 2 แบบ
(1) shell form —RUN <command>
(2) exec form—RUN ["executable", "param1", "param2"]
- EXPOSE
เพื่อระบุเลข ports ให้กับ container - ENV
สร้าง environment variables ด้วยการใช้ENV
will persist when a container - CMD
เป็นคำสั่งใช้งาน software บน image
(1) exec form —CMD ["executable","param1","param2"]
(exec form, this is the preferred form)
(2) ENTRYPOINT —CMD ["param1","param2"]
(3) shell form —CMD command param1 param2