服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - 服务器知识 - 使用Docker compose编排Laravel应用的方法

使用Docker compose编排Laravel应用的方法

2021-03-02 18:10rootrl 服务器知识

本篇文章主要介绍了使用Docker compose编排Laravel应用的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前言

Laravel官方开发环境推荐的是Homestead(其实就是一个封装好的Vagrant box),我感觉这个比较重,于是自己用Docker compose编排了一套开发环境,在这里分享下。

环境要求

先要安装好Docker 和 Docker compose,而且Docker 仓库镜像最好换成国内的。一般地,我开发电脑上会运行一个Vagrant,然后再在里面运行Docker等应用。

主要思路

Docker官方推荐的是一个容器运行一个服务,所以会有Compose编排,各个服务间通过容器互联技术通信,比如Php服务连接Mysql只用把Host名写成容器名,内部会直接转换成具体ip。代码目录使用数据卷从容器内映射到宿主机,配置文件(Nginx等)也是通过数据卷映射到容器内。

实践

这套服务我已经封装好了,平时用的话只用clone下来直接使用,我这里主要讲下实现思路。

项目地址:https://github.com/rootrl/php-environment-with-docker

我的项目目录结构:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
  1. bin 这里面都是封装的命令行工具,其实也是Docker容器服务,只不过他们都是用完即走的服务。
  2. conf 该目录都是应用的配置目录,会使用Volumn映射到容器内
  3. docker-composer.yaml compose 的编排文件,下面会具体讲到
  4. Dockerfile.php php的镜像构建(里面会有一些定制,比如改dns,装特殊扩展)
  5. start 运行./start就可以启动所有服务,重启也可以运行此命令

docekr-compose.yaml

此文件是compose的编排文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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

这里定义了php-fpm、nignx、mysql、redis四个服务(如果需要其他服务,自行添加)。然后定义了一个公共的networks,这样容器内都可以很方便地进行通信。

比如nginx.conf中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
  }
}

这里与php-fpm的连接方式:php:9000

Dockerfile.php

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

这是Php镜像构建,这里改了dns服务器,并安装了若干php扩展。

使用

启动

./start 启动所有服务

命令行

?
1
2
3
4
./bin/php -v
 
# Laravel artisan
./bin/php artisan

总结

具体可访问:https://github.com/rootrl/php-environment-with-docker

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000015491751

延伸 · 阅读

精彩推荐