2015/03/10

透過用 dockerfile 配置可以快速生成 docker image 並實現 container 部署


透過用 dockerfile 配置可以快速生成 docker image 並實現 container 部署

範例:
官方說明文件:


(1) 本機 dockerfile 建立
# 在本地端的目錄下,建立一個檔案名稱為 Dockerfile
#
# FROM:你的 base image 為基底
# MAINTAINER:維護 images 的人
# RUN:在 images 建立過程執行的指令
# ADD:將本機的檔案或遠端的檔案加入到 image 內的目錄
mtchang@mt ~/SCM_code/docker/apdemo $ vim Dockerfile
FROM ubuntu:latest
MAINTAINER mtchang
# Install packages
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && \
  apt-get -y install supervisor git apache2 libapache2-mod-php5 mysql-server php5-mysql pwgen php-apc php5-mcrypt && \
  echo "ServerName localhost" >> /etc/apache2/apache2.conf
#Enviornment variables to configure php
ENV PHP_UPLOAD_MAX_FILESIZE 10M
ENV PHP_POST_MAX_SIZE 10M
# 執行 docker build 會直接依據 dockerfile 內容編譯 image
mtchang@mt ~/SCM_code/docker/apdemo $ sudo docker build -t mtchang/apdemo   .  (點)
# 注意每個過程都會有各 hash code
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:latest
 ---> 5ba9dab47459
Step 1 : MAINTAINER mtchang
 ---> Running in 584aa4bb3e1c
 ---> 6c304964c942
Removing intermediate container 584aa4bb3e1c
Step 2 : ENV DEBIAN_FRONTEND noninteractive
 ---> Running in 9522aec6d092
 ---> 4eb8188f88d7
Removing intermediate container 9522aec6d092
Step 3 : RUN apt-get update &&   apt-get -y install supervisor git apache2 libapache2-mod-php5 mysql-server php5-mysql pwgen php-apc php5-mcrypt &&   echo "ServerName localhost" >> /etc/apache2/apache2.conf
 ---> Running in b7c3e263758c
...(省略數百行)...
Removing intermediate container cd49243be07b
Step 6 : EXPOSE 8080
 ---> Running in a4843a5a0c8e
 ---> c31129378fe8
Removing intermediate container a4843a5a0c8e
Successfully built c31129378fe8
# 第二次編譯速度會比較快,因為使用了 cache
mtchang@mt ~/SCM_code/docker/apdemo $ vim Dockerfile
mtchang@mt ~/SCM_code/docker/apdemo $ sudo docker build -t mtchang/apdemo .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:latest
 ---> 5ba9dab47459
Step 1 : MAINTAINER mtchang
 ---> Using cache
 ---> 6c304964c942
Step 2 : ENV DEBIAN_FRONTEND noninteractive
 ---> Using cache
 ---> 4eb8188f88d7
Step 3 : RUN apt-get update &&   apt-get -y install supervisor git apache2 libapache2-mod-php5 mysql-server php5-mysql pwgen php-apc php5-mcrypt &&   echo "ServerName localhost" >> /etc/apache2/apache2.conf
 ---> Using cache
 ---> b0d088b36fe3
Step 4 : ENV PHP_UPLOAD_MAX_FILESIZE 10M
 ---> Using cache
 ---> 61e966ed8a2d
Step 5 : ENV PHP_POST_MAX_SIZE 10M
 ---> Using cache
 ---> 431d2a1cc310
Successfully built 431d2a1cc310
# 建立的影像檔案 mtchang/apdemo
mtchang@mt ~/SCM_code/docker/apdemo $ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mtchang/apdemo      latest              431d2a1cc310        14 minutes ago      426 MB
mtchang@mt ~/SCM_code/docker/apdemo $ sudo docker login
Username (mtchang):
Login Succeeded
# 可以單獨將這個檔案 push 到 docker hub 上面
mtchang@mt ~/SCM_code/docker/apdemo $ sudo docker push mtchang/apdemo
The push refers to a repository [mtchang/apdemo] (len: 1)
Sending image list
Pushing repository mtchang/apdemo (1 tags)
511136ea3c5a: Image already pushed, skipping
27d47432a69b: Image already pushed, skipping
5f92234dcf1e: Image already pushed, skipping
51a9c7c1f8bb: Image already pushed, skipping
5ba9dab47459: Image already pushed, skipping
6c304964c942: Image already pushed, skipping
4eb8188f88d7: Image already pushed, skipping
b0d088b36fe3: Image already pushed, skipping
61e966ed8a2d: Image already pushed, skipping
431d2a1cc310: Image already pushed, skipping
Pushing tag for rev [431d2a1cc310] on {https://cdn-registry-1.docker.io/v1/repositories/mtchang/apdemo/tags/latest}
# 以上為依據 dockerfile 的建立方法,但是產生的檔案只有本機可以使用。 push 到 docker hub 仍是影像檔案型態,使用者看不到你的 dockerfile 。

(2) 配合 github 的 dockerfile 建立
Docker hub 可以從 github 的儲存庫中,依據 Dockerfile 的內容自動建立影像檔案。
所以請先在 github 中建立一個 Dockerfile 檔案, README 有則是最好。



在 Docker HUB 內建立一個 REPOS 選擇 Github 的儲存庫。


在 docker hub 內選擇 automated Build 選項。


把 Active 打勾,並確認路徑是否正確。


按下 save and trigger build 就會自動建立 dockerfile 檔案了。



(3) 使用及測試
mtchang@mt ~/SCM_code/docker/apdemo $ sudo docker pull mtchang/apdemo
Pulling repository mtchang/apdemo
d0611a67283c: Download complete
d0611a67283c: Pulling image (latest) from mtchang/apdemo
fa4fd76b09ce: Download complete
1c8294cc5160: Download complete
117ee323aaa9: Download complete
2d24f826cb16: Download complete
d5f32e0f2638: Download complete
f6b232724428: Download complete
a392b7a15278: Download complete
ad48e990d540: Download complete
Status: Downloaded newer image for mtchang/apdemo:latest
mtchang@mt ~/SCM_code/docker/apdemo $ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mtchang/apdemo      latest              d0611a67283c        17 minutes ago      425.9 MB
mtchang@mt ~/SCM_code/docker/apdemo $ sudo docker run -i -t -p 8080:80 mtchang/apdemo/bin/bash
root@c88dac59dbab:/# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:08  
          inet addr:172.17.0.8  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:8/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:648 (648.0 B)  TX bytes:648 (648.0 B)
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
root@c88dac59dbab:/# /etc/init.d/apache2 restart
 * Restarting web server apache2

# Docker HOST 上面看到的 iptables 狀況
mt ~ # iptables -L -n -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination        
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination        
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        
DOCKER     all  --  0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination        
MASQUERADE  all  --  172.17.0.0/16        0.0.0.0/0          
MASQUERADE  tcp  --  172.17.0.8           172.17.0.8           tcp dpt:80
Chain DOCKER (2 references)
target     prot opt source               destination        
DNAT       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8080 to:172.17.0.8:80

# 測試網頁
http://:8080/





沒有留言: