记一次用Docker为Laravel5.4部署本地开发环境:php7.1+redis+mysql+nginx
php:7.1-fpm-alpine
的基础上安装pdo_mysql和redis扩展,以及composer# 使用官方的 PHP 7.1 FPM Alpine 镜像作为基础镜像
FROM php:7.1-fpm-alpine
# 更新软件包索引
RUN apk update
# 安装必要的依赖包
RUN apk add --no-cache \
curl \
ca-certificates \
unzip \
libzip-dev \
icu-dev \
zlib-dev \
tcl-dev \
autoconf \
g++ \
libc-dev \
make \
libxml2-dev \
re2c
RUN docker-php-ext-install pdo_mysql \
&& docker-php-ext-enable pdo_mysql
# 安装redis扩展
RUN pecl install https://pecl.php.net/get/redis-4.3.0.tgz
RUN echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini
# 安装 Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
&& chmod +x /usr/local/bin/composer
# 设置工作目录
WORKDIR /var/www/html
# 暴露 PHP-FPM 默认监听的端口
EXPOSE 9000
# 启动 PHP-FPM 服务
CMD ["php-fpm"]
运行docker build -t my-php-fpm-composer .
构建镜像。
编写docker-compose.yml
version: '3.8'
services:
web:
image: my-php-fpm-composer
volumes:
- .:/var/www/html
depends_on:
- db
- composer
environment:
- DB_CONNECTION=mysql
- DB_HOST=db
- DB_PORT=3306
- DB_DATABASE=homestead
- DB_USERNAME=homestead
- DB_PASSWORD=secret
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- .:/var/www/html
- ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
- ./docker/snippets:/etc/nginx/snippets
depends_on:
- web
db:
image: mysql:5.7
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
volumes:
- dbdata:/var/lib/mysql
redis:
image: redis:alpine
volumes:
- redisdata:/data
composer:
image: composer:latest
volumes:
- .:/var/www/html
working_dir: /var/www/html
command: install --ignore-platform-reqs --no-interaction --optimize-autoloader
volumes:
dbdata:
redisdata:
按docker-compose.yml
的目录放置配置文件
nginx.conf
server {
listen 80;
root /var/www/html/public;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass web:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
# 确保这个位置允许访问
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
add_header Cache-Control "public, max-age=86400";
expires 30d;
log_not_found off;
}
}
fastcgi-php.conf
# /etc/nginx/snippets/fastcgi-php.conf
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
最后我们运行docker-compose up
将容器运行起来
再进入php的容器docker-compose exec web sh
,运行composer install
完成依赖包的安装。到这就结束了,去你的项目中愉快玩耍吧。