Docker for windows 現在比較好用了,這幾天,我包了一個名稱叫 pigochu/c7-nginx-php-fpm 的 docker image,我用了一種讀取 VOLUME 中檔案可以覆蓋掉 container 內檔案的方式來完成以 UI 來部屬 php 專案的方式,做為日後開發測試用途。
這裡用 Kitematic 示範一個 yii2 advanced template 專案的部屬,當你打開 Kitematic 時,找一個名稱叫 pigochu/c7-nginx-php-fpm 的 Image,然後建立 cotainer。
yii2 的專案,建立一個叫 docker-settings 的目錄,並且目錄結構如下
docker-settting/
/replace-files
/etc
/nginx
然後直接寫一個 nginx.conf 放到 docket-setting/replace-files/etc/nginx 之下
接著用 Kitematic 將 container 的 VOLUME 中的 /var/www/html 掛載到 yii2 專案的根目錄,然後再把 VOLUME /docker-settings 掛載到 yii2 專案下的 docker-settings 目錄,接著重新啟動後,進入EXEC 模式,做一些初始化,如 init 及 composer install 等, 這個 container 就能跑 frontend 及 backend 了。若你不想進入 EXEC 模式,其實也可以直接在 Windows 上執行 composer 初始化的動作,也是可以的。
nginx.conf server 區段的設定範例如下,http://ip:port 就是你的 frontend , http://ip:port/backend/ 就是 backend
server {
set $project_root /var/www/html;
charset utf-8;
client_max_body_size 128M;
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root $project_root/frontend/web;
index index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ^~ /backend {
rewrite ^/backend(.*)$ /backend/web$1 last;
}
location ^~ /backend/web {
root $project_root;
# uncomment the following, if you want to enable speaking URL in the backend
#try_files $uri $uri/ /index.php$is_args$args;
location ~ /\.(ht|svn|git) {
deny all;
}
include /etc/nginx/snippets/php-fpm.conf;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# include default fastcgi:php-fpm setting
include /etc/nginx/snippets/php-fpm.conf;
}
上述設定檔只是 server 的區段,你可以將 container 內的 nginx.conf copy 到你的專案再進行修改 server 區段即可。
目前這個 Image,僅適合快速開發與測試用途,可達到完全用 UI 不用寫到任何 docker 設定及下命令的方式就能輕鬆開發及測試,但請勿用來當正式環境跑就是了。


