docker

docker

ข่าวและบทความที่เกี่ยวข้อง

All docker

PostType Filter En

บทความ

VSCode DevContainer: เครื่องมือพัฒนาสารพัดใช้
DevContainer คืออะไร? ผู้อ่านเคยประสบกับปัญหาการพัฒนาโปรแกรมแบบนี้บ้างหรือเปล่า? ทำไมกว่าจะ Build ได้มันยากจังง? ทำไมต้อง Install อะไรหลายอย่าง? Project นี้ Framework ใช้ Version อะไรนะ? เครื่องเพื่อน Build ผ่านทำไมเครื่องฉัน Build พัง? หากปัญหาที่กล่าวมาฟังดูคู้นนน คุ้น ผู้เขียนคิดว่า DevContainer จะเป็นตัวเลือกหนึ่งที่สามารถใช้แก้ปัญหาดังกล่าวได้ สำหรับบทความนี้ผู้เขียนขออนุมานก่อนว่าผู้อ่านคุ้นชินกับ Docker อยู่แล้วประมาณหนึ่ง (แต่ถ้ายังไม่เคยลอง ท่านผู้อ่านสามารถเริ่มทำความรู้จักกับ Docker ก่อนได้ที่นี่) สมมุติว่าเรามี Docker Container ซึ่งเราสามารถติดตั้งอะไรก็ได้ลงไปบน Container ตัวนี้ ทีนี้จะเกิดอะไรขึ้นถ้าเราติดตั้งชุดพัฒนาทุกอย่างที่เราต้องการบน Container ตัวนั้น? คำตอบคือเราจะสามารถ Build/Compile และ Run สิ่งที่เราเขียนได้นั่นเอง ทีนี้ลองคิดต่อจะเกิดอะไรขึ้นอีกถ้า VSCode มันสามารถต่อเข้าไปยัง Container ตัวนี้และใช้ Container ตัวนี้เป็น Workspace? นั่นคือเราใช้ VSCode เป็นตัวเขียน Code ซึ่งทำงานหลังบ้านใน (Dev)Container นั่นเอง… ขออนุญาตอ้างอิง(ไม่ได้ขโมยนะครับ) เอารูปจากเวป VSCode มาใช้ประกอบการอธิบายนะครับ จากรูปจะเห็นได้ว่ากรอบด้านซ้ายมือ Local OS เป็นตัวแทนของ OS บนเครื่องจริง ๆ ของท่านผู้อ่าน (โดยทั่วไปคงจะเป็น Windows) และซ้ายมือ Container นั่นคือ Docker Container ที่ VSCode จะใช้เป็น Workspace ในการพัฒนา ดังนั้นไม่ว่าผู้อ่านจะพัฒนาภาษาอะไร (หมายถึงภาษาคอมพิวเตอร์นะครับ ^_^’’’) ก็จะใช้เครื่องมือบนเครื่องอยู่เพียงสามสี่อย่างเท่านั้น นั่นก็คือ… เครื่องมือที่ต้องใช้ เพียงแค่ผู้อ่านติดตั้งเครื่องมือเหล่านี้ ก็สามารถลงมือพัฒนาได้เลย (สะดวกสบายมั้ย!) การทดลองใช้ หากผู้อ่านไม่มี Source Code หรือ Project อยู่ในมือเลย ก็สามารถเริ่มสร้างใหม่ได้จากศูนย์ โดยการใช้ VSCode ช่วย ให้ผู้อ่านทำตามขั้นตอนดังนี้ การแก้ไขไฟล์ภายใต้โฟลเดอร์ .devcontainer ที่มีอยู่ ภายใต้ Folder .devcontainer นั้นจะมีไฟล์หลักอยู่สามไฟล์ ได้แก่ devcontainer.json เป็นไฟล์หลักที่บ่งบอก VSCode ว่า Parent Folder นี้มี DevContainer อยู่ด้วย ภายในไฟล์นี้มีหน้าตาตั้งต้นดังนี้ คำอธิบาย Property docker-compose.yml เหมือนกับไฟล์ docker-compose.yml ธรรมดา ที่ใช้กับคำสั่ง docker compose โครงสร้างภายในคือ Docker Compose YAML เราสามารถทำทุกอย่างได้เหมือนกับ Docker Compose ปกติ (ตัวอย่างเช่น เพิ่ม Service ใส่ Volume หรือใส่ Environment Variable เป็นต้น) Dockerfile เหมือนกับไฟล์ DockerFile ธรรมดา ที่ใช้กับคำสั่ง docker build โครงสร้างภายในคือ Docker File เราสามารถทำได้เหมือนกับ Docker File ปกติเช่นกัน ทดลองสร้าง API Server ในห้วข้อนี้ผู้เขียนจะพาผู้อ่านทดลองการสร้าง API Server ขึ้นมาใน DevContainer ที่เราได้สร้างกันไว้ในหัวข้อ การทดลองใช้ ให้ผู้อ่านทำตามขั้นตอนนี้ได้เลยยย หาก Run สำเร็จ Terminal จะแสดงผลลัพธ์ดังนี้ การแก้ไขปัญหาเบื้องต้น ถ้าหาก VSCode ไม่สามารถสร้าง Docker Container ได้สำเร็จจะเกิด Message Box ขึ้นมาดังนี้ ผู้อ่านสามารถลองคลิกปุ่ม Retry หรือ Open docker-compose.yml Locally ก็ได้ ถ้าหาก Retry แล้วไม่สำเร็จให้ลองกดปุ่ม Open docker-compose.yml Locally แล้ว VSCode จะแสดงหน้าต่าง Log ขึ้นมา จากนั้นให้พิจารณา Log ที่เกิดขึ้น (ในที่นี้ผู้เขียนใส่ย่อหน้าใน docker-compose.yml ผิด) แล้วแก้ไขให้ถูกต้องแล้วลองคลิกเลือกเมนู Reopen in Container จะเอาไปใช้จริง ๆ ยังไงดี? อย่างที่ผู้เขียนเล่าไปแล้วว่า DevContainer นั้นใช้ docker-compose เป็นแกน ดังนั้นผู้อ่านอาจจะลองหา Framework ในเมนูดูก่อน เผื่อว่าอาจจะมีสิ่งที่ผู้อ่านต้องการจะสร้างอยู่ในนั้นแล้ว ถ้าเป็นแบบนี้ผู้อ่านไม่ต้องทำอะไรมาก แต่ถ้าหากผู้อ่านต้องการปรับเปลี่ยน Compose (ยกตัวอย่างเช่น เปลี่ยนจาก MongoDB เป็น MySQL เป็นต้น) อันนี้ก็สามารถเปลี่ยน docker-compose.yml ได้เลย ข้อสังเกตอีกประการหนึ่งก็คือ เมื่อเราใช้ DevContainer เราจะไม่ติดตั้งเครื่องมือที่เกี่ยวกับภาษา หรือ Framework ที่เราจะพัฒนา (เช่นถ้าผู้อ่านจะพัฒนา Node ผู้อ่านก็ไม่ต้องติดตั้ง Node บนเครื่อง) เพราะเครื่องมือต่าง ๆ นั้นอยู่ภายใน Container อยู่แล้ว สรุป DevContainer เป็นเครื่องมืออีกชิ้นหนึ่งที่ทำให้เครื่องผู้อ่าน ไม่รก และไม่ติดปัญหาตอน Build/Run สามารถจำลอง Database โดยไม่ต้องกังวลเรื่องข้อมูล (เพราะเริ่มต้นจาก Database เปล่า ๆ ได้) ทำให้ชีวิตง่ายขึ้น ลองเล่นกับมันดูนะครับ เนื้อหาโดย ประณิธาน ธรรมเจริญพรตรวจทานและปรับปรุงโดย พีรดล สามะศิริ
4 August 2021

บทความ

มาสนุกกับการใช้งาน Docker สำหรับ Data Science กัน
จะทำอย่างไรถ้าต้องทำ Project Data Science ที่ต้องพัฒนากันทั้งในหลาย Environment หลาย Version มาทำความรู้จักกับปลาวาฬสีฟ้า เจ้า Docker ที่จะมาแก้ปัญหาเหล่านี้
19 February 2021
PDPA Icon

We use cookies to optimize your browsing experience and improve our website’s performance. Learn more at our Privacy Policy and adjust your cookie settings at Settings

Privacy Preferences

You can choose your cookie settings by turning on/off each type of cookie as needed, except for necessary cookies.

Accept all
Manage Consent Preferences
  • Strictly Necessary Cookies
    Always Active

    This type of cookie is essential for providing services on the website of the Personal Data Protection Committee Office, allowing you to access various parts of the site. It also helps remember information you have previously provided through the website. Disabling this type of cookie will result in your inability to use key services of the Personal Data Protection Committee Office that require cookies to function.
    Cookies Details

  • Performance Cookies

    This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

  • Functional Cookies

    This type of cookie enables the Big Data Institute (Public Organization)’s website to remember the choices you have made and deliver enhanced features and content tailored to your usage. For example, it can remember your username or changes you have made to font sizes or other customizable settings on the page. Disabling these cookies may result in the website not functioning properly.

  • Targeting Cookies

    "This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

Save settings
This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.