I haven’t written anything for a long time, today I will talk about how to build a Laravel running Docker environment.

The most famous one on the market is “Laradock” github.com/laradock/la…

Docker PHP development environment. Usage reference: laradock. IO

Since it is “self-built”, we can refer to this to minimize the Laravel operation needs.

Here are the basics:

  1. Software: PHP 7.2, Nginx, MySQL, Composer, NPM or Yarn, etc.
  2. Use domestic mirror; Use domestic mirror; Use domestic mirror;
  3. Easy to extend, such as switching between PHP versions at any time, or Apache and Nginx.

Docker-Compose

To achieve scalable rows, use the same docker-compose arrangement as with “Laradock” to assemble several images from the core.

php-fpm

Here we use the DaoCloud accelerated image — 7.2-FTM-alpine.

This version uses BOTH PHP version 7.2 and alpine Minimization system, based on which you can install additional tools required by the environment: for example, Composer, NodeJS, Python, YARN, etc.

IO/PHP :7.2-fpm-alpine MAINTAINER coding01 <[email protected]> RUN sed -i's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

RUN apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        curl-dev \
        imagemagick-dev \
        libtool \
        libxml2-dev \
        postgresql-dev \
        sqlite-dev \
    && apk add --no-cache \
        curl \
        git \
        imagemagick \
        mysql-client \
        postgresql-libs \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && docker-php-ext-install \
        curl \
        iconv \
        mbstring \
        pdo \
        pdo_mysql \
        pdo_pgsql \
        pdo_sqlite \
        pcntl \
        tokenizer \
        xml \
        zip \
    && curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer \
    && apk del -f .build-deps

# Change composer to domestic mirror
RUN composer config -g repo.packagist composer https://packagist.laravel-china.org

# install prestissimo
RUN composer global require "hirak/prestissimo"

# install laravel envoy
RUN composer global require "laravel/envoy"

#install laravel installer
RUN composer global require "laravel/installer"

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

RUN apk update && apk add -u nodejs libpng-dev python

ENV PATH /root/.yarn/bin:$PATHRUN apk update \ && apk add curl bash binutils tar \ && rm -rf /var/cache/apk/* \ && /bin/bash \ && touch ~/.bashrc \ &&  curl -o- -L https://yarnpkg.com/install.sh | bash \ && yarn configset registry 'https://registry.npm.taobao.org' \
  && npm install -g cnpm --registry=https://registry.npm.taobao.org

WORKDIR /var/www
Copy the code

Where to install alpine system plug-in, we use mirrors.aliyun.com Ali cloud mirror.

PHP: 7.2 – FPM – alpine specific use, you can refer to: dashboard. Daocloud. IO/packages / 01…

nginx

We use Nginx, mainly to load the configuration files of the website into Nginx.

The FROM daocloud. IO/nginx: 1.13 - alpine MAINTAINER coding01 < [email protected] > ADD vhost. Conf/etc/nginx/conf. D/default. Conf WORKDIR /var/wwwCopy the code

All that’s left is to connect these images. Finally, take a look at the docker-comemess. yml file:

version: '2'
services:

  # The Applicationapp: build: context: ./ dockerfile: app.dockerfile working_dir: /var/www volumes: - .. /:/var/www environment: -"DB_PORT=3306"
      - "DB_HOST=database"
      - "REDIS_HOST=redis"
      - "REDIS_PORT=6379"

  # The Web Server
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes_from:
      - app
    ports:
      - 8080:80

  # The DatabaseDatabase: image: daoCloud. IO /mysql:5.7.4 volumes: -dbdata :/var/lib/mysql environment: -"MYSQL_DATABASE=homestead"
      - "MYSQL_USER=homestead"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"
    ports:
        - "3306:3306"Redis: image: daocloud. IO/library/redis: 4.0.10 - alpinecommand: redis-server --appendonly yes

volumes:
  dbdata:
Copy the code

Test it again

Create the Laravel project

composer create-project laravel/laravel demo
Copy the code

* Note: * For testing purposes, you can remove the vendor folder and composer. Lock files.

git clone

Git Clone “Laraveldocker” in the same folder as the demo project:

git clone https://github.com/fanly/laraveldocker.git
Copy the code

Modify the docker – compose. Yml

Execute our project with the path to the docker-comemage. yml file:

app: build: context: ./ dockerfile: app.dockerfile working_dir: /var/www volumes: - .. /:/var/wwwCopy the code

build

Execute the build command under LaravelDocker:

docker-compose up
Copy the code

The overall speed is pretty good

And then we go into the container

docker exec -it de075c525528 bash
Copy the code

Let’s look at the effect of installing the plugin:

The use of domestic mirror is https://packagist.laravel-china.org.

Note: This project is a public benefit project jointly launched by Laravel China community, Youbiyun.com and Youfan Yuanyang. It aims to provide stable and high-speed domestic Composer services for PHP users.

Recommended use

Reference: laravel-china.org/topics/4484…

Install the plug-in using YARN or CNPM:

Generate Laravel key secret:

cp .env.example .env
php artisan key:generate

Application key [base64:4A7VK6MEX7FakPLDSLji97kz/nyWUAWhW4wYn3gefsY=] set successfully.
Copy the code

Run it to see what it looks like:

Let’s take a look at the database connection and modify.env:

DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Copy the code

We use PHP Artisan Make: Auth to generate layout, registration and login views, and routes for all authentication interfaces. It also generates HomeController to handle application login requests. Use PHP Artisan Migrate to load data.

Let’s look at the data table:

Connect to MySQL database OK

conclusion

In the learning process, the use of others to do a good Dockerfile, although you can directly use, but if you can be self-sufficient, that is the best.

Through the process of self-built Docker development environment, I can also learn more. The next will continue to improve, minimize to meet the needs of development.

The code has been put on Github, welcome to reference and issue:

Github.com/fanly/larav…

Finally, you can also see the previous use of “Laradock” article:

Learn Docker with Laradock – Configuration

Learn docker-https with Laradock