Home  >  Article  >  Backend Development  >  Orchestrate Laravel applications using Docker compose

Orchestrate Laravel applications using Docker compose

不言
不言Original
2018-07-06 11:15:211525browse

This article mainly introduces the use of Docker compose to orchestrate Laravel applications. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

Preface

Laravel official The recommended development environment is Homestead (actually a packaged Vagrant box). I feel that this is relatively heavy, so I used Docker compose to organize a development environment and share it here.

Environmental requirements

Docker and Docker compose must be installed first, and it is best to replace the Docker warehouse image with a domestic one. Generally, I run Vagrant on my development computer, and then run Docker and other applications on it.

Main idea

What Docker officially recommends is that one container runs one service, so there will be Compose orchestration, and each service communicates through container interconnection technology. For example, when the Php service connects to Mysql, you only need to put the Host name Written as a container name, it will be directly converted into a specific IP internally. The code directory is mapped from the container to the host using data volumes, and the configuration files (Nginx, etc.) are also mapped to the container through data volumes.

Practice

I have encapsulated this set of services. For daily use, just clone it and use it directly. I will mainly talk about the implementation ideas here.

Project address: https://github.com/rootrl/php...

My project directory structure:

php-environment-with-docker/
├── bin
│ ├── composer
│ ├── getContainerIp
│ └── php
├── conf
│ ├── nginx
│ │ └── conf.d
│ │ │ └── nginx.conf
│ └── redis
│ └── redis.conf
├── docker-compose.yaml
├─ ─ Dockerfile.php
├── LICENSE
├── README.MD
└── start

  • bin These are all encapsulated command line tools. In fact, they are also Docker container services, but they are all disposable services.

  • conf This directory is the configuration directory of the application and will be mapped to the orchestration file of

  • docker-composer.yaml compose in the container using Volume , the following will talk specifically about the image construction of

  • Dockerfile.php php (there will be some customization, such as changing dns and installing special extensions)

  • start Run ./start to start all services. You can also run this command after restarting.

docekr-compose.yaml

This file is the orchestration file of compose

version: '2'

services:
nginx:
  depends_on:
   - "php"

  image: "nginx"

  volumes:
   - "$PWD/conf/nginx/conf.d:/etc/nginx/conf.d"
   - "$PWD/www:/usr/share/nginx/html"
  ports:
   - "8888:80"
  networks:
   - oa-network
  container_name: "oa-nginx"
  command: /bin/bash -c "mkdir -p /var/www && ln -s /usr/share/nginx/html /var/www && nginx -g 'daemon off;'"
php:
  image: "oa-php-fpm"
  build:  
   context: .
   dockerfile: "Dockerfile.php"
  networks:
   - oa-network
  container_name: "oa-php-fpm"
  volumes:
   - "$PWD/www:/var/www/html"

mysql:
  image: mysql:5.7
  volumes:
   - "$PWD/db_data:/var/lib/mysql"
  environment:
   MYSQL_ROOT_PASSWORD: root123
   MYSQL_DATABASE: oa
   MYSQL_USER: oa
   MYSQL_PASSWORD: oa123
  ports:
   - "3306:3306"
  networks:
   - oa-network
  container_name: "oa-mysql"

redis:
  image: "redis"
  ports:
   - "6379:6379"
  networks:
   - oa-network
  volumes:
   - "$PWD/conf/redis/redis.conf:/usr/local/etc/redis/redis.conf"
  container_name: "oa-redis"

networks:
oa-network:
  driver: bridge

Four services php-fpm, nignx, mysql, and redis are defined here (if you need other services, add them yourself). Then a public networks are defined so that all containers can communicate easily.

For example, in nginx.conf

server {
    listen       80;
    server_name  localhost;
    root /usr/share/nginx/html/public;
    index index.php index.html;
    location / {
    try_files $uri $uri/ /index.php?$query_string;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

The connection method with php-fpm is: php:9000

Dockerfile.php

FROM php:7.2-fpm
    Run echo "nameserver 223.5.5.5" >> /etc/resolv.conf \
    && echo "nameserver 223.6.6.6" >> /etc/resolve.conf \
    && apt-get update \
    && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install mysqli pdo_mysql \
    && pecl install swoole \
    && pecl install redis \
    && docker-php-ext-enable swoole redis

This is the Php image Build, change the dns server here, and install several php extensions.

Use

Start

./start to start all services

Command line

./bin/php -v

# Laravel artisan
./bin/php artisan

The above is the entire content of this article, I hope it will be helpful Everyone’s learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

The above is the detailed content of Orchestrate Laravel applications using Docker compose. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP quick sort codeNext article:PHP quick sort code