Does anyone use Docker to install Laravel’s local development environment

  docker, question

Does anyone use docker to install laravel’s local environment, what should be done, how to develop after installation, and what should the whole process look like?

Thank you for inviting me to talk about my plan. I hope it can bring slight help to the subject.

First of all, you need to be clear, one container one process, multi-container cooperation to complete.

Therefore, the following four containers are required:

  1. nginx

    • Role: Respond to web requests and process static files.

    • Mirror Image: No need to build it yourself, pull off the official mirror image directly.

  2. php-fpm

    • Role: Handle PHP scripts.

    • Mirroring: As the project may depend on different extensions, it needs to rely on official mirroring to build itself and composer support.

  3. mysql

    • Role: Database.

    • Mirror Image: No need to build it yourself, pull off the official mirror image directly.

  4. redis

    • Role: Cache database.

    • Mirror Image: No need to build it yourself, pull off the official mirror image directly.

Let’s talk about the construction of php-fpm image. Attention should be paid to the following points:

  • Directly dependent on the governmentphp:7.0.12-fpmMirroring is enough. You don’t need to build it from scratch. You can choose the version yourself, and you won’t rule out bugs in the latest version.

  • It is best not to use toys if they are not used properly.alpineThe mirror image of the series, although it is small and exquisite.

A simple dockerfile example:

FROM php:7.0.12-fpm
 MAINTAINER Tairy  <tairyguo@gmail.com >
 
 WORKDIR /working
 RUN apt-get update --fix-missing && apt-get install -y \
 g++ autoconf bash git apt-utils libxml2-dev libcurl3-dev pkg-config \
 && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
 && echo "Asia/Shanghai" > /etc/timezone \
 && docker-php-ext-install iconv curl mbstring \
 xml json mcrypt mysqli pdo pdo_mysql zip \
 && docker-php-ext-configure gd \
 --with-gd \
 --with-freetype-dir=/usr/include/ \
 --with-png-dir=/usr/include/ \
 --with-jpeg-dir=/usr/include/ \
 && docker-php-ext-install gd \
 && docker-php-ext-enable gd \
 && pecl install /pecl/redis-3.0.0.tgz \
 && docker-php-ext-enable redis \
 && apt-get purge -y --auto-remove \
 && rm -rf /var/cache/apt/* \
 && rm -rf /var/lib/apt/lists/* \
 && rm -rf /pecl
 
 # Install composer
 RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"  \
 && php  composer-setup.php \
 && php -r "unlink('composer-setup.php');"  \
 && mv composer.phar /usr/local/bin/composer \
 && composer self-update \
 && composer config -g repo.packagist composer  https://packagist.phpcomposer.com \
 && composer global require "laravel/installer=~1.1" \
 && composer global require predis/predis \
 && wget  https://phar.phpunit.de/phpunit.phar \
 && chmod +x phpunit.phar \
 && mv phpunit.phar /usr/local/bin/phpunit

Of course, there may be problems with GFW during the construction process. Please refer to my article to make some adjustments:Docker Practice (7): Promoting Happiness

After the image is built, multi-container management requires the use of orchestration tools.docker-compose, so still need to writedocker-compose.ymlFile, a simple example (don’t forget to look at the comments):

version: '2'
 services:
 nginx:
 image: nginx:alpine
 depends_on:
 - red
 ports:
 - 8080:80
 volumes:
 - /path/to/nginx.conf:/etc/nginx/nginx.conf
 - /path/to/default.conf:/etc/nginx/conf.d/default.conf
 # This mount is for static files
 - /path/to/static:/working
 networks:
 - app
 app:
 image: your-php-fpm-image
 depends_on:
 - mysql
 - redis
 volumes:
 - .:/working
 - /path/to/php.ini:/usr/local/etc/php/php.ini
 networks:
 - app
 mysql:
 image: mysql:latest
 environment:
 TZ: 'Asia/Shanghai'
 MYSQL_ROOT_PASSWORD: 123456
 volumes:
 - ./data:/var/lib/mysql
 ports:
 - 8002:3306
 networks:
 - app
 redis:
 image: redis:latest
 ports:
 - 8003:6379
 networks:
 - app
 networks:
 app:

Some points to note:

  • Be sure to define the network.

  • Nginx.conf, default.conf, php.ini had better define it and mount it in a container.

  • Don’t forget to set the time zone.

In this way, the default.conf file in nginx can be written as follows:

server {
 listen 80 default_server;
 server_name  default;
 
 location /static/ {
 root /working;
 index  index.html ;
 }
 
 index  index.html  index.php ;
 root /working/public;
 location / {
 try_files $uri $uri/ /index.php?  $query_string;
 }
 
 location /packages {
 try_files $uri $uri/;
 }
 
 location ~ [^/]\.php(/|$) {
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 # Note the following line. pass to the service name of the php-fpm container.
 fastcgi_pass app:9000;
 fastcgi_index  index.php ;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
 
 sendfile off;
 }

At this point, the configuration work is completed, and you only need cd to execute in your project directory in the future.

docker-compose up -d

Can be developed, is not very simple.