1.创建文件目录在Workspace文件夹下创建如下目录;├─etc│├─nginx││└─conf.d│├─php││└─conf.d│└─php-fpm.d└─web2.打开C:\\docker\\etc\\nginx\\conf.d\\defa...
1.创建文件目录
在Workspace文件夹下创建如下目录;
├─etc
│ ├─nginx
│ │ └─conf.d
│ ├─php
│ │ └─conf.d
│ └─php-fpm.d
└─web
2.打开C:\\docker\\etc\\nginx\\conf.d\\default.conf,添加如下
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
其中fastcgi_pass对应的是php容器名称。
3.构建php镜像或拉取已有PHP程序
此处以这个Dockerfile为例:
FROM php:7.3.8-fpm
RUN apt-get update \
&& export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS" \
&& apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev libmagickwand-dev libmcrypt-dev libmemcached-dev zlib1g-dev \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) bcmath calendar exif gettext sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm iconv gd \
&& pecl install imagick-3.4.4 mcrypt-1.0.2 memcached-3.1.3 redis-5.0.2 xdebug-2.7.2 swoole-4.4.3\
&& docker-php-ext-enable imagick mcrypt memcached redis xdebug swoole \
&& docker-php-ext-configure opcache --enable-opcache && docker-php-ext-install opcache
LABEL Description="PHP 7.3.8 开发环境镜像. "
# xdebug 扩展(如果已有镜像不存在xdebug,则可以进入容器后安装)
#RUN pecl install xdebug-2.7.2 && docker-php-ext-enable xdebug
以演示为例构建镜像:
docker build -t php:7.3-dev .
4.复制php镜像配置文件到本地
docker run --name tmp-my-php-fpm -d php:7.3-dev
docker cp tmp-my-php-fpm:/usr/local/etc d:\\docker
docker rm -f tmp-my-php-fpm
5.修改php相关配置文件
1.进入d:/docker/etc/php 下,将 php.ini-development 复制改名为 php.ini,配置内容根据自己情况修改即可。
2.修改 xdebug 的配置文件,d:/docker/etc/php/conf.d/docker-php-ext-xdebug.ini,加入如下内容
xdebug.remote_enable = On
xdebug.remote_handler = dbgp
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9001
xdebug.remote_log = /var/log/php/xdebug.log
xdebug.idekey = PHPSTOR
6.启动php服务(此处其它镜像按需加载)
docker run --name php-fpm-dev -v d:\\docker\\web:/var/www/html -v d:\\docker\\etc:/usr/local/etc -v d:\\docker\\log\\php:/var/log/php -d php:7.3-dev
7.启动nginx服务
docker run --name nginx-dev -p 80:80 --link php-fpm-dev:php-fpm -v d:\\docker\\web:/usr/share/nginx/html -v d:\\docker\\etc\\nginx\\conf.d\\default.conf:/etc/nginx/conf.d/default.conf -v d:\\docker\\log\\nginx:/var/log/nginx -d nginx
其中php-fpm-dev为php容器名称;php-fpm是nginx连接用的php-fpm:9000
至此,开发环境已经搭建完毕,接下来就可以用vs-code打开本地存档,就可以二次开发了。
全文详见:http://xpxw.com/?id=145