1.mysql8.0安装
docker pull mysq
docker run -p 3306:3306 --name mysql8.0 -d -v D:\docker\mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql
2,php8.0安装
docker pull php:8.0-rc-fpm
#D:\code为本地php文件保存路径,自行修改
docker run --name php8.0 -p 9000:9000 -v D:\code:/var/www -d php:8.0-rc-fpm
3.nginx安装
docker pull nginx
为了方便本地配置可使用的环境,先建一个测试容器获取各类配置
docker run --name nginx-test -p 80:80 -d nginx
#host配置,D:\docker\nginx\conf.d/自行修改为自己本地路径
docker cp nginx-test:/etc/nginx/conf.d/default.conf D:\docker\nginx\conf.d/
#nginx配置文件
docker cp nginx-test:/etc/nginx/nginx.conf D:\docker\nginx\conf
#删除测试容器
docker stop nginx-test
docker rm nginx-test
创建新的容器
docker run --name nginx -p 80:80 -v D:\docker\nginx\conf:/etc/nginx/conf -v D:\docker\nginx\conf.d:/etc/nginx/conf.d -v D:\docker\nginx\logs:/var/log/nginx -v D:/code:/var/www -d --link php8.0:php nginx
修改本地host配置:D:\docker\nginx\conf.d\default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /var/www; #修改此处
index index.html index.htm;
}
#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 /var/www;
fastcgi_pass php8.0:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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-mysql扩展
docker exec -it php8.0 /bin/bash
docker-php-ext-install pdo_mysql mysqli
重启下php8.0容器即可!
4.docker-compose管理应用
在对应的项目目录下新建docker-compose.yml文件
version: '3'
services:
nginx:
image: nginx:latest
container_name: local_nginx #注意留个空格
ports:
- "80:80"
volumes:
- d:/code:/var/www
- d:/docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf ###这里为防止host多了之后出现打架情况,最好本地使用一个单独的目录分别将不同的host映射到docker,例如d:/docker/nginx/host/test.conf:/etc/nginx/conf.d/test.conf
- d:/docker/nginx/conf:/etc/nginx/conf
- d:/docker/nginx/logs:/var/log/nginx
php:
image: php:8.0-rc-fpm
container_name: local_php8.0
ports:
- "9000:9000"
volumes:
- d:/code:/var/www
修改d:/docker/nginx/conf.d/default.conf(根据自己的目录自行修改)
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /var/www;
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 /var/www;
fastcgi_pass local_php8.0:9000; ######此处换成yml文件中php的容器名称
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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;
}
}
docker-compose up -d 启动即可