第一种方式 使用IP

启动PHP

docker run --name php -v /nas/k8s_doc/nas/www:/usr/share/nginx/html -p 9000:9000 -d   php:7.4-fpm-alpine 
73e69f48970a7251ec6cd119c2c9dade712622e9bef9b6bb6a43d564e3a1bcc6

执行:
docker inspect php
获取到addres:

[root@master1 nas]# docker inspect php| grep "IPAddress"
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",

修改default中的fastcgi_pass中的IP为这个IP:
172.17.0.3

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.php 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           html;
       fastcgi_pass   172.17.0.3:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
       fastcgi_param  SCRIPT_NAME      $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;
    #}
}

启动NGINX

docker run --name nginx -v /nas/k8s_doc/nas/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /nas/k8s_doc/nas/www:/usr/share/nginx/html -v /nas/k8s_doc/nas/nginx/logs:/var/log/nginx -d -p 8080:80 nginx:stable-alpine

这种方式需要手动修改IP,方法二,采用DOCKER network 来自动解析名称:

方法二:docker network 自动解析名称

# 查看网络

[root@master1 nas]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
eaae0152cb16        bridge              bridge              local
c664bcc9d002        host                host                local
b44256c6abd1        none                null                local

创建自定义网络:

docker network create my-net

上述命令中加上:–network my-net

修改default.conf fastcgi_pass 中的IP为 php:

  location ~ \.php$ {
       root           html;
       fastcgi_pass   php:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
       fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
       include        fastcgi_params;
    }

删除后重新启动

[root@master1 nas]# docker rm -f nginx
nginx
[root@master1 nas]# docker rm -f php

docker run --name nginx -v /nas/k8s_doc/nas/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /nas/k8s_doc/nas/www:/usr/share/nginx/html -v /nas/k8s_doc/nas/nginx/logs:/var/log/nginx -d -p 8080:80 --network my-net  nginx:stable-alpine

docker run --name nginx -v /nas/k8s_doc/nas/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /nas/k8s_doc/nas/www:/usr/share/nginx/html -v /nas/k8s_doc/nas/nginx/logs:/var/log/nginx -d -p 8080:80 --network my-net  nginx:stable-alpine

echo “<?php phpinfo():?>” > /nas/k8s_doc/nas/www/info.php

打开浏览器:
http://192.168.132.50:8080/info.php
192.168.132.50 为docker 主机IP
在这里插入图片描述

结语

此文仅用于测试掌握细节,正式环境建议用docker-composer,或者k8s来部署,更加方便。
请看我的另一篇文字:
docker compose 部署 NGINX + PHP

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐