如何正确链接 php-fpm 和 Nginx Docker 容器? [英] How to correctly link php-fpm and Nginx Docker containers?

查看:33
本文介绍了如何正确链接 php-fpm 和 Nginx Docker 容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to link 2 separate containers:

问题是 php 脚本不起作用.也许 php-fpm 配置不正确.这是源代码,它在我的存储库中.这是文件 docker-compose.yml:

The problem is that php scripts do not work. Perhaps the php-fpm configuration is incorrect. Here is the source code, which is in my repository. Here is the file 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 的自定义镜像:

and Dockerfile which I used to build a custom image based on the nginx one:

FROM nginx

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

最后,这是我自定义的 Nginx 虚拟主机配置:

Lastly, here is my custom Nginx virtual host config:

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 脚本?

Could anybody help me configure these containers correctly to execute php scripts?

附言我像这样通过 docker-composer 运行容器:

P.S. I run containers via docker-composer like this:

docker-compose up

来自项目根目录.

推荐答案

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

Don't hardcode ip of containers in nginx config, docker link adds the hostname of the linked machine to the hosts file of the container and you should be able to ping by hostname.

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

Docker 1.9 Networking no longer requires you to link containers, when multiple containers are connected to the same network, their hosts file will be updated so they can reach each other by hostname.

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

Every time a docker container spins up from an image (even stop/start-ing an existing container) the containers get new ip's assigned by the docker host. These ip's are not in the same subnet as your actual machines.

查看 docker 链接文档(这是 compose 在后台使用的)

see docker linking docs (this is what compose uses in the background)

但在 docker-compose 文档中的链接和链接中有更清楚的解释暴露

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

链接

links:
 - db
 - db:database
 - redis

将在容器内的/etc/hosts 中为此服务创建一个具有别名名称的条目,例如:

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 ports without publishing them to the host machine - they'll only be accessible to linked services. Only the internal port can be specified.

如果您将项目设置为通过环境变量获取端口 + 其他凭据,链接会自动设置一堆系统变量:

and if you set up your project to get the ports + other credentials through environment variables, links automatically set a bunch of system variables:

要查看服务可用的环境变量,请运行 docker-compose run SERVICE env.

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

name_PORT

完整网址,例如DB_PORT=tcp://172.17.0.5:5432

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

name_PORT_num_protocol

完整网址,例如DB_PORT_5432_TCP=tcp://172.17.0.5:5432

name_PORT_num_protocol_ADDR

容器的 IP 地址,例如DB_PORT_5432_TCP_ADDR=172.17.0.5

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

name_PORT_num_protocol_PORT

暴露的端口号,例如DB_PORT_5432_TCP_PORT=5432

name_PORT_num_protocol_PROTO

协议(tcp 或 udp),例如DB_PORT_5432_TCP_PROTO=tcp

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

name_NAME

完全限定的容器名称,例如DB_1_NAME=/myapp_web_1/myapp_db_1

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

这篇关于如何正确链接 php-fpm 和 Nginx Docker 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆