Laravel官方開發環境推薦的是Homestead(其實就是一個封裝好的Vagrant box),我感受這個比較重,因而本身用Docker compose編排了一套開發環境,在這裏分享下。php
先要安裝好Docker 和 Docker compose,並且Docker 倉庫鏡像最好換成國內的。通常地,我開發電腦上會運行一個Vagrant,而後再在裏面運行Docker等應用。html
Docker官方推薦的是一個容器運行一個服務,因此會有Compose編排,各個服務間經過容器互聯技術通訊,好比Php服務鏈接Mysql只用把Host名寫成容器名,內部會直接轉換成具體ip。代碼目錄使用數據卷從容器內映射到宿主機,配置文件(Nginx等)也是經過數據卷映射到容器內。mysql
這套服務我已經封裝好了,平時用的話只用clone下來直接使用,我這裏主要講下實現思路。nginx
項目地址:https://github.com/rootrl/php...git
個人項目目錄結構:github
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
└── startredis
此文件是compose的編排文件sql
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,這樣容器內均可以很方便地進行通訊。docker
好比nginx.conf中bash
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
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 啓動全部服務
./bin/php -v # Laravel artisan ./bin/php artisan