香雨站

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 87|回复: 0

Docker

[复制链接]

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
发表于 2022-12-21 21:17:15 | 显示全部楼层 |阅读模式
环境:window,自行安装docker
拉取nginx和php镜像

docker pull nginx
docker pull php:7.3-fpm启动php容器

docker run --name my-php-fpm -v D:/www:/www -d php:7.3-fpm

  • --name my-php-fpm
  • 将容器命名为 my-php-fpm
  • ---------------------------------------------------------------------------------------------
  • -v D:/www:/www
  • 将本地目录D:/www挂载(关联)到php容器的目录/www
启动nginx容器

docker run --name my-nginx -p 8080:80 -d \
    -v D:/www:/usr/share/nginx/html:ro \
    -v D:/conf/conf.d:/etc/nginx/conf.d:ro \
    --link my-php-fpm:php \
    nginx

  • -p 8080:80
  • 端口映射,把nginx容器中的 80 端口映射到本地的 8080 端口
  • ---------------------------------------------------------------------------------------------
  • -v D:/www:/usr/share/nginx/html:ro
  • 将本地目录D:/www挂载(关联)到nginx容器的目录/usr/share/nginx/html
  • 本地根目录D:/www,nginx容器根目录是/usr/share/nginx/html
  • ---------------------------------------------------------------------------------------------
  • -v D:/conf/conf.d:/etc/nginx/conf.d:ro
  • 将本地目录D:/conf/conf.d挂载(关联)到nginx容器的目录/etc/nginx/conf.d
  • 本地nginx配置目录是D:/conf/conf.d,nginx容器配置目录是/etc/nginx/conf.d
  • ---------------------------------------------------------------------------------------------
  • --link my-php-fpm:php
  • 把my-php-fpm的网络并入nginx,并通过修改nginx的 /etc/hosts,把域名php映射成 127.0.0.1,让 nginx 通过 php:9000 访问 php-fpm
挂载目的是方便共享文件,本地文件的修改会同步到容器里,
让php-fpm解析器像本地一样解析php文件
让nginx像访问本地目录一样获取项目目录和获取配置文件
<hr/>测试


  • 在D:/www下创建index.php
<?php
echo phpinfo();

  • 在D:/conf/conf.d下创建test.conf
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000; #启动nginx容器时--link my-php-fpm:设置的值php
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

  • 浏览器打开http://localhost:8080/index.php



如果打开浏览器显示File not found.
修改本地nginx配置文件中
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;为 启动php容器时设置的映射目录-v D:/www:/www冒号后的/www路径
fastcgi_param  SCRIPT_FILENAME  /www$fastcgi_script_name;注意:新增和修改nginx配置文件需要重新nginx容器让新的配置生效
docker restart my-nginx
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|香雨站

GMT+8, 2025-3-15 10:28 , Processed in 0.114415 second(s), 24 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表