Docker

จากที่ได้ยินชื่อของ Docker มันมานานเหมือนกัน
ฟังมาก็เยอะ ดังนั้นเราลองมาใช้มันบ้าง ว่าจะยากหรือง่าย ?
เริ่มต้นด้วยการติดตั้งและใช้งานมันเลยดีกว่า
เพราะว่ากว่าจะไปอ่านว่ามันคืออะไร ทำงานยังไงคงต้องใช้เวลานานไป
มาเริ่มกันเลย !!!


1. ไปที่ web มันเลย

อยากรู้ว่ามันคืออะไร อ่านสิ แต่งงอย่างแน่นอน
ยิ่งบอกว่ามันเอาไว้สร้าง lightweight container
งงไปกันใหญ่ ดังนั้นข้ามไปก่อน

2. ไปเริ่มติดตั้งจาก Getting Started ดีกว่า
โดยผมเลือกติดตั้งบน MacOS
เอกสารเตรียมให้ครบมาก copy มาใช้ได้เลย

2.1 ก่อนอื่นให้ทำการ download VirtualBox มาติดตั้งก่อน เนื่องจาก Docker มันใช้งาน
2.2 ต่อมาทำการติดตั้ง boot2loader
ทำหน้าที่จัดการ script ต่างๆ สำหรับจัดการ Virtual Machine ที่ docker จะทำงาน
ทำหน้าที่ติดตั้ง image ของ OS ที่ต้องการใช้งาน

ทำการติดตั้งด้วยคำสั่ง

#ไปที่ folder bin สำหรับติดตั้ง
$cd ~/bin

#ทำการ download boot2loader
$curl https://raw.github.com/boot2docker/boot2docker/master/boot2docker > boot2docker

#เปลี่ยน permission ให้สามารถ execute ได้
$chmod +x boot2docker

2.3 ติดตั้ง Docker client สำหรับเข้าใช้งาน Docker deamon ที่ทำงานอยู่

# Get the docker client file
DIR=$(mktemp -d ${TMPDIR:-/tmp}/dockerdl.XXXXXXX) && \
curl -f -o $DIR/ld.tgz https://get.docker.io/builds/Darwin/x86_64/docker-latest.tgz && \
gunzip $DIR/ld.tgz && \
tar xvf $DIR/ld.tar -C $DIR/ && \
cp $DIR/usr/local/bin/docker ./docker

# Set the environment variable for the docker daemon
$export DOCKER_HOST=tcp://127.0.0.1:4243

# Copy the executable file
$sudo cp docker /usr/local/bin/


3. เริ่มใช้งาน Docker กันดีกว่า

# Initiate the VM
 $./boot2docker init

# Run the VM (the docker daemon)
 $./boot2docker up

ผลก็คือ ทำการสร้าง Virtual Machine ไว้ใน VirtualBox
และ start Docker deamon ขึ้นมา
สามารถตรวจสอบใน VirtualBox ได้ดังรูป

VirtualBox ของ Docker

VirtualBox ของ Docker

และ boot2docker ยังมี parameter อื่นๆ ให้ใช้งานดังนี้
./boot2docker {init|start|up|pause|stop|restart|status|info|delete|ssh|download

4. ทดลองใช้งานผ่าน Docker client ด้วยคำสั่ง

$docker version

ผลการทำงานเป็นดังนี้
Client version: 0.10.0
Client API version: 1.10
Go version (client): go1.2.1
Git commit (client): dc9c28f
Server version: 0.10.0
Server API version: 1.10
Git commit (server): dc9c28f
Go version (server): go1.2.1

5. สามารถเข้าจัดการ Virtual Machine ผ่าน SSH ด้วยคำสั่ง

$./boot2docker ssh

โดยใช้
username=docker
password=tcuser

เข้าใช้งาน VM ผ่าน ssh

เข้าใช้งาน VM ผ่าน ssh

6. ทดสอบ Hello World กับ Docker ด้วยคำสั่ง

$docker run ubuntu /bin/echo hello world

จะทำการแสดงผลลัพธ์ว่า hello world เท่านั้น
แต่ในครั้งแรกจะแสดงผลการทำงานเยอะมากๆ
แต่ถ้าสรุปขั้นตอนการทำงานจริงๆ ของมันเป็นดังนี้
เริ่มด้วยการ download image หลักของ docker จาก Docker Index https://index.docker.io/
สร้าง Linux Container ใหม่
สร้าง File system
ทำการ Mount read-write layer
สร้าง network interface
กำหนด IP ของ container
ทำการประมวลผลต่างๆ
สุดท้ายส่ง output และแสดงผลออกมาดังรูป

ผลการทำงาน

ผลการทำงาน

เท่านี้ก็สามารถ Hello World กับ Docker ได้แล้ว
ต่อไปค่อยมาดูว่าต่อว่าจะทำอะไรกับมันดี !!!!

Tags: