Build a development environment with phpstorm + docker

Time:2022-6-20

premise

  • The basic operation of docker will be understood.
  • Be able to write dockerfile.
  • Docker compose.

The above skills are the premise of the following contents.

PHP developers will gradually become full stack

The integrated environment can achieve the goal of writing code quickly, but it is not good for “slowly becoming a full stack”! Instead of using docker, it will become a full stack. Just using tools that have not been used before will enable you to learn new knowledge, so as to improve yourself and master new skills.

Step 1: customize a PHP image using dockerfile

The best environment for taking over maintenance projects and writing maintenance code is to be consistent with the server, so docker can easily achieve this.

For example,To maintain a project developed with php5.6.4 and thinkphp5.0, you can use php5.6.4 image as the basis to install the extensions required by thinkphp5.0. Write the following dockerfile:

FROM  php:5.6.4-fpm
RUN apt-get update && apt-get install libssl-dev -y \
    && pecl install redis-2.2.5  xdebug-2.5.5 \
    && docker-php-ext-install pdo_mysql mbstring ftp

You can run the following command to generate a custom image:

docker build -t php564:v1 .

Step 2: use docker compose to combine multiple images

Docker compose can orchestrate multiple containers to cooperate with each other to complete a task and make the project run. Nginx is also required, so a new directory is created to specifically write docker compose configuration:

PS E:\docker\workEnv> pwd

Path                  
----                  
E:\docker\workEnv  

2.1 build directory structure

Create a file: docker compose yml
Two directories: nginx and php564 are used to store some things related to containers
The directory structure is as follows:

PS E:\docker\workEnv> ls
Directory: E:\docker\workEnv
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        27/04/2021     13:20                nginx
d-----        18/05/2021     10:15                php564
-a----        18/05/2021     10:47            560 docker-compose.yml
PS E:\docker\workEnv> 

2.2 move the customized PHP dockerfile to php564 directory

Other colleagues may also need to maintain this project. Put the dockerfile in this directory. If other colleagues also use docker, they can directly send the current directory (workenv in my case) to him. He can compile directly through docker compose.

In the php564 directory, create two more directories: conf.d and Xdebug, which are used to map configuration and record Xdebug logs
docker-compose. YML will come in handy.

2.3 prepare docker compose yml

version: '3'

services: 
    web:
        image: nginx
        ports: 
            - "80:80"
            - "443:443"
            - "1212:1212"
        depends_on: 
            - php564
        volumes: 
            #Nginx directory maps to nginx container configuration directory
            - ./nginx:/etc/nginx/conf.d
            #Local project directory mapped to nginx container working directory
            - E:/www:/usr/share/nginx

    php564:
        build: ./php564
        volumes: 
            #The local project directory maps to the PHP container working directory
            - E:/www:/var/www
            #PHP configuration file
            - ./php564/conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
            #Xdebug debug log
            - ./php564/xdebug:/xdebug
        ports:
            - "9000:9000"

2.4 writing nginx configuration

Go to the nginx directory and create a configuration file corresponding to the project. I created olderp conf:

# oldERP.conf
server {
    listen       1212;
    server_name  location;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location /erp/ {
        alias  /usr/share/nginx/olderp/;
        if (!-e $request_filename){
            rewrite  ^/erp/index.php/(.*)$  /index.php?s=/$1  last;
        }
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
       #Write php564 here and define it in docker compose
       fastcgi_pass   php564:9000;
       fastcgi_index  index.php;
       #Here, you should also specify the project path in the php564 container
       fastcgi_param SCRIPT_FILENAME /var/www/olderp$fastcgi_script_name;
       include fastcgi_params;
    }
}

2.5 compile docker compose yml

To the workenv directory, execute:

docker-compose up -d

Then you can look at the container:

PS E:\docker\workEnv> docker-compose top
workenv_php564_1
  UID       PID    PPID    C   STIME   TTY     TIME                              CMD
-------------------------------------------------------------------------------------------------------------
root       12463   12442   0   03:03   ?     00:00:01   php-fpm: master process (/usr/local/etc/php-fpm.conf)
www-data   12518   12463   0   03:03   ?     00:00:02   php-fpm: pool www
www-data   12519   12463   0   03:03   ?     00:00:02   php-fpm: pool www
root       12529   12442   0   03:03   ?     00:00:00   bash

workenv_web_1
 UID     PID    PPID    C   STIME   TTY     TIME                        CMD
-----------------------------------------------------------------------------------------------
root    12275   12254   0   02:50   ?     00:00:00   nginx: master process nginx -g daemon off;
uuidd   12352   12275   0   02:50   ?     00:00:00   nginx: worker process
PS E:\docker\workEnv> 

Step 3: configure Xdebug

You can write PHP code without Xdebug, but when the program needs debugging, it may be var everywhere_ Break point codes such as dump and die

This embarrassment can be avoided by using Xdebug. It can be debugged step by step. It can listen to input parameters and change input parameters. Therefore, some companies include whether they will use Xdebug in their skill rating for a reason.

To use Xdebug, you need these two steps:

  1. Configure PHP to support Xdebug
  2. Configure phpstrom remote debugging Xdebug

3.1 configure PHP to support Xdebug

You have customized the installed Xdebug of the PHP image. However, you need to configure the following parameters before writing docker compose When creating the YML file, the configuration mapping is done:

#Omit others
#PHP configuration file
- ./php564/conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
#Omit others

So we go to the php564/conf.d directory to write php ini:

extension=redis.so
zend_extension=xdebug.so
xdebug. Idekey=phpstorm \ied keyword
xdebug.remote_autostart=on
xdebug.remote_enable=on
xdebug.remote_port=9100
xdebug. remote_ Host=192.168.2.159 \local IP
xdebug.remote_log=/xdebug/debug.log # Xdebug log
memory_limit=1024M

After completing the above configuration, in the workenv directory, executedocker-compose restartRestart docker:

PS E:\docker\workEnv> docker-compose restart
Restarting workenv_web_1    ... done
Restarting workenv_php564_1 ... done
PS E:\docker\workEnv> 

3.2 configure phpstrom remote debugging Xdebug

Different versions may have different interfaces, but the ideas are the same:

  • Turn on settings
  • Find Languages & frameworks
  • Find PHP and set the Xdebug port of the debug column
  • In PHP, add a server, configure the mapping of local directory and container directory
    My computer is as follows:
    Build a development environment with phpstorm + docker
    Build a development environment with phpstorm + docker
    Build a development environment with phpstorm + docker

After setting the above, you also need to configure the run:
Build a development environment with phpstorm + docker
Build a development environment with phpstorm + docker

So far, my environment is OK.

3.3 break points, using Xdebug

Build a development environment with phpstorm + docker

Concluding remarks

If this blog can give you some help, then my goal has been achieved!

In the process of trying to use docker, I read the document several times. Finally, I can customize a simple dockerfile. Using docker compose is trying to read the document again and again. When I practice to a certain extent, my previous incomprehension becomes reasonable. If I don’t understand it, I will read the document more and try more!

reference material

Docker – from introduction to practice

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Less is more.

Recommended Today

Flowable 6.6.0 remove the built-in login permission

​ 1. find the project Maven directory ​ 2. create the same package name and class name in your own project package org. flowable. ui. modeler. conf; Override modelersecurityconfiguration package org. flowable. ui. modeler. conf; ​ flowable Spring Boot This work adoptsCC agreement, reprint must indicate the author and the link to this article