Skip to main content
Docker

การเปิดใช้งานหรือสต๊าท Container มีวิธีการทำดังนี้

LABs: ทดสอบโดยใช้ CentOS 7 +Apache+PHP

1. ตรวจสอบอิมเมจที่มีอยู่
[root@arnut ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos centos6.6 77e369743e24 5 days ago 215.8 MB
centos 6.6 77e369743e24 5 days ago 215.8 MB
centos 6 a30bc9f3097e 5 days ago 215.8 MB
centos centos6 a30bc9f3097e 5 days ago 215.8 MB
centos centos7 dade6cb4530a 5 days ago 224 MB
centos latest dade6cb4530a 5 days ago 224 MB
centos 7 dade6cb4530a 5 days ago 224 MB
ubuntu latest 9bd07e480c5b 9 weeks ago 192.7 MB

2. ทำการสต๊าท Container พร้อมติดตั้ง Apache HTTP Server
[root@arnut ~]# docker run centos /bin/bash -c "yum -y update; yum -y install httpd"

3. ตรวจสอบโปรเซส ระบบแสดง Container ID
[root@arnut ~]# docker ps -a | head -2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b245e295b3e centos:7 "/bin/echo 'hello wo 7 minutes ago Exited (0) 7 minutes ago high_pike

4. เพิ่มชื่ออิมเมจที่ต้องการ เข้าไปในระบบด้วยคำสั่ง commit เพื่อให้อิมเมจนี้อยู่ถาวร (อ้างอิงตามชื่อคอนเทนเนอร์ในข้อ 3)
[root@arnut ~]# docker commit 5b245e295b3e custom/webserver1
655223d187eb6417ebdfcafac779ea6ed8ce7cd8189764fd2974941c1fc600d8

5. ตรวจสอบอิมเมจอีกครั้ง
[root@arnut ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
custom/webserver1 latest 655223d187eb 44 seconds ago 224 MB
centos 6.6 77e369743e24 5 days ago 215.8 MB
centos centos6.6 77e369743e24 5 days ago 215.8 MB
centos 6 a30bc9f3097e 5 days ago 215.8 MB
centos centos6 a30bc9f3097e 5 days ago 215.8 MB
centos 7 dade6cb4530a 5 days ago 224 MB
centos centos7 dade6cb4530a 5 days ago 224 MB
centos latest dade6cb4530a 5 days ago 224 MB
ubuntu latest 9bd07e480c5b 9 weeks ago 192.7 MB

6. กำหนดให้รันเว็บเซิร์ฟเวอร์ในคอนเทนเนอร์ ผ่านทางหมายเลขพอร์ต 8081 (สามารถเปลี่ยนเป็นหมายเลขพอร์ตอื่นๆ ได้ แต่อย่าให้ชนกับพอร์ตหลักในระบบที่ใช้งานอยู่)
[root@arnut ~]# docker run -it -p 8081:80 custom/webserver1 /bin/bash
[root@78cb02cf0300 /]#

ึ7. สั่งสต๊าท Apache ภายใต้ Container
[root@arnut ~]# /usr/sbin/httpd &

----------
หากระบบแจ้งมาว่ายังไม่ได้ติดตั้ง Apache ให้ทำการตรวจสอบ และสั่งติดตั้งสต๊าทใหม่
[root@arnut ~]# which httpd
[root@arnut ~]# yum install httpd -y
-----------

9. หลังจากสต๊าทแล้วให้ทดสอบเรียกใช้งานจากฝั่งเครื่องคอมพิวเตอร์ลูกข่าย (Windows 7, Windows 8) โดยพิมพ์ URL ตามด้วยหมายเลขพอร์ตที่กำหนดไว้
รูปแบบ
http://IP_Address:8081/

ตัวอย่าง เช่น
http://192.168.1.2:8081

ทดสอบเขียนภาษา PHP
1. ติดตั้งภาษา PHP ภายใต้ Container
[root@arnut ~]# yum install php -y

2. ทดสอบเขียนสตริปต์ภาษา PHP
[root@78cb02cf0300 /]# vi /var/www/html/phpinfo.php

3. สั่งรีสต๊าท Apache ใหม่
* ยังไม่สามารถใช้คำสั่ง service httpd stop หรือ systemctl stop httpd ได้เนื่องจากยังไม่ได้ติดตั้งคำสั่งลงไปต้องสั่งงาโดยตรงด้วยคำสั่งดังนี้
[root@arnut ~]# ps aux|grep httpd
[root@arnut ~]# httpd -k stop
[root@arnut ~]# /usr/sbin/httpd &
[root@arnut ~]# ps aux|grep httpd

4. ทดสอบเรียกใช้งานจากฝั่งเครื่องคอมพิวเตอร์ลูกข่าย (Windows 7, Windows 8) โดยพิมพ์ URL ตามด้วยหมายเลขพอร์ตที่กำหนดไว้
รูปแบบ
http://IP_Address:8081/filename.php

ตัวอย่าง เช่น
http://192.168.1.2:8081/phpinfo.php

คำสั่งอื่นๆ ที่น่าสนใจ ขณะอยู่ใน Container
- ตรวจสอบ IP
[root@78cb02cf0300 /]# ip addr | grep net
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 172.17.0.14/16 scope global eth0
inet6 fe80::42:acff:fe11:e/64 scope link

- ทดสอบ Ping ไปยังอินเทอร์เน็ต
[root@78cb02cf0300 /]# ping www.yahoo.com
PING fd-fp3.wg1.b.yahoo.com (106.10.138.240) 56(84) bytes of data.
64 bytes from ir2.fp.vip.sg3.yahoo.com (106.10.138.240): icmp_seq=1 ttl=48 time=90.1 ms
64 bytes from ir2.fp.vip.sg3.yahoo.com (106.10.138.240): icmp_seq=2 ttl=48 time=78.5 ms
64 bytes from ir2.fp.vip.sg3.yahoo.com (106.10.138.240): icmp_seq=3 ttl=48 time=78.9 ms

- ตรวจสอบโปรเซสที่ใช้งาน
[root@arnut ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78cb02cf0300 custom/webserver1:latest "/bin/bash" 56 minutes ago Up 56 minutes 0.0.0.0:8081->80/tcp jolly_brattain

- เข้าไปยังคอนเทนเนอร์ไอดี 78cb02cf0300
[root@arnut ~]# docker attach 78cb02cf0300
[root@78cb02cf0300 /]#

- ออกจาก คอนเทนเนอร์
[root@78cb02cf0300 /]# exit
exit
[root@arnut ~]#

- ล้างโปรเซส คอนเทนเนอร์
[root@arnut ~]# docker kill 78cb02cf0300
78cb02cf0300
[root@arnut ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

--
Write by Aj.Arnut Ruttanatirakul
(c) 2015 SysAdmin Knowledge
http://www.sysadmin.in.th
February,13 2015

Tags