使用Docker在同一機器中建立多站環境
使用Docker在同一機器中建立多站環境
相信有在開發網站並且使用版控的大家,一定多多少少都有遇過需要同時測試不同feature不同branch的情況。
但可能測試環境又只有一台或是只有一個database,在切換環境的同時可能就影響了資料庫,導致問題發生時不確定是否是另一個branch修改了資料庫所造成;或是直接多個branch merge起來測試卻不知道出問題的是哪一個branch。
最理想的解決辦法就是配合CI/CD, 當發出PR時就建立起一台完全獨立的機器以及基本的sample data,這樣就可以單獨的測試該feature的branch,但上述作法會有相當的成本考量。
以下我們使用docker 及 traefik這個服務的docker來達成差不多的目的。
Docker with Traefik
此Repository可同台機器中使用同一份網站的docker-compose.yml來啟動多台站台,
透過Traefik反向代理不需再個別站台修改nginx port or mysql port等等會衝突的設定, 每個站台可以有自己一套獨立的services
(eg. nginx, php-cli, php-fpm, mysql)
並經過監聽80,8080,443,3306等port, 及label的設定,
透過domain name, subdomain即可進入你想要的web applicaiton中
使用說明
首先 請clone此repo
Traefik
先將trarfik啟動
$ <span class="token function">cd</span> traefik/ <span class="token operator">&&</span> docker-compose up -d
由於trarfik是共用的反向代理會去監聽設定的port 再看request的domain跟subdomain符合哪一個站的label, 所以只要起動一次就好其他站台不需要再啟動
Magento2
在此repo根目錄中除了docker-compose.yml之外還有另外一隻docker-run.sh檔案
如果單純啟動或是進去docker-compose.yml會看到裏面有設定了一些環境變數
這些環境變數就是需要透過docker-run.sh及給定的參數來給予
首先我們要先設定一些基本的環境變數
環境變數設定
在config裏面會看到有
docker-<local|dev|staging|production>-env.sh
等等四隻檔案, 顧名思義裡面就是設定對應環境的環境變數
先將 DOMAINNAME改為你自訂的domain
unset DIRNAME<span class="token punctuation">;</span>
unset DOMAINNAME<span class="token punctuation">;</span>
unset GIT_COMMIT_HASH<span class="token punctuation">;</span>
unset DOCKER_PREFIX<span class="token punctuation">;</span>
<span class="token function">export</span> DOMAINNAME<span class="token operator">=</span><span class="token string">'magento2-test.com'</span>
再確認及修改config/<local|dev|staging|production>
四個資料夾中, magento裡面的config.php還有env.php
- magento/config.php:app/etc/config.php
- magento/env.php:app/etc/env.php
DB預設
MYSQL_DATABASE: magento
MYSQL_PASSWORD: magento
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: magento
啟動服務
直接執行./docker-run.sh可以看到有什麼參數需要帶入
$ ./docker-run.sh
usage: <span class="token operator"><</span>docker-run<span class="token operator">></span> <span class="token operator"><</span>local<span class="token operator">|</span>dev<span class="token operator">|</span>staging<span class="token operator">|</span>production<span class="token operator">></span> options:<span class="token operator"><</span>s:<span class="token operator">|</span>c<span class="token operator">|</span>d<span class="token operator">></span>
- 必選參數
<local|dev|staging|production>
為環境 會依照輸入值去設定變數值:- local 會對應到 config/docker-local-env.sh
- dev 會對應到 config/docker-dev-env.sh
- 以此類推
- 可選參數
- -d: deamon背景模式, 如同docker-compose up -d
- -c: 使用當下的commit hash當作subdomain
- -s: 自訂義subdomain
例如:
$ ./docker-run.sh local -d
$ ./docker-run.sh dev -c
$ ./docker-run.sh dev -s thisisyoursubdomain -d
(別忘了把domain跟subdomain加入你的hosts或是網站的DNS服務中)
執行
執行後會看到如下
networks:
network-back:
external: <span class="token boolean">true</span>
traefik_default:
external: <span class="token boolean">true</span>
services:
cli:
command: <span class="token function">bash</span> -c <span class="token string">"php bin/magento setup:store-config:set --base-url='http://your-domain.name/';
php bin/magento setup:store-config:set --base-url-secure='https://your-domain.name/';
php bin/magento cron:install; rm -rf /app/generated/*; tail -f /dev/null"</span>
image: magento/magento-cloud-docker-php:7.2-cli-1.1
links:
- mysql
networks:
network-back: null
volumes:
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/src:/app:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/magento/.user.ini:/app/pub/.user.ini:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/magento/env.php:/app/app/etc/env.php:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/magento/config.php:/app/app/etc/config.php:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:ro
- /home/glenn/snap/docker/423/.composer/cache:/root/.composer/cache:delegated
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/composer/auth.json:/root/.composer/auth.json:delegated
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/composer/bin/composer:/usr/local/bin/composer:delegated
fpm:
environment:
MAGENTO_RUN_MODE: developer
UPDATE_UID_GID: <span class="token string">"true"</span>
VERBOSE: <span class="token string">"true"</span>
image: magento/magento-cloud-docker-php:7.2-fpm-1.1
links:
- mysql
networks:
network-back: null
volumes:
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/src:/app:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/magento/.user.ini:/app/pub/.user.ini:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/magento/env.php:/app/app/etc/env.php:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/magento/config.php:/app/app/etc/config.php:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:ro
- /home/glenn/snap/docker/423/.composer/cache:/root/.composer/cache:delegated
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/composer/auth.json:/root/.composer/auth.json:delegated
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/composer/bin/composer:/usr/local/bin/composer:delegated
mysql:
environment:
MYSQL_DATABASE: magento
MYSQL_PASSWORD: magento
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: magento
hostname: m2-traefikdocker.mysql
image: mysql:5.7
labels:
traefik.docker.network: traefik_default
traefik.enable: <span class="token string">"true"</span>
traefik.tcp.routers.m2-traefikdocker.entrypoints: mysql
traefik.tcp.routers.m2-traefikdocker.rule: HostSNI<span class="token punctuation">(</span><span class="token variable"><span class="token variable">`</span>*<span class="token variable">`</span></span><span class="token punctuation">)</span>
networks:
network-back: null
traefik_default: null
volumes:
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/var/mysql/dbdata:/var/lib/mysql:rw
web:
environment:
VERBOSE: <span class="token string">"true"</span>
image: magento/magento-cloud-docker-nginx:latest
labels:
traefik.docker.network: traefik_default
traefik.enable: <span class="token string">"true"</span>
traefik.http.routers.m2-traefikdocker.rule: Host<span class="token punctuation">(</span><span class="token variable"><span class="token variable">`</span>your-domain.name<span class="token variable">`</span></span><span class="token punctuation">)</span>
traefik.http.services.m2-traefikdocker.loadbalancer.server.port: <span class="token string">'80'</span>
links:
- fpm
networks:
network-back: null
traefik_default: null
volumes:
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/src:/app:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/magento/.user.ini:/app/pub/.user.ini:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/magento/env.php:/app/app/etc/env.php:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/magento/config.php:/app/app/etc/config.php:rw
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/config/dev/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:ro
- /home/glenn/snap/docker/423/.composer/cache:/root/.composer/cache:delegated
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/composer/auth.json:/root/.composer/auth.json:delegated
- /home/glenn/src/AstralWebInc/Projects/m2-traefikDocker/composer/bin/composer:/usr/local/bin/composer:delegated
version: <span class="token string">'3.0'</span>
---
<span class="token comment"># =============================================================================== </span>
<span class="token comment">#containar prefix: m2-traefikdocker </span>
<span class="token comment">#host: your-domain.name </span>
<span class="token comment">#database host: m2-traefikdocker.mysql </span>
<span class="token comment"># ===============================================================================</span>
Please <span class="token function">make</span> sure the config, Do you want to run the docker according to the above config? <span class="token punctuation">[</span>y/n<span class="token punctuation">]</span> <span class="token keyword">:</span>
重要資訊
最重要的是最後三行
<span class="token comment">#containar prefix: m2-traefikdocker </span>
<span class="token comment">#host: your-domain.name </span>
<span class="token comment">#database host: m2-traefikdocker.mysql</span>
- containar prefix: 此站台docker containar name的prefix, 可以透過
$ docker ps -a
看到 - host: 站台url
- database host: db透過tcp/ip連線方式的host, port 3306
要關掉某一站台全部服務請執行
$ docker-compose -p <span class="token operator"><</span>yourprefix<span class="token operator">></span> down
以上資訊都會紀錄在info.txt
啟動網站
從原來專案安裝
如果有原來m2專案的話, 直接把整個專案丟進src中即可, 並確認config/<local|dev|staging|production>
四個資料夾中, magento裡面的config.php還有env.php
啟動服務後再把原來專案的db匯入
最後重新啟動服務
第一次安裝
1. 下載magento2
- 在src外執行
composer create-project --repository-url<span class="token operator">=</span>https://repo.magento.com/ magento/project-community-edition<span class="token operator">=</span>2.3.3 src
- 或是把已有的composer.json丟至src內執行
composer <span class="token function">install</span>
2. 安裝
<span class="token function">chmod</span> 755 bin/magento <span class="token punctuation">;</span>
BASE_URL<span class="token operator">=</span><span class="token string">"<span class="token variable">${DOMAINNAME}</span>"</span><span class="token punctuation">;</span>
ADMIN_EMAIL<span class="token operator">=</span><span class="token string">"[email protected]"</span><span class="token punctuation">;</span>
bin/magento setup:install \
--db-host<span class="token operator">=</span>mysql \
--db-name<span class="token operator">=</span>magento \
--db-user<span class="token operator">=</span>magento \
--db-password<span class="token operator">=</span>magento \
--base-url<span class="token operator">=</span>http://<span class="token variable">$BASE_URL</span>/ \
--backend-frontname<span class="token operator">=</span>admin \
--admin-firstname<span class="token operator">=</span>magento \
--admin-lastname<span class="token operator">=</span>admin \
--admin-email<span class="token operator">=</span><span class="token variable">$ADMIN_EMAIL</span> \
--admin-user<span class="token operator">=</span>admin \
--admin-password<span class="token operator">=</span>admin123 \
--language<span class="token operator">=</span>en_US \
--currency<span class="token operator">=</span>TWD \
--timezone<span class="token operator">=</span>Asia/Taipei \
--use-rewrites<span class="token operator">=</span>1<span class="token punctuation">;</span>
3. 重新啟動
檢查info.txt中的執行或關閉命令
e.g
$ docker-compose -p <span class="token operator"><</span>yourprefix<span class="token operator">></span> down
$ docker-compose -p <span class="token operator"><</span>yourprefix<span class="token operator">></span> up -d
同時多站
要再起一台直接整個資料夾cp到另一個資料夾, 通常此時cp完都已經有了magento2跟db資料
正常來說只要 ./docker-run.sh 加上參數<-c | -s>設定subdomain就好
以上為本次的教學,喜歡歐斯瑞新知分享的讀者們,別忘了追蹤歐斯瑞臉書粉絲團、Instagram,以及訂閱我們的電子報唷!
我要留言