基于Docker統一PHP開發環境
背景
- 團隊內每個開發人員PHP版本不一致
- 本地開發環境與生產環境不一致,環境不一致易導致生產問題
- 每次新人過來安裝PHP環境耗時比較長且不可控
目標
- 所有開發人員環境保持一致
- 開發環境與生產環境保持一致
- 能快速安裝輕量級的開發環境
思路
- 將標準PHP環境打包靜docker鏡像中,所有開發成員代碼運行在容器里面,本地不需要安裝開發環境(除IDE工具外)
- 容器里面提供sshd服務,使開發者能夠通過ssh登陸并傳輸檔案(類似輕量級虛擬機,在容器啟動后利用sftp遠程同步檔案比較有用)
構造環境鏡像
-
版本
linux:alpine3.13
PHP: 7.4 -
準備檔案(在后面國建鏡像有用)
nginx主組態檔:nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
#tcp_nopush on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
include /etc/nginx/conf.d/*.conf;
}
鏡像入口檔案(用于啟動鏡像服務): entrypoint.sh
#!/bin/sh
/usr/sbin/sshd -D &
nginx &
php-fpm
- 鏡像Dockerfile
在有了上面兩個檔案后,我們就可以開始構建我們的環境鏡像了
# 采用基于alpine的php-fpm鏡像(alpine為一個輕量級的linux鏡像)
FROM amd64/php:7.4-fpm-alpine3.13
# 復制PHP組態檔
RUN cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
# 由于倉庫鏡像網路問題,換成阿里云資源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 安裝php擴展(可以根據自己的需要安裝所需PHP擴展)
# install-php-extensions使用檔案:
# https://github.com/mlocati/docker-php-extension-installer
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions swoole
RUN install-php-extensions mongodb
RUN install-php-extensions redis
RUN install-php-extensions rdkafka
RUN install-php-extensions bcmath
RUN install-php-extensions pdo_mysql
# 安裝composer
RUN php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
RUN php composer-setup.php
RUN php -r "unlink('composer-setup.php');"
RUN mv composer.phar /usr/local/bin/composer
# alpine利用apk進行系統包管理
# 更新apk庫并安裝時區資料庫、sshd服務、nginx
RUN apk update && apk add tzdata openssh nginx
# 配置時區(alpine默認時區為0時區,與北京時間相差8小時,需要調整為Asia/Shanghai)
ENV TIME_ZONE=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_ZONE > /etc/timezone
# 安裝openssh,容器啟動后可以通過ssh登陸或傳輸檔案
# 初始賬號密碼為root:123456
RUN sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config &&\
ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key &&\
ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key &&\
ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key &&\
ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key &&\
echo "root:123456" | chpasswd
# 配置nginx主組態檔
COPY nginx.conf /etc/nginx/nginx.conf
COPY entrypoint.sh /data/entrypoint.sh
# 安裝swoft開發工具(swoft框架的開發工具,如果沒有用到則不需要安裝)
RUN wget https://github.com/swoft-cloud/swoft-cli/releases/download/v0.2.1/swoftcli.phar &&\
mv swoftcli.phar /usr/local/bin/swoftcli &&\
chmod a+x /usr/local/bin/swoftcli
# 設定默認作業目錄
WORKDIR /data/web/
# 暴露埠
EXPOSE 22
EXPOSE 80
EXPOSE 9000
# 啟動sshd、nginx、fpm
CMD ["sh","/data/entrypoint.sh"]
構建鏡像
docker build -t jeanslin/php7.4-dev .
查看構建后的鏡像

運行環境
- 啟動容器
此處可以將存放代碼的目錄掛載到容器里面,這樣宿主機和容器的代碼就是相同的
docker run -d
-p 10080:80 -p 10022:22 -p 19000:9000
-v /Users/it/project:/data/web/project
jeanslin/php7.4-dev
- 利用ssh登陸容器
由于我們把容器的22埠映射到宿主機的10022埠,通過10022埠就可以登陸容器里面

查看已啟動的服務行程,可以看到已啟動了php-fpm、nginx、sshd服務

- 配置后端nginx檔案:api.conf
由于容器只對外開放了80埠,為了區分不同的服務,我們可以在宿主機/etc/hosts檔案配置開發域名來映射到容器的不同服務,此處將api.dev.com映射到127.0.0.1
server {
listen 80;
server_name api.dev.com;
root /data/web/;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $http_host;
fastcgi_ignore_client_abort on;
}
}
我們在容器的/data/web/寫一個phpinfo.php的腳本檔案,用來除錯服務是否正常
<?php
phpinfo();
由于容器的80埠是掛載到宿主機的10080埠,我們通過該埠訪問服務
訪問地址為:http://api.dev.com:10080/phpinfo.php

- 配置前端nginx檔案:web.conf
在宿主機/etc/hosts檔案將web.dev.com映射到127.0.0.1
此處為了解決前端跨域問題,將/api/開頭的路徑轉發到api.dev.com
server {
listen 80;
server_name web.dev.com;
root /data/web/;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.html;
}
location ^~/api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://@back/;
}
}
upstream @back{
server api.dev.com;
}
我們在容器/data/web/目錄創建一個index.html檔案,內容如下
<html>
<body>
nice to meet you!
</body>
</html>
由于容器的80埠是掛載到宿主機的10080埠,我們通過該埠訪問服務
訪問地址為:http://web.dev.com:10080/index.html

至此,開發者本地nginx+php-fpm的開發環境已經準備完成,開發者通過拉取該鏡像使用即可(開發者本地需要安裝docker),本地無需安裝任何環境軟體,
遠程開發
除上面的場景外,還可以用于以下場景
-
我們還可以在一臺開發機上為每個開發者創建一個容器,然后開發者可以通過ssh服務遠程登錄到容器里面,這樣開發者本地docker也不需要安裝
-
在一些swoole框架,比如swoft需要讀取檔案注解的,如果通過掛載的方式把代碼掛到容器里面,在代碼變更熱重啟時會很慢,影響開發體驗,這時可以通過ssh自帶sftp服務直接將代碼直接同步到容器里面,這樣容器熱重啟會快許多
以下我們基于swoft框架,用phpstorm演示一下如何遠程開發容器里面的代碼:
入口為:tools > Deployment > Configuration
配置connection,此處容器的22埠掛載到宿主機的10022埠,我們通過宿主機的10022連接容器

我們添加一個Mappings,將本地的檔案/Users/linjunda/it/tme/doc/doc_service同步到容器的/data/web/

tools > Deployment > Automatic upload 選擇為always,該選項表示如果目錄代碼發生變更則自動同步到遠程服務器
我們先執行一次全量的同步,入口為:tools > Deployment > Sync with deployed to

控制臺顯示檔案同步完成資訊

現在我們可以在容器里面啟動這個swoft服務的熱重啟監聽

此處容器里面doc_service服務的監聽為1007,我們通過nginx配置進行埠轉發
server {
listen 80;
server_name doc.dev.com;
root /data/web/;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://@docBack/;
}
}
upstream @docBack{
server localhost:1007;
}
我們寫一個TestController
/**
* @Controller(prefix="test")
*/
class TestController
{
/**
* @RequestMapping(route="test")
*/
public function test(Response $response)
{
return $response->withContent("hello world");
}
}
此處我們將doc.dev.com域名的所有請求轉發到1007埠下面,通過宿主機訪問如下

現在我們再IDE修改腳本檔案后就會遠程同步到容器里面,容器里面的swoft熱重啟服務監聽到檔案變化時會觸發服務重啟,這樣我們就相當于遠程在除錯容器里面的代碼了,
接下來我們在本地IDE把回傳內容改為“nice to meet you!”,再訪問原來的地址,內容如下:

以上就是本次的一些分享,如果轉載請標明遠處,覺得對你有幫助的話,請點個贊吧 _
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/289230.html
標籤:其他
上一篇:linux服務器實作百萬并發
下一篇:ansible分離部署lamp
