1.建立工作目录
mkdir -p /home/test/html && cd /home/test/html
2.准备nginx配置文件 touch nginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
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$ {
fastcgi_pass php-fpm:9000; #这里是nginx 与 php-fpm 容器所在同一网络的服务名
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; #php-fpm 容器中php代码位置
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
3.创建容器通信网络
docker network create -d bridge my_network
4.启动php-fpm 容器
docker pull php:7.4-fpm
docker run -itd --network my_network --name php-fpm -v /home/test/html:/var/www/html php:7.4-fpm
5.创建nginx 的dockerfile 文件并启动 touch nginx_dfile
FROM nginx
COPY nginx.conf /etc/nginx/conf.d/default.conf
#需要替换配置文件
6.启动nginx 因为nginx依赖于php-fpm 所以先启动php-fpm
docker build -f nginx_dfile -t my_nginx .
docker run -d -p 8080:80 --network my_network --name nginx -v /home/test/html:/usr/share/nginx/html my_nginx
7.测试 touch index.php
<?php
phpinfo();
8. 浏览器访问8080 端口
笔记docker 常用命令
#停止并删除所有容器
docker stop $(docker ps -aq) && docker rm $(docker ps -aq)
#查看所有容器
docker ps -a