Home » Php » Docker Connection Refused Between Nginx And PHP Containers

Docker Connection Refused Between Nginx And PHP Containers

Posted by: admin July 12, 2020 Leave a comment


I’m creating a cluster with my php-fpm compiled from source image and the nginx official image, but when I try to run a phpinfo() file the nginx returns the error below:

*5 connect() failed (111: Connection refused) while connecting to upstream, client:, server: localhost, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://", host: ""

The docker-compose.yml file:

version: '2'
    restart: always
    build: data/php
    image: php:7.1
      - "9000:9000"
      - 9000
      - ./app:/usr/share/nginx/html
    restart: always
    build: data/nginx
    image: nginx:1.10.2
      - "8000:80"
      - php
      - ./data/nginx/vhost.conf:/etc/nginx/conf.d/default.conf
      - ./data/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./app:/usr/share/nginx/html
      - ./data/log/nginx:/var/log/nginx

I checked the 9000 port in my host and the netstat -an | grep :9000 command and the port are alive:

tcp6       0      0 :::9000                 :::*                    OUÇA

For more details see the files in this github repo (with the Dockerfile for that images):


How to&Answers:

Each container has its own localhost

Nginx and PHP are running in two different containers, so PHP is not running on localhost (from Nginx’s perspective).

Since your PHP container is named “php”, you don’t want to connect to this:


You would want Nginx to connect to this instead:


Looking at your repo on Github, I believe you need to change fastcgi_pass in data/nginx/vhost.conf to this:

fastcgi_pass phpserver;

And add an upstream to match. An upstream in Nginx is a way to define another server you are pointing to. You would put this outside of the server {} block, not inside.

upstream phpserver {
    server php:9000

What this does is define an upstream server in Nginx, named phpserver. That points to the hostname php on port 9000. Because your PHP container is named php in Docker, that is the hostname you can use to talk to it (within the Docker bridge network).

Then we tell fastcgi_pass it should use the upstream server called phpserver.

“ports” is only needed to expose a port outside of Docker

Unless you need to independently connect to fastcgi/PHP from outside of Docker, you do not need the ports config on the php container. You need only expose in order to have nginx able to talk to php internally.

It’s possible you need to define a Docker network

Recently I have noticed sometimes people need to specifically create a Docker network to make this sort of scenario work. I’m not sure why that is, but here is how you would do that.

First, create a network. Pick whatever name you like, I will use myapp in the below example.

docker network create myapp

You should now see this new network in docker network ls:

$ docker network ls
NETWORK ID          NAME                DRIVER       SCOPE
c39b88eb8450        bridge              bridge       local
175efb89adef        docker_default      bridge       local
b34434cc8b1c        myapp               bridge       local

Next, tell docker-compose to use this network. Add this block to the bottom of your docker-compose.yml file. It is at the top level of the YAML, meaning it should start at the left margin, not indented.

      name: myapp

After doing this, you will want to stop your containers and restart, so they will pick up changes. You do not need to do a rebuild to change the network config.