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

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

问题描述

我试图链接2个独立的容器:

I am trying to link 2 separate containers:

  • nginx:latest
  • php:fpm

问题是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的自定义图像: p>

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?

PS
我通过docker-composer运行容器,如下所示:

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

docker-compise up

推荐答案

不要对nginx配置中的容器进行硬编码,docker链接将链接的机器的主机名添加到容器的主机文件,您应该能够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容器从图像中旋转(甚至停止/启动现有容器)时,容器将获取新的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链接文档(这是后台使用的组合)

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.

如果您设置项目以通过环境变量获取端口+其他凭据,一个href =http://docs.docker.com/compose/env/>链接自动设置一系列系统变量:

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

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天全站免登陆