/setup-php-develop-environment-with-docker

不使用WAMPP,不使用虚拟机,优雅方便快捷地在windows的docker下搭建多套php开发环境.

Primary LanguageDockerfile

将php开发环境部署在docker内

不使用WAMPP,不使用虚拟机,优雅方便快捷地在windows的docker下搭建多套php开发环境.

安装并运行php container

建立一个Dockerfile,安装必要依赖

FROM php:5.6-fpm
LABEL maintainer="Xavier-Lam <Lam.Xavier@hotmail.com>"
LABEL description="加装xdebug,redis,memcached,composer,psysh"

# 安装依赖
RUN apt-get update
RUN apt-get install -y wget git libmemcached-dev zlib1g-dev
RUN apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev

# 安装composer
RUN wget https://getcomposer.org/installer -O /tmp/composer-setup.php
RUN php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN composer config -g repo.packagist composer https://packagist.phpcomposer.com

# 用composer安装必要包
RUN composer g require psy/psysh:@stable

# 将composer的bin目录加入环境变量
ENV PATH "$PATH:/root/.composer/vendor/bin/"

# 安装拓展
RUN docker-php-ext-install pdo pdo_mysql 
RUN docker-php-ext-install -j$(nproc) iconv
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd

# 安装pecl拓展
RUN pecl install xdebug-2.5.4
RUN pecl install redis
RUN pecl install memcached-2.2.0
RUN pecl install memcache-2.2.7

# 开启pecl拓展
RUN docker-php-ext-enable redis memcached xdebug memcache

# 配置xdebug
RUN echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

# 设置工作路径
WORKDIR /data

构建并运行容器

docker build . -t php:5.6-dev
docker run -d -p 9000:9000 -v D:/projects:/data --name php-5.6 --restart unless-stopped php:5.6-dev

如在后期需要额外安装其他拓展或修改镜像,只需FROM php:5.6-dev构建即可

安装并运行nginx container

docker pull nginx
docker run -v D:/projects:/data -v D:/conf.d/:/etc/nginx/conf.d/ --name nginx -d -p 10000:80 --restart unless-stopped nginx
docker exec -t nginx nginx -s reload

链接数据库cache等

以redis为例

docker pull redis
docker run --name redis -p 6379:6379 --restart unless-stopped redis

构建网络连接容器

docker network create php-5.6
docker network connect php-5.6 nginx
docker network connect php-5.6 redis
docker network connect php-5.6 php-5.6

在php的config中,redis的host直接用redis代替,nginx中fpm的host直接用php-5.6代替

使用IIS反代以便他人访问

用系统自带的IIS,也不用额外安装Apache了,优雅

参见这篇文章

https://tecadmin.net/set-up-reverse-proxy-using-iis/

保留反向代理头部(不然host会掉)

https://stackoverflow.com/a/14842856/4719118

对于反代的 X_FORWARDED_FOR 带有端口的问题 在ARR代理配置中取消勾选 Include TCP port from client IP

https://docs.microsoft.com/en-us/iis/extensions/configuring-application-request-routing-arr/creating-a-forward-proxy-using-application-request-routing

使用

以cli模式运行

docker exec -it php-5.6 php /path/to/your/file/in/your/docker

xdebug调试

还没解决T T

composer

docker exec -it php-5.6 composer install -d /path/to/your/project/

psysh

docker exec -it php-5.6 psysh

可能遇到的问题

挂载路径为空

可能是修改过开机密码等登陆凭据,在docker GUI的settings > Shared Drives 中Reset credentials 重新选择Shared 并Apply

https://stackoverflow.com/questions/50018812/docker-for-windows-volumes-are-empty/50348492#50348492

我感觉我每次reboot都要重新执行这个操作...

参考链接

https://www.pascallandau.com/blog/php-php-fpm-and-nginx-on-docker-in-windows-10/

https://hub.docker.com/_/php