Home  >  Q&A  >  body text

请问下有人使用Docker来安装Laravel本地开发环境吗

有人使用docker来安装laravel本地环境吗,应该怎么做,安装好后怎么开发,就是一整套流程应该是什么样的?

曾经蜡笔没有小新曾经蜡笔没有小新2706 days ago799

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-04-26 09:03:45

    Thank you for the invitation, let me tell you about my plan, I hope it can bring some slight help to the subject.

    First of all, you need to make it clear that one container and one process are completed through collaboration between multiple containers.

    So, the following four containers are needed:

    1. nginx

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

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

    2. php-fpm

      • Function: Process PHP scripts.

      • Mirror: Since the project may rely on different extensions, you need to rely on the official image to build it yourself. In addition, composer support is required.

    3. mysql

      • Function: database.

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

    4. redis

      • Function: cache database.

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

    Let’s talk about the construction of php-fpm image. You need to pay attention to the following points:

    • Just rely on the official php:7.0.12-fpm image. There is no need to build it from scratch. You can choose the version by yourself. Bugs in the latest version are not ruled out.

    • If it is not used as a toy, it is best not to use the alpine series of mirrors, 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
        
    # 安装 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, you may encounter GFW problems during the construction process. You can refer to my article to make some adjustments: Docker Practice (7): Improve Happiness

    After building the image, multi-container management requires the use of orchestration tools docker-compose,所以还需要编写 docker-compose.yml file, a simple example (don’t forget to read 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
          # 这个挂载是为了处理静态文件
          - /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 is best defined by yourself and mounted into the container.

    • Don’t forget to set the time zone.

    In this way, the default.conf file of nginx can be written like this:

    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)(/.+)$;
        # 注意下面这行,pass 到 php-fpm 容器的服务名即可。
        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. In the future, you only need to cd to your project directory to execute

    docker-compose up -d

    You can start developing, isn’t it very simple?

    reply
    0
  • 滿天的星座

    滿天的星座2017-04-26 09:03:45

    Okay, let me recommend this
    Laradock
    In addition, if you have difficulty reading English, you can read the dean’s article...
    Laravel Academy
    Of course, there are many things that can be referenced, I want to ask Better to ask damn Google

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-04-26 09:03:45

    Recommend laradock on Github, don’t forget to configure a domestic acceleration, otherwise the download will be very slow

    reply
    0
  • Cancelreply