安装docker-compose Install Docker Compose | Docker Documentation
首先创建环境目录
data 目录用来存放redis mysql数据
mysql nginx php redis 目录用来构建镜像,存放Dokerfile等
www目录为项目根目录,index.php输出phpinfo()
docker-compose.yml 为启动配置文件
.env 存储全局变量
配置如下
nginx Dockerfile
FROM nginx
COPY default.conf /etc/nginx/conf.d/
nginx default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
php Dockerfile
FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql \
&& curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/5.3.6.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mkdir -p /usr/src/php/ext \
&& mv phpredis-5.3.6 /usr/src/php/ext/redis \
&& docker-php-ext-install redis
redis Dockerfile
FROM redis
mysql Dockerfile
FROM mysql
.env
# 设置时区
TZ=Asia/Shanghai
# 设置网络模式
NETWORKS_DRIVER=bridge
# PATHS ##########################################
# 宿主机上代码存放的目录路径
WWW_PATH_HOST=./www
# 宿主机上Mysql Reids数据存放的目录路径
DATA_PATH_HOST=./data
# Mysql 服务映射宿主机端口号,可在宿主机127.0.0.1:3306访问
MYSQL_PORT=3306
MYSQL_USERNAME=admin
MYSQL_PASSWORD=123456
MYSQL_ROOT_PASSWORD=123456
# Redis 服务映射宿主机端口号,可在宿主机127.0.0.1:6379访问
REDIS_PORT=6379
#Php-fpm 端口号
PHP_PORT=9000
docker-compose.yml
version: '3.0'
# 网络配置
networks:
backend:
driver: ${NETWORKS_DRIVER}
# 服务容器配置
services:
nginx: # 自定义容器名称
build:
context: ./nginx # 指定构建使用的 Dockerfile 文件
environment: # 设置环境变量
- TZ=${TZ}
volumes: # 设置挂载目录
- ${WWW_PATH_HOST}:/usr/share/nginx/html # 引用 .env 配置中 WWW_PATH_HOST 变量,将宿主机上代码存放的目录挂载到容器中 /usr/share/nginx/html 目录
ports: # 设置端口映射
- "80:80"
- "8000:8000"
- "8001:8001"
- "8002:8002"
- "8003:8003"
networks:
- backend
restart: always # 指定容器退出后的重启策略为始终重启
php:
build:
context: ./php
environment:
- TZ=${TZ}
volumes:
- ${WWW_PATH_HOST}:/usr/share/nginx/html # 引用 .env 配置中 WWW_PATH_HOST 变量,将宿主机上代码存放的目录挂载到容器中 /usr/share/nginx/html 目录
ports:
- "${PHP_PORT}:9000" # 设置容器9000端口映射指定宿主机端口
networks:
- backend
restart: always
mysql:
build:
context: ./mysql
environment:
- TZ=${TZ}
- MYSQL_USER=${MYSQL_USERNAME} # 设置 Mysql 用户名称
- MYSQL_PASSWORD=${MYSQL_PASSWORD} # 设置 Mysql 用户密码
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} # 设置 Mysql root 用户密码
volumes:
- ${DATA_PATH_HOST}/mysql:/var/lib/mysql # 引用 .env 配置中 DATA_PATH_HOST 变量,将宿主机上存放 Mysql 数据的目录挂载到容器中 /var/lib/mysql 目录
ports:
- "${MYSQL_PORT}:3306" # 设置容器3306端口映射指定宿主机端口
networks:
- backend
restart: always
redis:
build:
context: ./redis
environment:
- TZ=${TZ}
volumes:
- ${DATA_PATH_HOST}/redis:/data # 引用 .env 配置中 DATA_PATH_HOST 变量,将宿主机上存放 Redis 数据的目录挂载到容器中 /data 目录
ports:
- "${REDIS_PORT}:6379" # 设置容器6379端口映射指定宿主机端口
networks:
- backend
restart: always
后台构建并启动
docker-compose up -d
出现done代表安装成功
打开浏览器测试