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

I am trying to link 2 separate containers:

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:

    build: .
        - "80:80"
        - "443:443"
        - ./:/var/www/test/
        - fpm
    image: php:fpm
        - "9000:9000"

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/

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_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;

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

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

docker-compose up

from the project root directory.


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 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.

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.

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

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


  • db
  • db:database
  • redis

An entry with the alias' name will be created in /etc/hosts inside containers for this service, e.g: db database 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:

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


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


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


Container’s IP address, e.g. DB_PORT_5432_TCP_ADDR=


Exposed port number, e.g. DB_PORT_5432_TCP_PORT=5432


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


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

Leave a Reply

Your email address will not be published. Required fields are marked *