DEV Community

Cover image for รัน Laravel 8 ด้วย Sail บน Mac M1
Atthaphon Urairat
Atthaphon Urairat

Posted on

2 1

รัน Laravel 8 ด้วย Sail บน Mac M1

ผมเพ่ิงได้ใช้ Macbook Air ซึ่งใช้ M1 chip มาครับ และก็ได้เจอปัญหาตอนที่ลองใช้ Docker ในการใช้งาน laravel นิดหน่อย เลยอยากจะมาแชร์ให้ฟังเผื่อคนที่เจอปัญหาเดียวกันจะได้นำไปแก้ไขได้ครับ

ต้องขอบอกก่อนว่าตอนที่ผมใช้นั้น Docker Desktop รองรับการใช้งาน M1 Chip แล้วนะครับ แต่ปัญหาจะอยู่ที่ Image บางตัวที่อาจจะยังไม่รองรับเท่านั้น เอาล่ะมาดูกันเลยครับ

ขั้นแรกเลยให้เราติดตั้ง Laravel ด้วยคำสั่ง

# เปลี่ยน "my-app" ให้เป็นชื่อโปรเจ็คที่เราต้องการ
curl -s https://laravel.build/my-app | bash 
Enter fullscreen mode Exit fullscreen mode

การใช้คำสั่งนี้แทนที่จะใช้คำสั่ง composer ในการสร้างโปรเจ็คนั้นจะทำให้เราได้ ไฟล์ Dockerfile และ Default setting สำหรับ Docker มาโดยอัตโนมัติ โดยที่เราไม่ต้องมานั่ง setup docker เองให้เสียเวลา แต่สำหรับ M1 เมื่อดาวโหลด laravel ด้วยคำสั่งนี้จะติดปัญหาที่ตัว MySQL ครับ

แก้ไขปัญหาเพื่อทำให้ MySQL สามารถทำงานได้

ปัญหาตอนนี้คือ ณ ปัจจุบัน MySQL ไม่สามารถทำงานร่วมกันได้กับสถาปัตยกรรม arm ของ M1 การแก้ไขอาจทำได้มากกว่า 1 วิธี เช่นการเปลี่ยนไปใช้ MariaDB image แทน แต่วันนี้จะขอแสดงวิธีแก้ไขด้วยการเพิ่ม platform linux/x86_64 แทนครับ

ให้เราเปิดไฟล์ docker-compose.yml ขึ้นมาและเพ่ิม platform: 'linux/x86_64' ตามตัวอย่างด้านล่างได้เลยครับ

services:
  mysql:
    image: 'mysql:8.0'
    platform: 'linux/x86_64'
    ...
Enter fullscreen mode Exit fullscreen mode

ในส่วนของด้าน performance แล้วการเพ่ิม emulator linux/x86_64 เข้ามาจะมีผลทำให้เ MySQL ช้าลง แต่อย่างไรก็ดี การทดลอง โปรแกรมในเครื่อง local โดยมากก็ไม่ได้ใช้ข้อมูลทั้งหมดเหมือนใน production ดังนั้นการใช้งาน docker ที่เครื่อง local แบบนี้ก็ไม่น่าจะเป็นประเด็นเท่าไหร่นัก เว้นเสียแต่คุณจะ deploy setting นี้ขึ้น production

หลังจากนั้นก็ทดสอบการใช้งาน Laravel ได้เลย

ทำการ spin up container ด้วยคำสั่ง

./vendor/bin/sail up
Enter fullscreen mode Exit fullscreen mode

จากนั้น รอสักพัก เพื่อให้มันทำการ build เสร็จแล้วก็ไปทดสอบโดยคลิ๊กเข้าไปที่ลิงค์ http://localhost ได้เลย

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (1)

Collapse
 
s4wet profile image
s4wet

ขอบคุณครับ ทดลองแล้วใช้งานได้

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay