Docker built using PHP development environment

Docker built using PHP development environment (Mac, Docker, Nginx, PHP-FPM, XDebug, PHPStorm, VSCode).

Because of the recent change sake computers need to re-deploy about local development environment, a virtual machine program before adoption, and take up disk space is too great, then the point where distressed SSD, so this use of Docker's program to deploy.
Principles of this deployment, my idea is to try to use the official mirror to achieve, try not to change or little change image. Because it is a local development environment, so would like to try some fresh, try to use high version of the software.
From the results, the hope that they will also have the students play environment need to build some help small step on some of the pit, if you can get one by following the steps in this article that would be ideal.

First is about the environment:

  • Docker 18.09.2
  • Nginx 1.17.1
  • PHP 7.3.7
  • XDebug 2.7.2
  • PhpStorm 2019.1.3
  • VSCode 1.36.1

1, Docker installation

Here it directly in the official website to download Docker Docker for Mac is like, directly follow the prompts, not go into details here.

2, install Nginx

Address: https: //hub.docker.com/_/nginx
direct execution docker pull nginxto pull in the mirror;
we can see some official documentation provided by the above address, we can follow the operation, I personally alternative was the first the entire configuration directory nginx copy to the local copy, then at run time to bind to the configuration directory nginx directory container, so modify the configuration is more convenient.

The nginx configuration directory to a local copy:

$ docker run --name tmp-nginx -d nginx
$ docker cp tmp-nginx:/etc/nginx /Users/yourname/Workspace/etc/nginx
$ docker rm -f tmp-nginx

Description: / Users / yourname / Workspace / etc, this is my personal working directory, to the time according to the situation into their own on the line; command simply nginx container start running in the background, the configuration directory terminate and copy out after delete container.

I may try it and see the effect:

$ docker run --name run-nginx -d -p 80:80 -v /Users/yourname/Workspace/www:/usr/share/nginx/html:ro nginx

Description: -v parameter binding of a local directory to a container nginx web directory, there is no binding configuration directory, you can create a hello.html in the web directory browser to http: // localhost / hello .html see the effect, after which you can you can delete the vessel, complete configuration change behind us start it again.
Delete method:

$ docker rm -f run-nginx

3, installed php-fpm

Address: https: //hub.docker.com/_/php
because here we need to install some PHP extensions used in the development, so the best way is to generate our own image according to Dockerfile, the following is my Dockerfile, everyone can refer to, you do not need to be cut according to the extension, otherwise the generated image will be relatively large.

Dockerfile reads as follows:

# 从官方基础版本构建
FROM php:7.3.7-fpm
# 官方版本默认安装扩展: 
# Core, ctype, curl
# date, dom
# fileinfo, filter, ftp
# hash
# iconv
# json
# libxml
# mbstring, mysqlnd
# openssl
# pcre, PDO, pdo_sqlite, Phar, posix
# readline, Reflection, session, SimpleXML, sodium, SPL, sqlite3, standard
# tokenizer
# xml, xmlreader, xmlwriter
# zlib

# 更新为国内镜像
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \
    && echo 'deb http://mirrors.163.com/debian/ stretch main non-free contrib' > /etc/apt/sources.list \
    && echo 'deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib' >> /etc/apt/sources.list \
    && echo 'deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib' >> /etc/apt/sources.list \
    && apt-get update

# bcmath, calendar, exif, gettext, sockets, dba, 
# mysqli, pcntl, pdo_mysql, shmop, sysvmsg, sysvsem, sysvshm 扩展
RUN docker-php-ext-install -j$(nproc) bcmath calendar exif gettext sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm iconv

# GD 扩展
RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev \
    && rm -r /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) gd

# imagick 扩展
RUN export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS" \
    && apt-get install -y --no-install-recommends libmagickwand-dev \
    && rm -r /var/lib/apt/lists/* \
    && pecl install imagick-3.4.4 \
    && docker-php-ext-enable imagick

# mcrypt 扩展 
RUN apt-get install -y --no-install-recommends libmcrypt-dev \
    && rm -r /var/lib/apt/lists/* \
    && pecl install mcrypt-1.0.2 \
    && docker-php-ext-enable mcrypt

# Memcached 扩展 
RUN apt-get install -y --no-install-recommends libmemcached-dev zlib1g-dev \
    && rm -r /var/lib/apt/lists/* \
    && pecl install memcached-3.1.3 \
    && docker-php-ext-enable memcached

# redis 扩展
RUN pecl install redis-5.0.0 && docker-php-ext-enable redis

# opcache 扩展 
RUN docker-php-ext-configure opcache --enable-opcache && docker-php-ext-install opcache

# xdebug 扩展
RUN pecl install xdebug-2.7.2 && docker-php-ext-enable xdebug

# swoole 扩展
RUN pecl install swoole-4.4.0 && docker-php-ext-enable swoole

# 镜像信息
LABEL Author="Stone"
LABEL Version="2019.7"
LABEL Description="PHP 7.3.7 开发环境镜像. 

Description: This content https://www.jianshu.com/p/20fcca06e27e Dockerfile I made reference to the author's, and made some adjustments, you can increase or decrease according to their own situation, because it is a development environment it is best to keep xdebug, Let's back will be used;
Here is the RUN concatenated Dockerfile, so the resulting image can be smaller, but there are still 636M, official php: 7.3.7-fpm mirroring is 371M, if you want to be optimized from the official Alpine provides image creation, we here briefly rude.

FROM php:7.3.7-fpm

RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \
    && echo 'deb http://mirrors.163.com/debian/ stretch main non-free contrib' > /etc/apt/sources.list \
    && echo 'deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib' >> /etc/apt/sources.list \
    && echo 'deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib' >> /etc/apt/sources.list \
    && 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.0 xdebug-2.7.2 swoole-4.4.0\
    && docker-php-ext-enable imagick mcrypt memcached redis xdebug swoole \
    && docker-php-ext-configure opcache --enable-opcache && docker-php-ext-install opcache

LABEL Author="Stone"
LABEL Version="2019.7"
LABEL Description="PHP 7.3.7 开发环境镜像. "

Say something to step on pit: apt-get source is best replaced by domestic sources, otherwise it may be stuck in the building when the mirror; official container because the default case is based on Debian, find the source of the time many of which are outdated , such as the current Debian, code-named stretch, but used jessie package that certainly will complain, "E: Unable to correct problems, you have held broken packages."; we follow directly above me Dockerfile generated image can be, test.

Dockerfile executed in the directory where:

docker build -t my-php-fpm:2019.7 .

-t parameter setting image name and label, according to its own name, after the image is created, like the above we can copy the nginx configuration as the php configuration is also related to the local copy.

$ docker run --name tmp-my-php-fpm -d my-php-fpm:2019.7
$ docker cp tmp-my-php-fpm:/usr/local/etc /Users/yourname/Workspace/etc/php
$ docker rm -f tmp-my-php-fpm

4, modify nginx, php-fpm, xdebug profile

Nginx modify the configuration file, open the / Users / yourname / Workspace / etc / nginx / default.conf, add the following contents:

 location ~ \.php$ {
        fastcgi_pass   php-fpm-container: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;
    }

Additions which have a need to explain, "php-fpm-container" This is an alias for php-fpm container of our own creation, in particular in the run time specified, after which we will mention.

Modify the php configuration file, go to the next / Users / yourname / Workspace / etc / php, copy the php.ini-development renamed php.ini, you can modify the contents of the configuration according to their own circumstances.
Xdebug modified configuration file, / Users / yourname / Workspace / etc / php / conf.d / docker-php-ext-xdebug.ini, the following was added

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

The main set is turned over xdebug remote debugging mode, because php-fpm uses port 9000, so here we xdebug port instead of 9001; "host.docker.internal" docker 18.03 is newly added, can be resolved to obtain the host ip address, so do not write the dead ip address.

5, start php-fpm, nginx container

$ docker run --name run-my-php-fpm \
-v /Users/yourname/Workspace/www:/var/www/html \
-v /Users/yourname/Workspace/etc/php:/usr/local/etc \
-v /Users/yourname/Workspace/log/php:/var/log/php \
-d my-php-fpm:2019.7
$ docker run --name run-nginx \
-p 80:80 \
--link run-my-php-fpm:php-fpm-container \
-v /Users/yourname/Workspace/www:/usr/share/nginx/html \
-v /Users/yourname/Workspace/etc/nginx:/etc/nginx \
-v /Users/yourname/Workspace/log/nginx:/var/log/nginx \
-d nginx

Note: I will configuration directory, log directory and web directories were bound, easy to modify in a local environment. Remember nginx mentioned above has a configuration to fill in the "php-fpm-container" it? Actually specified here, connecting the two containers, and containers to php-fpm played a few names, aliases access configuration pass.

Phpinfo.php create a file, php output information in / Users / yourname / Workspace / www in, http by: view //localhost/phpinfo.php access.

6, install the Chrome browser plug-xdebug helper

May be a wall, resolve on their own.

7, PhpStorm debugging environment configuration

Establish / Users / yourname / Workspace / www / xdebug / demo.php, php file inside just write php code, you can also create a new empty project by PhpStorm, but we are in position to bind and php-fpm container directory .

Menu: PhpStorm-> Preferences ... enter preferences, set according to the FIG.

Based on the graph window prompts to enter CLI Interpreter set point "+" add configuration.

Debug settings continue configuration, as shown below:

Enter the main interface, select the top right of the "Add Configuration ..."

After entering the debug configuration window select the "+", select "PHP Web Page", after the name is set to enter Servers settings, and set the Start URL: "/ xdebug / demo.php"

Into the main window, hit a few breakpoints in the program, and then click the green beetle on it.

8, VSCode debugging environment configuration

First install PHP Debug plug-in, and then open the xdebug directory created above. Shown, enter the debug panel, gear selection below PHP, launch.json then creates a file, the file is added pathMappings modify this configuration, the configuration parameter set corresponding relationship between the server path and a local path, the low version localSourceRoot and serverSourceRoot, has now been abolished. Also remember to change the port to 9001.

进入 demo.php,随意设置两个断点,然后点左侧的运行按钮,进入调试模式,接下来刷新 http://localhost/xdebug/demo.php 会返回到 VSCode 的调试窗口,左侧已经列出调试信息,可选择单步执行跟踪程序的运行。

结尾:实际上这次部署环境踩了不少坑,主要原因是网上的资料都比较陈旧,很多文章也没有后续的更新,或者方向和我不太一致,例如 hub 中直接就有 xdebug 的镜像,但是我还是想尽量用官方提供的镜像自己创建,为了避免踩坑,大家最好还是多多参考官方文档,一般都写的比较清楚了。得益于 docker 的便利,我会不定期的更新这篇文章,这也是我在文章开头设置了一个版本号的原因,希望对大家有所帮助。

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160149.htm