Docker Connection Refused Between Nginx And PHP Containers

I’m creating a cluster with my php-fprm 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: 172.18.0.1, server: localhost, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1:8000"

The docker-compose.yml file:

  • Using Docker and MongoDB
  • I have error when I run docker in daemon mode
  • Problems with Dockerbeats dashboard containerName field
  • What does docker use for (image) versioning?
  • How to choose interface while creating a docker network?
  • Kubernetes access labels from containers
  • version: '2'
    services:
      php:
        restart: always
        build: data/php
        image: php:7.1
        ports:
          - "9000:9000"
        expose:
          - 9000
        volumes:
          - ./app:/usr/share/nginx/html
      web:
        restart: always
        build: data/nginx
        image: nginx:1.10.2
        ports:
          - "8000:80"
        links:
          - php
        volumes:
          - ./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):

    https://github.com/mayconfsbrito/docker

  • Docker “ERROR: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network”
  • How to get Docker host IP on Travis CI?
  • docker container port format does not looks right(like <port>-<port>)
  • Dockerfile: Setup SSH key for Bitbucket
  • Docker-Swarm, Kubernetes, Mesos & Core-OS Fleet
  • How to configure docker-compose for storing data of Elastic search out-side of docker container?
  • One Solution collect form web for “Docker Connection Refused Between Nginx And PHP Containers”

    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:

    fastcgi://127.0.0.1:9000
    

    You would want Nginx to connect to this instead:

    fastcgi://php:9000
    

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

    networks:
      default:
        external:
          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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.