How to correctly link php-fpm and Nginx Docker containers? -


i trying link 2 separate containers:

the problem php scripts not work. perhaps php-fpm configuration incorrect. here source code, in repository. here file docker-compose.yml:

nginx:     build: .     ports:         - "80:80"         - "443:443"     volumes:         - ./:/var/www/test/     links:         - fpm fpm:     image: php:fpm     ports:         - "9000:9000" 

and dockerfile used build custom image based on nginx one:

from nginx  # change nginx config here... run rm /etc/nginx/conf.d/default.conf add ./default.conf /etc/nginx/conf.d/ 

lastly, here 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 serve file directly, fallback 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;     } } 

could me configure these containers correctly execute php scripts?

p.s. run containers via docker-composer this:

docker-compose up

from project root directory.

don't hardcode ip of containers in nginx config, docker link adds hostname of linked machine hosts file of container , should able ping hostname.

edit: docker 1.9 networking no longer requires link containers, when multiple containers connected same network, hosts file updated can reach each other hostname.

every time docker container spins image (even stop/start-ing existing container) containers new ip's assigned docker host. these ip's not in same subnet actual machines.

see docker linking docs (this compose uses in background)

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

links

links:  - db  - db:database  - redis 

an entry alias' name created in /etc/hosts inside containers service, e.g:

172.17.2.186  db 172.17.2.186  database 172.17.2.187  redis 

expose

expose ports without publishing them host machine - they'll accessible linked services. internal port can specified.

and if set project ports + other credentials through environment variables, links automatically set bunch of system variables:

to see environment variables available 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