gpt4 book ai didi

php - 如何正确链接php-fpm和Nginx Docker容器?

转载 作者:行者123 更新时间:2023-12-02 19:48:25 24 4
gpt4 key购买 nike

我正在尝试链接2个单独的容器:

  • nginx:latest
  • php:fpm

  • 问题是php脚本不起作用。也许php-fpm配置不正确。
    这是源代码,位于我的 repository中。这是文件 docker-compose.yml:
    nginx:
    build: .
    ports:
    - "80:80"
    - "443:443"
    volumes:
    - ./:/var/www/test/
    links:
    - fpm
    fpm:
    image: php:fpm
    ports:
    - "9000:9000"

    Dockerfile,我曾经根据nginx构建了一个自定义图像:
    FROM nginx

    # Change Nginx config here...
    RUN rm /etc/nginx/conf.d/default.conf
    ADD ./default.conf /etc/nginx/conf.d/

    最后,这是我自定义的Nginx虚拟主机配置:
    server {
    listen 80;

    server_name localhost;
    root /var/www/test;

    error_log /var/log/nginx/localhost.error.log;
    access_log /var/log/nginx/localhost.access.log;

    location / {
    # try to serve file directly, fallback to app.php
    try_files $uri /index.php$is_args$args;
    }

    location ~ ^/.+\.php(/|$) {
    fastcgi_pass 192.168.59.103:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
    }
    }

    有人可以帮助我正确配置这些容器以执行php脚本吗?

    P.S.
    我通过docker-composer运行容器,如下所示:
    docker-compose up
    从项目根目录。

    最佳答案

    不要在nginx config中对容器的ip进行硬编码,docker link将链接机器的主机名添加到容器的hosts文件中,您应该能够按主机名ping。

    编辑:Docker 1.9 Networking不再需要您链接容器,当多个容器连接到同一网络时,它们的主机文件将被更新,以便它们可以通过主机名相互访问。

    每次docker容器从镜像启动(甚至停止/启动现有容器)时,这些容器都会获得docker主机分配的新IP。这些ip与您的实际计算机不在同一子网中。

    see docker linking docs(这是compose在后台使用的内容)

    but more clearly explained in the docker-compose docs on links & expose

    links

    links:
    - db
    - db:database
    - redis

    An entry with the alias' name will be created in /etc/hosts inside containers for this service, e.g:

    172.17.2.186  db
    172.17.2.186 database
    172.17.2.187 redis

    expose

    Expose ports without publishing them to the host machine - they'll only be accessible to linked services. Only the internal port can be specified.



    如果您设置了项目以通过环境变量获取端口+其他凭据, links automatically set a bunch of system variables:

    To see what environment variables are available to a service, run docker-compose run SERVICE env.

    name_PORT

    Full URL, e.g. DB_PORT=tcp://172.17.0.5:5432

    name_PORT_num_protocol

    Full URL, e.g. DB_PORT_5432_TCP=tcp://172.17.0.5:5432

    name_PORT_num_protocol_ADDR

    Container's IP address, e.g. DB_PORT_5432_TCP_ADDR=172.17.0.5

    name_PORT_num_protocol_PORT

    Exposed port number, e.g. DB_PORT_5432_TCP_PORT=5432

    name_PORT_num_protocol_PROTO

    Protocol (tcp or udp), e.g. DB_PORT_5432_TCP_PROTO=tcp

    name_NAME

    Fully qualified container name, e.g. DB_1_NAME=/myapp_web_1/myapp_db_1

    关于php - 如何正确链接php-fpm和Nginx Docker容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62262377/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com