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


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

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
    └── xdebug-2.8.0.tgz

2. Integrated services

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

3、 Use

It is strongly recommended that 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 = # instead of IP
    DB_ Host = MySQL # similarly, fill in MySQL
    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/
  3. Create a dns resolver:

       sudo mkdir -v /etc/resolver
       sudo bash -c 'echo "nameserver" > /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: (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

      - laravel. Test: # 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