Docker build PHP environment? Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for a lifetime.! I’ll show you

Time:2022-5-14

Share a DNMP environment built by myself. I think it’s relatively simple. In addition, please give advice and continue to improve!
https://github.com/839891627/…

1、 Background

What docker is and why it is used will not be repeated here. Here to share with you the PHP development environment I built.
This warehouse tries to follow“Principle of simplification”。 Minimize customization and hand over customization to users.Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for a lifetime., I hope to give you room to play by yourself through simple examples. After all, everyone’s needs are different. Do big and complete things, butBloated and unable to understand

2、 Highlights

Well, self righteousHighlights

  • simple
  • Auxiliary script
  • Optimization: script acceleration
  • Organization: build the grouping of scripts, etc
  • you say…

3、 Function description

1. Description of directory structure

.
├── . Env # environment variable definition. Here you can define the required PHP / nginx / MSYQL versions and port numbers
Image - build script of dockerfile # PHP image. It mainly installs some extensions and nodejs and python required by the front end
├── config
│♪ nginx # nginx configuration folder. For the configuration file of the new project, just add nginx configuration here
│♪ PHP # here's PHP ini。  I adjusted the session save_ Handler = redis and session save_ path = " tcp://redis:6379 ". means redis (container) is used to store the session
│   └── redis. Conf # redis configuration file
Table of contents -- directory of data # persistent database
│   ├── composer
│   ├── mysql
│   └── redis
├── docker-compose.yml
Logs - logs # store nginx logs
│   └── nginx
└ - resources # pre prepared resources
    ├── Python-3.8.0.tgz
    ├── mcrypt-1.0.3.tgz
    ├── mongodb-1.6.0.tgz
    ├── node-v12.13.0-linux-x64.tar.xz
    ├── redis-5.1.1.tgz
    ├── sources.list
    ├── swoole-src-4.4.12.zip
    └── xdebug-2.8.0.tgz

2. Integrated services

  • mysql
  • php7.2/7.3
  • nginx
  • redis

3、 Use

It is strongly recommended that docker-compose.com be used before use Familiar dockeryml content

  1. Clone this project into the peer directory of your other development projects

    – laravel # for example, there is a laravel project here
    – DNMP # so you need clone here for this project

  2. Start / create container environment

Then switch to the DNMP directory and execute

docker-compose up -d
#If you use the alias of zshrc given below, it is DUP - D
  1. Add the corresponding nginx configuration file. reference resourcesconfig/nginx/conf.d/laravel.conf
  2. If the nginx configuration is adjusted, clickdnmpExecute under directory (restart nginx container)docker-compose restart nginx(if alias is used, executedr nginx
  3. You can access the project

    1. Configure hosthosts, specify the domain name127.0.0.1 laravel.test
    2. When MySQL / redis is used in the project, it needs to be configured asContainer name。 For example, inlaravel/env.phpin
    REDIS_ Host = redis # fill in redis, and the container will resolve automatically at runtime. Because docker PS checks, it is known that the redis container is called "redis"
    # REDIS_ Host = 127.0.0.1 # instead of IP
    REDIS_PASSWORD=null
    REDIS_PORT=6379
    DB_CONNECTION=mysql
    DB_ Host = MySQL # similarly, fill in MySQL
    #DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=homestead
    DB_USERNAME=homestead
    DB_PASSWORD=secret
    1. [optional] in this way, you have to configure host for each project. It’s more troublesome. You can usednsmasqCan reach a directory once and for all. Refer to the appendix below

4、 Other functions

  1. Switch PHP version

opendocker-compose.ymlwithinphp73Part, thendocker-compose up -d php73, nginx.confwithinfastcgi_pass php73:9000;that will do

  1. Install extensions
  • docker-php-ext-install -hView extensions that can be installed
  • docker-php-ext-enable -hEnable extension

    If the required extension is not available, install it through the source package. There is no explanation here

  1. implementphp、composerSuch script
    Need to enter the containerdocker exec -it php72 bash, and then switch to the corresponding project for execution

    php72Is the name of the container, in docker compose In YMLcontainer_name: php72Defined in
    If you use alias, you can directly run commands under the host project without entering the container

5、 Appendix

Auxiliary command

Please refer to.zshrc.fishto configure. Copy to your corresponding configuration

Unified setting.testDomain name to local

>Under Mac. Please try it yourself
  1. brew install dnsmasq
  2. /usr/local/etc/dnsmasq.confInside configuration address=/.test/127.0.0.1
  3. Create a dns resolver:

       sudo mkdir -v /etc/resolver
       sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'

Xdeug use

>Installed is 3 X version. The default port number is * * 9003**  
>For phpstorm configuration, please refer to the video here: https://www.bilibili.com/video/BV1J64y1u7dw#reply2806426126 (the port number in the video is 9001, and now the project should be 9003)  
> `config/php/php72.ini`  
>In the configuration, I have already configured it and enabled debug and profile (if not needed, just delete it directly)

Get / post / cookie add parameter ‘Xdebug_ Trigger = any value ‘

Different solutions to a project curl accessing B project in container

1. Programme I

Currently passed inPHP container, modify / etc / hosts to solve the problem

Docker inspect - f '{{range. Networksettings. Networks} {. IPAddress} {{end}}' nginx # first obtain the nginx container IP in the host
Docker exec - it PHP bash # enters PHP container
Echo 'IP domain returned in the first step test' >> /etc/hosts

1. Programme II
See docker compose yml

extra_hosts:
      - laravel. Test: 172.20.128.2 # this IP is the nginx container IP and has been set to fixed. In this way, the IP address will not change every time you restart