- สารบัญ
- WSL คืออะไร
- WSL 1 กับ WSL 2 ต่างกันอย่างไร
- ทำไม Docker จึงเหมาะกับ WSL 2
- สถาปัตยกรรม Docker บน WSL 2
- สิ่งที่ต้องเตรียม
- ตรวจสอบ Virtualization
- ติดตั้ง WSL 2
- เปิด Windows Features แบบ Manual
- เปิด Features ผ่าน PowerShell
- กำหนด WSL 2 เป็นค่าเริ่มต้น
- ติดตั้ง Ubuntu
- อัปเดต Ubuntu
- อัปเดต WSL
- ติดตั้ง Docker Desktop
- เปิด WSL Integration
- ไม่ควรติดตั้ง Docker Engine ซ้ำใน Ubuntu
- ทดสอบ Docker
- Workshop 1: รัน Nginx Container
- Workshop 2: ใช้งาน Docker Compose
- Workshop 3: Full Stack ด้วย Docker Compose
- การจัดเก็บ Source Code
- เปิด Project ด้วย VS Code
- Windows เข้าถึงไฟล์ WSL
- WSL เข้าถึงไฟล์ Windows
- Docker Volume
- Bind Mount
- Port Mapping
- คำสั่ง Docker ที่ใช้บ่อย
- คำสั่ง WSL ที่ควรรู้
- Troubleshooting
- Best Practices
- Development Workflow ที่แนะนำ
- Checklist ก่อนเริ่มใช้งาน
- สรุป
- เอกสารอ้างอิง
การพัฒนา Software บน Windows ในปัจจุบันสามารถใช้สภาพแวดล้อม Linux ได้อย่างสะดวกผ่าน Windows Subsystem for Linux (WSL 2) และเมื่อนำมาใช้งานร่วมกับ Docker Desktop จะช่วยให้นักพัฒนาสามารถสร้างและรัน Linux Container บน Windows ได้ในรูปแบบที่ใกล้เคียงกับ Server จริง
บทความนี้อธิบายตั้งแต่การเปิด Windows Features, ติดตั้ง WSL 2 และ Ubuntu, เชื่อมต่อ Docker Desktop กับ WSL ไปจนถึงการทดลองรัน Container และ Docker Compose
#สารบัญ
- WSL คืออะไร
- ทำไม Docker จึงเหมาะกับ WSL 2
- สถาปัตยกรรม Docker บน WSL 2
- สิ่งที่ต้องเตรียม
- ติดตั้ง WSL 2
- ติดตั้ง Ubuntu
- ติดตั้ง Docker Desktop
- เปิด WSL Integration
- ทดสอบ Docker
- Workshop: Nginx
- Workshop: Docker Compose
- การใช้งาน Source Code บน WSL
- คำสั่ง Docker ที่ใช้บ่อย
- Troubleshooting
- Best Practices
- สรุป
#WSL คืออะไร
Windows Subsystem for Linux (WSL) เป็นความสามารถของ Windows ที่ช่วยให้สามารถใช้งาน Linux environment ได้จาก Windows โดยตรง
ผู้ใช้งานสามารถติดตั้ง Linux Distribution เช่น
- Ubuntu
- Debian
- Kali Linux
- openSUSE
จากนั้นสามารถใช้คำสั่ง Linux ได้ เช่น
ls
cd
grep
ssh
git
python
node
go
docker
โดยไม่จำเป็นต้องติดตั้ง Linux แบบ Dual Boot หรือสร้าง Virtual Machine แยกเอง
#WSL 1 กับ WSL 2 ต่างกันอย่างไร
สำหรับงาน Docker ควรเลือก WSL 2
| คุณสมบัติ | WSL 1 | WSL 2 |
|---|---|---|
| Linux Kernel | Compatibility Layer | Linux Kernel จริง |
| Docker | ไม่เหมาะ | เหมาะมาก |
| System Call Compatibility | จำกัดกว่า | สูงกว่า |
| Container | จำกัด | รองรับได้ดี |
| File I/O ภายใน Linux FS | ปานกลาง | ดี |
| เหมาะกับ DevOps | น้อยกว่า | มากกว่า |
ดังนั้นโครงสร้างที่แนะนำคือ
Windows
|
+-- WSL 2
| |
| +-- Ubuntu
|
+-- Docker Desktop
|
+-- WSL 2 Backend
|
+-- Linux Containers
#ทำไม Docker จึงเหมาะกับ WSL 2
Docker Container ส่วนใหญ่ในระบบ Production ทำงานบน Linux
การใช้ Docker บน Windows ผ่าน WSL 2 จึงมีข้อดี เช่น
- ใช้งาน Linux Container ได้โดยตรง
- Environment ใกล้เคียง Production Server
- ใช้ Bash, Git, SSH และ Linux CLI ได้
- ทำงานร่วมกับ VS Code ได้ดี
- ใช้งาน Docker Compose ได้
- รองรับงาน Microservices
- เหมาะกับ DevOps และ Cloud Native Development
- ไม่ต้องสร้าง Linux VM แยกด้วย VirtualBox
#สถาปัตยกรรม Docker บน WSL 2
โครงสร้างโดยทั่วไปมีลักษณะดังนี้
+----------------------------------------------------+
| Windows 10 / Windows 11 |
| |
| +----------------------+ |
| | Visual Studio Code | |
| +----------+-----------+ |
| | |
| v |
| +----------------------+ |
| | WSL 2 | |
| | Ubuntu | |
| | | |
| | Source Code | |
| | Git | |
| | Docker CLI | |
| +----------+-----------+ |
| | |
| v |
| +----------------------+ |
| | Docker Desktop | |
| | WSL 2 Backend | |
| +----------+-----------+ |
| | |
| +-------+--------+ |
| | | |
| v v |
| Containers Volumes |
| |
+----------------------------------------------------+
Docker Desktop ทำหน้าที่ดูแล Docker Engine ส่วน Ubuntu บน WSL ใช้งาน Docker CLI ผ่าน WSL Integration
#สิ่งที่ต้องเตรียม
ควรมี
- Windows 10 หรือ Windows 11 ที่รองรับ WSL 2
- CPU ที่รองรับ Hardware Virtualization
- RAM อย่างน้อย 8 GB แนะนำ 16 GB ขึ้นไปสำหรับงาน Development
- พื้นที่ Disk เพียงพอสำหรับ Docker Images
- สิทธิ์ Administrator สำหรับการติดตั้ง
#ตรวจสอบ Virtualization
เปิด Task Manager
Ctrl + Shift + Esc
เลือก
Performance
จากนั้นเลือก CPU
ตรวจสอบค่า
Virtualization: Enabled
หากเป็น
Disabled
ต้องเปิด Virtualization ใน BIOS/UEFI ก่อน
ชื่อ Option อาจเป็น
Intel Virtualization Technology
Intel VT-x
AMD-V
SVM Mode
#ติดตั้ง WSL 2
เปิด
PowerShell
หรือ
Windows Terminal
แบบ Run as Administrator
จากนั้นรัน
wsl --install
Windows จะติดตั้ง Component ที่เกี่ยวข้องกับ WSL
หลังจากติดตั้งเสร็จควร Restart เครื่อง
#ตรวจสอบสถานะ WSL
wsl --status
ตรวจสอบ Version
wsl --version
ตรวจสอบ Linux Distribution
wsl --list --verbose
หรือ
wsl -l -v
ตัวอย่าง
NAME STATE VERSION
* Ubuntu Running 2
ค่าที่ควรตรวจสอบคือ
VERSION = 2
#เปิด Windows Features แบบ Manual
สามารถค้นหา
Turn Windows features on or off
จากนั้นเปิด
Windows Subsystem for Linux
Virtual Machine Platform
ตัวอย่าง
[✓] Windows Subsystem for Linux
[✓] Virtual Machine Platform
จากนั้น Restart Windows
#เปิด Features ผ่าน PowerShell
สามารถเปิด Windows Subsystem for Linux ด้วยคำสั่ง
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
เปิด Virtual Machine Platform
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
จากนั้น Restart
#กำหนด WSL 2 เป็นค่าเริ่มต้น
wsl --set-default-version 2
หาก Distribution เดิมเป็น WSL 1 สามารถเปลี่ยนเป็น WSL 2 ได้ เช่น
wsl --set-version Ubuntu 2
ตรวจสอบอีกครั้ง
wsl -l -v
#ติดตั้ง Ubuntu
ดู Distribution ที่สามารถติดตั้งได้
wsl --list --online
ติดตั้ง Ubuntu
wsl --install -d Ubuntu
หากต้องการ Ubuntu รุ่นเฉพาะ ให้เลือกชื่อจากรายการที่แสดง เช่น
wsl --install -d Ubuntu-24.04
หลังติดตั้ง เปิด Ubuntu และกำหนด
Username
Password
#อัปเดต Ubuntu
ภายใน Ubuntu Terminal
sudo apt update
sudo apt upgrade -y
ตรวจสอบ Distribution
cat /etc/os-release
ตรวจสอบ Kernel
uname -a
#อัปเดต WSL
ใน PowerShell
wsl --update
หลังอัปเดตสามารถ Restart WSL ด้วย
wsl --shutdown
แล้วเปิด Ubuntu ใหม่
#ติดตั้ง Docker Desktop
ติดตั้ง Docker Desktop for Windows
หลังติดตั้ง เปิด Docker Desktop
ไปที่
Settings
จากนั้น
General
ตรวจสอบว่า Docker Desktop ใช้
WSL 2 based engine
โครงสร้างการทำงานจะเป็น
Docker Desktop
|
+-- WSL 2 Backend
|
+-- Docker Engine
#เปิด WSL Integration
ใน Docker Desktop ไปที่
Settings
-> Resources
-> WSL Integration
เปิด
Enable integration with my default WSL distro
และเลือก Ubuntu
Ubuntu ON
จากนั้นกด
Apply
หลังจากนั้น Docker CLI ภายใน Ubuntu จะสามารถติดต่อ Docker Engine ของ Docker Desktop ได้
#ไม่ควรติดตั้ง Docker Engine ซ้ำใน Ubuntu
หากเลือกใช้
Docker Desktop + WSL 2
โดยทั่วไปไม่ควรติดตั้ง Docker Engine อีกชุดใน Ubuntu เช่น
sudo apt install docker.io
เพราะอาจทำให้เกิด Docker Engine สองชุด
Docker Desktop Engine
+
Docker Engine ใน Ubuntu
และอาจสร้างความสับสนเกี่ยวกับ
- Docker daemon
- Docker socket
- Docker context
- Images
- Containers
- Volumes
แนวทางที่ง่ายสำหรับเครื่อง Development คือ
Docker Desktop
|
+-- Docker Engine
|
+-- Ubuntu ใช้งานผ่าน Docker CLI
#ทดสอบ Docker
เปิด Ubuntu Terminal
ตรวจสอบ Docker Client และ Server
docker version
ตรวจสอบรายละเอียด
docker info
ทดลองรัน Container
docker run hello-world
หากระบบทำงานถูกต้อง จะเห็นข้อความยืนยันจาก Docker
#Workshop 1: รัน Nginx Container
ทดลองสร้าง Web Server
docker run -d --name nginx-demo -p 8080:80 nginx
ตรวจสอบ Container
docker ps
ตัวอย่าง
CONTAINER ID IMAGE PORTS NAMES
xxxxxx nginx 0.0.0.0:8080->80/tcp nginx-demo
เปิด Browser บน Windows
http://localhost:8080
ควรพบหน้า Welcome ของ Nginx
#ดู Log
docker logs nginx-demo
ติดตาม Log แบบ Real-time
docker logs -f nginx-demo
#หยุด Container
docker stop nginx-demo
ลบ Container
docker rm nginx-demo
หรือสั่งพร้อมกัน
docker rm -f nginx-demo
#Workshop 2: ใช้งาน Docker Compose
สร้าง Project
mkdir -p ~/projects/docker-wsl-demo
cd ~/projects/docker-wsl-demo
สร้างไฟล์
compose.yaml
เนื้อหา
services:
web:
image: nginx:alpine
ports:
- "8080:80"
รัน
docker compose up -d
ตรวจสอบ Service
docker compose ps
เปิด Browser
http://localhost:8080
#ดู Log
docker compose logs
หรือ
docker compose logs -f
#หยุดระบบ
docker compose down
#Workshop 3: Full Stack ด้วย Docker Compose
ตัวอย่างระบบประกอบด้วย
Frontend
Backend
PostgreSQL
สร้างโครงสร้าง
fullstack-demo/
├── frontend/
├── backend/
└── compose.yaml
ตัวอย่าง compose.yaml
services:
frontend:
image: node:22-alpine
working_dir: /app
volumes:
- ./frontend:/app
command: npm run dev -- --host 0.0.0.0
ports:
- "5173:5173"
backend:
image: node:22-alpine
working_dir: /app
volumes:
- ./backend:/app
command: npm run dev
ports:
- "3000:3000"
database:
image: postgres:17
environment:
POSTGRES_DB: app
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data:
รัน
docker compose up -d
ตรวจสอบ
docker compose ps
#การจัดเก็บ Source Code
สำหรับ Project ที่พัฒนาผ่าน WSL ควรเก็บ Source Code ใน Linux filesystem เช่น
/home/username/projects
ตัวอย่าง
mkdir -p ~/projects
cd ~/projects
Clone Repository
git clone https://github.com/example/my-project.git
แล้วเข้า Project
cd my-project
#ทำไมไม่ควรเก็บ Project ใน /mnt/c
WSL สามารถเข้าถึง Drive C: ได้ผ่าน
/mnt/c
ตัวอย่าง
cd /mnt/c/Users
แต่สำหรับ Project ที่มีไฟล์จำนวนมาก เช่น
Node.js
React
Next.js
Laravel
Python
Java
Go
การเก็บ Source Code ใน Linux filesystem มักเหมาะกับ Linux-native development workload มากกว่า
ตัวอย่างที่แนะนำ
~/projects/my-app
แทน
/mnt/c/Users/username/project
#เปิด Project ด้วย VS Code
ติดตั้ง Extension
WSL
จาก Microsoft
จาก Ubuntu Terminal
cd ~/projects/my-app
code .
VS Code UI จะรันบน Windows แต่ Development Environment จะทำงานภายใน WSL
Windows
|
+-- VS Code
|
+-- WSL Extension
|
+-- Ubuntu
|
+-- Source Code
+-- Git
+-- Docker CLI
#Windows เข้าถึงไฟล์ WSL
ใน File Explorer สามารถเปิด
\\wsl$
ตัวอย่าง
\\wsl$\Ubuntu\home\username\projects
บางระบบสามารถใช้
\\wsl.localhost
#WSL เข้าถึงไฟล์ Windows
Drive C:
cd /mnt/c
Drive D:
cd /mnt/d
Mapping โดยทั่วไป
C:\ -> /mnt/c
D:\ -> /mnt/d
#Docker Volume
สร้าง Volume
docker volume create app-data
ดูรายการ
docker volume ls
ใช้งาน
docker run -d --name nginx-volume -v app-data:/usr/share/nginx/html nginx
Named Volume เหมาะกับข้อมูลที่ต้อง Persist เช่น
PostgreSQL
MySQL
Redis
MongoDB
#Bind Mount
หาก Project อยู่ที่
~/projects/web
สามารถ Mount Current Directory เข้า Container ได้
docker run --rm -v "$(pwd)":/app -w /app node:22-alpine node --version
เหมาะกับ Development Environment ที่ต้องการให้ Source Code ใน Host และ Container เชื่อมกัน
#Port Mapping
สมมติ Application ใน Container รันที่ Port 80
Container Port = 80
ต้องการให้ Windows เข้าที่ Port 8080
docker run -p 8080:80 nginx
Flow
Windows Browser
|
| localhost:8080
v
Docker / WSL
|
v
Container:80
#คำสั่ง Docker ที่ใช้บ่อย
#Images
ดู Images
docker images
Download Image
docker pull nginx
ลบ Image
docker rmi nginx
#Containers
ดู Container ที่กำลังทำงาน
docker ps
ดูทั้งหมด
docker ps -a
รัน Container
docker run nginx
หยุด
docker stop <container-name>
เริ่มใหม่
docker start <container-name>
Restart
docker restart <container-name>
ลบ
docker rm <container-name>
#เข้า Shell ของ Container
กรณีมี sh
docker exec -it <container-name> sh
กรณีมี Bash
docker exec -it <container-name> bash
#ดู Logs
docker logs <container-name>
Real-time
docker logs -f <container-name>
#Networks
ดู Network
docker network ls
Inspect
docker network inspect bridge
#Volumes
docker volume ls
ตรวจสอบ
docker volume inspect <volume-name>
#Cleanup
ลบ Container ที่ไม่ทำงาน
docker container prune
ลบ Image ที่ไม่ได้ใช้
docker image prune
ลบ Resource ที่ไม่ได้ใช้หลายประเภท
docker system prune
ควรตรวจสอบ Resource ก่อนใช้คำสั่ง Cleanup โดยเฉพาะในเครื่องที่มีหลาย Project
#คำสั่ง WSL ที่ควรรู้
ดู Distribution
wsl -l -v
เข้า Default Distribution
wsl
เข้า Ubuntu
wsl -d Ubuntu
Shutdown WSL
wsl --shutdown
Update WSL
wsl --update
ดูสถานะ
wsl --status
กำหนด Default Distribution
wsl --set-default Ubuntu
#Troubleshooting
#ปัญหา 1: docker: command not found
ตรวจสอบ Docker Desktop
Docker Desktop
-> Settings
-> Resources
-> WSL Integration
เปิด Ubuntu
จากนั้น Restart WSL
wsl --shutdown
แล้วเปิด Docker Desktop และ Ubuntu ใหม่
#ปัญหา 2: Docker Engine ใช้งานไม่ได้
ตรวจสอบ
docker version
หาก Client ใช้งานได้แต่ Server ไม่ตอบสนอง ให้ตรวจสอบว่า Docker Desktop เปิดอยู่
จากนั้นลอง
wsl --shutdown
แล้วเปิด Docker Desktop ใหม่
#ปัญหา 3: Ubuntu ยังเป็น WSL 1
ตรวจสอบ
wsl -l -v
หากพบ
Ubuntu VERSION 1
เปลี่ยนเป็น WSL 2
wsl --set-version Ubuntu 2
#ปัญหา 4: Port ใช้งานไม่ได้
ตรวจสอบ Container
docker ps
ตรวจสอบ Mapping เช่น
0.0.0.0:8080->80/tcp
จากนั้นเข้า
http://localhost:8080
หาก Port ซ้ำ ให้เปลี่ยน Host Port เช่น
docker run -p 8081:80 nginx
#ปัญหา 5: WSL ใช้ RAM สูง
สามารถสร้างไฟล์
%UserProfile%\.wslconfig
ตัวอย่าง
[wsl2]
memory=8GB
processors=4
swap=4GB
หลังแก้ไข
wsl --shutdown
แล้วเปิด WSL ใหม่
ควรกำหนดค่าให้เหมาะกับ RAM และ Workload ของเครื่อง
#Best Practices
#1. ใช้ WSL 2
ตรวจสอบ
wsl -l -v
ควรเห็น
VERSION 2
#2. อัปเดต WSL เป็นระยะ
wsl --update
#3. ใช้ Docker Desktop WSL Integration
เปิด
Docker Desktop
-> Settings
-> Resources
-> WSL Integration
#4. เก็บ Project ใน Linux Filesystem
แนะนำ
~/projects
เช่น
/home/user/projects
#5. ใช้ Named Volume สำหรับ Database
ตัวอย่าง
volumes:
postgres-data:
ช่วยแยก Persistent Data ออกจาก Source Code
#6. ใช้ .dockerignore
ตัวอย่าง
.git
node_modules
dist
build
coverage
.env
ช่วยลด Docker Build Context
#7. Pin Image Version
สำหรับ Development อาจใช้
image: nginx:alpine
แต่ Production ควรระบุ Version ตามนโยบาย Project เช่น
image: nginx:1.28-alpine
เพื่อให้ Deployment คาดเดาได้มากขึ้น
#8. ใช้ Docker Compose สำหรับหลาย Services
แทนการรัน Container ทีละตัว
docker run ...
docker run ...
docker run ...
ควรสร้าง
compose.yaml
แล้วใช้
docker compose up -d
ช่วยให้ Environment ทำซ้ำได้ง่าย
#9. อย่าเก็บ Secret ไว้ใน Git
ตัวอย่าง Secret
Database Password
API Key
JWT Secret
Cloud Credentials
ควรใช้
.env
และเพิ่ม
.env
ใน .gitignore
#Development Workflow ที่แนะนำ
1. เปิด Windows
|
2. เปิด Docker Desktop
|
3. เปิด Ubuntu / Windows Terminal
|
4. cd ~/projects/my-app
|
5. code .
|
6. docker compose up -d
|
7. พัฒนา Application
|
8. ทดสอบ
|
9. git add
|
10. git commit
|
11. git push
#Checklist ก่อนเริ่มใช้งาน
[ ] Hardware Virtualization เปิดแล้ว
[ ] Windows Subsystem for Linux เปิดแล้ว
[ ] Virtual Machine Platform เปิดแล้ว
[ ] WSL 2 ติดตั้งแล้ว
[ ] Ubuntu ใช้ VERSION 2
[ ] WSL อัปเดตแล้ว
[ ] Docker Desktop ติดตั้งแล้ว
[ ] Docker Desktop ใช้ WSL 2 Backend
[ ] WSL Integration เปิดให้ Ubuntu
[ ] docker version ใช้งานได้
[ ] docker run hello-world ผ่าน
[ ] docker compose ใช้งานได้
#สรุป
การใช้งาน Docker บน Windows ผ่าน WSL 2 เป็นแนวทางที่เหมาะสำหรับนักพัฒนา Software และงาน DevOps เพราะสามารถใช้ Linux Environment ได้โดยตรงจาก Windows
สถาปัตยกรรมที่แนะนำคือ
Windows
|
+-- WSL 2
| |
| +-- Ubuntu
| |
| +-- Source Code
| +-- Git
| +-- Development Tools
|
+-- Docker Desktop
|
+-- WSL 2 Backend
|
+-- Docker Engine
+-- Containers
+-- Images
+-- Networks
+-- Volumes
เมื่อกำหนด WSL 2, Ubuntu และ Docker Desktop อย่างถูกต้อง นักพัฒนาจะสามารถใช้ Workflow ใกล้เคียง Linux Server จริง เหมาะกับงาน
- Web Development
- Backend Development
- Microservices
- API Development
- CI/CD
- DevOps
- Kubernetes
- Cloud Native
- Software Testing
จึงถือเป็น Development Environment ที่มีประสิทธิภาพสำหรับผู้ใช้ Windows ที่ต้องทำงานกับ Docker และ Linux Container
#เอกสารอ้างอิง
-
Docker Documentation — WSL 2
https://docs.docker.com/desktop/features/wsl/ -
Docker Documentation — Docker Desktop on Windows
https://docs.docker.com/desktop/setup/install/windows-install/ -
Microsoft Learn — Install WSL
https://learn.microsoft.com/windows/wsl/install -
Microsoft Learn — Basic commands for WSL
https://learn.microsoft.com/windows/wsl/basic-commands -
Microsoft Learn — WSL Development Environment
https://learn.microsoft.com/windows/wsl/setup/environment
บทความนี้เหมาะสำหรับผู้เริ่มต้น Docker, นักศึกษา Software Engineering, Developer และผู้ที่ต้องการเตรียม Windows Development Environment สำหรับงาน DevOps และ Cloud Native