2016
Jun
27

這篇文章將會帶你一步一步的建立 Docker Container 並裝好一個網頁伺服器 (apache) ,如果你還沒安裝過 docker ,那麼你可以先讀文章 https://www.puritys.me/docs-blog/article-309

首先我使用的是 Mac 環境,如果你不是用 Mac 也不用擔心,操作步驟是一樣的,一開始要建立一個檔案叫 Dockerfile,我想要安裝的 OS 是最新版的 Centos ,所以第一行我要寫 FROM centos ,如果你想指定 OS 版本,那麼你也可以寫 FROM centos:7,預設沒有指定版號的話, Docker 會去抓最新版。

dockerfile init
  1. FROM centos
  2. MAINTAINER [email protected]

建好 Dockerfile 之後,我們輸入指令 "docker build . " ,這時 docker 會自動幫我們下載 Centos image 然後安裝在機器上,成功後,輸入 docker images 就會看到安裝好的 images 如下,整個 OS 只有 196.8 MB 是不是非常小巧的 Linux 系統呢,如果你的 Docker 設定檔不是命名為 Dockerfile ,那麼你可以自已指定 Dockerfile 名稱: docker build -f myDockerfile

docker images
  1. REPOSITORY TAG IMAGE ID CREATED SIZE
  2. centos latest 904d6c400333 4 weeks ago 196.8 MB

安裝網頁伺服器: Apache web service

上一步只是裝好一個簡單的 OS 系統,接著我們要裝上一些必備軟體, dockerfile 中可以使用 RUN 來執行 Linux 指令,所以我們用 RUN 加上 yum 就可以安裝各種軟體,一開始一定要先執行 yum update -y 先更新軟體清單,再來使用 yum install -y ... 來安裝我們需要的軟體,這裡我安裝了 httpd (Web service) 與 PHP,MySQL 資料庫 ,還有一些常用的小工具,方便我們測試如 vim, net-tool, telnet。

請在 dockerfile 中加下以下四行。

  1. RUN yum update -y
  2. RUN yum install -y httpd vim net-tools
  3. RUN yum install -y php-mysql php
  4. RUN yum install -y mariadb-server mariadb telnet

再執行一次 "docker build ." 這個指令,docker 會再建立一個 Centos image ,並且在這個 image 中,已經安裝好我所指定的那些軟體,這時我再輸入一次 docker images 就會看到一個新的 image ,它的 ID 是 7513cc650b6f。

  1. docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. <none> <none> 7513cc650b6f 16 minutes ago 801.2 MB
  4. centos latest 904d6c400333 4 weeks ago 196.8 MB

啟動 Apache server

這個新的 image 已經安裝好 web service 了,我們就可以啟動它,但是我們得先用 "docker run -t -i 7513cc650b6f /bin/bash" 這個指定開啟 Centos Container 並且登入 ,然後再輸入指令 "/usr/sbin/httpd -f /etc/httpd/conf/httpd.conf &" 來啟動 Apache Server。

完成後,必須先確認 web server 80 port 是否有開啟,這時我們剛剛安裝的小工具就派上用場了,輸入 telnet localhost 80 看看是否有連線成功。

Example
  1. [www@ /]# docker run -t -i 7513cc650b6f /bin/bash
  2. [root@828e8782d60d /]# /usr/sbin/httpd -f /etc/httpd/conf/httpd.conf &
  3.  
  4. AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
  5.  
  6. [1]+ Done /usr/sbin/httpd -f /etc/httpd/conf/httpd.conf
  7.  
  8. [root@828e8782d60d /]# telnet localhost 80
  9. Trying ::1...
  10. Connected to localhost.
  11. Escape character is '^]'.

這個 container 的 web server 已經啟動,但是如果我想要從外部的機器連到 container 的 web server ,這是不允許的,Docker 預設是不允許任何外面的機器直接連線進 container 。

例如我在 Mac terminal 中先用 docker-machine ip 找出 docker host 的 IP,再用 telnet 192.168.99.100 80 ,會發現根本就無法連進這台機器,使用 "curl -k http://192.168.99.100" 也是無效。

Example
  1. [www]@ ~ $ docker-machine ip
  2. 192.168.99.100
  3.  
  4. [www]@ ~ $ telnet 192.168.99.100 80
  5. Trying 192.168.99.100...
  6. telnet: connect to address 192.168.99.100: Connection refused
  7. telnet: Unable to connect to remote host

如何打開 container port

打開 container 80 port 的方式很簡單,使用 "-p 80:80" 即可,這句話是告訴 docker host ,host 的 80 port 會直接連進這個 container 80 port 。

所以我們用 exit 把 container 關掉,重新再啟動一次 container 並指定打開 80 port 。

Example
  1. [www@ /]# docker run -t -i -p 80:80 7513cc650b6f /bin/bash
  2. [root@6629dcac3090 /]# /usr/sbin/httpd -f /etc/httpd/conf/httpd.conf &

這時你在 Mac 用 "curl -k http://192.168.99.100/" 是不是就能順利的收到 Container apache server 的 Response 了。

執行 PHP

再來我想要執行一個簡單的 PHP 檔,我寫了一個 test.php ,內容只寫一句簡單的 echo "My first docker"; 。

建好這個檔案之後,我們把 test.php 與 Dockerfile 放在同一個目錄下,接著我想要把這個檔案 copy 進 image 裡面,並且自動 commit 。

我們只要在 Dockerfile 加入下面這行,然後再次執行 "docker build .",Docker 就會幫我們把這個檔案 commit 到 7513cc650b6f 這個 image 裡,而且更改 IMAGE ID 成 d95cd1ef50dc

Example
  1. COPY test.php /var/www/html/

查看新的 images 列表如下:

Example
  1. [www@ ] $ docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. <none> <none> d95cd1ef50dc 15 minutes ago 801.2 MB
  4. centos latest 904d6c400333 4 weeks ago 196.8 MB

你再重新啟動一次新的 container,檢查一下 /var/www/html 這個目錄是不是已經存在 test.php 了呢。

在 container 中啟動 apache server , 測試一下 PHP 是否也可以正確運作。

整個 Dockerfile 如下
  1. FROM centos
  2. MAINTAINER [email protected]
  3.  
  4. RUN yum update -y
  5. RUN yum install -y httpd vim net-tools
  6. RUN yum install -y php-mysql php
  7. RUN yum install -y mariadb-server mariadb telnet

Docker images list


回應 (Leave a comment)