1

I have been on this error for 3 (full days) and still trying to figure out the issue and at the same time contemplating suicide :)

I have a docker setup for LEMP stack that I cloned from this repo:

https://github.com/matchish/skeleton

Everything works nicely on my development machine running window 10, but when I push the image to docker hub and pull it on my VPS no matter what I do I always get this error:

[emerg] 1#1: host not found in upstream "php-fpm:9000" in /etc/nginx/conf.d/upstream.conf:1

This error is coming from two files.

First: From this Nginx Docker file

Here is the code:

RUN echo "upstream php-upstream { server ${PHP_UPSTREAM_CONTAINER}:${PHP_UPSTREAM_PORT}; }" > /etc/nginx/conf.d/upstream.conf \
&& rm /etc/nginx/conf.d/default.conf

Second From this Nginx default.conf file

Here is the code:

    location ~ \.php$ {
      ...
      fastcgi_pass php-upstream;
      ...
    }

I say these two files are the cause b/c there is no reference of php-upstream elsewhere.

I have tried every possible combination of adding/removing hosts, adding depens_on, changing nginx, php version, disabling selinux but it just doesn't work. I always get the same error on production, but on local server evereything work.

2

nginx fails to resolve the hostname php-fpm and therefore refuses to start.

There is a simple workaround which - in this case - results in a 502 - Bad Gateway until nginx is able to resolve the upstream's hostname: Put the upstream address into a variable!

Further you should manually point nginx to docker's internal DNS with the resolver option. The docker internal DNS server is always at 127.0.0.11 as to be found in the documentation.

    resolver 127.0.0.11;
    set $upstream php-fpm:9000;
    # nginx will now start if host is not reachable
    fastcgi_pass    $upstream; 
  • thanks, but this does not work. I spent 3 hours, the only difference is now my app doesn't even run on my local machine. I get 502 - Bad Gateway on local (which used to work) but I get the same issue on production as well, so this answer actually was a regression – ANW Oct 15 '18 at 18:45
  • what happens if you ommit the resolver line? Does it still not work? I am 100% sure that the "trick" to put the upstream address into a variable works very well with any container setup. While your problem of the nginx container not being able to communicate with the php-fpm container might have a different cause it's still a good practice to include this trick in order to prevent a non-starting nginx container i.e. upon system restart. – nifr Oct 17 '18 at 17:04
  • I actually removed images, and now I am only using docker-compose to install the libraries, and I upload the compose file in my prod machine to install the same libs, so I can have the same environment in both dev/prod machines, because the whole image create, push, pull does not work. I hired someone on upwork.com and he said the issue was with the network, I run docker create new network my-network e.t.c... and it partially work but with more problem. So, the issue I am sure is with docker network and not php, nginx upstream – ANW Oct 17 '18 at 21:23
1

So, that error looks to me like it's trying to resolve what "php-fpm:9000" is, and failing. This line sets the variable, is there something different on your local machine than what's in production?

Are you deploying using the same docker-compose.yml file? From what I can tell it looks like you need to have the php-fpm container running before you can run the nginx container.

  • The machine is just a simple centos7 vps, nothing installed except yum update ..how do I maka php-fpm run after the nginx container? – ANW Oct 15 '18 at 18:29
  • If you're using docker-compose add depends_on: [php-fpm] to the configuration of the nginx service. This will start nginx after php-fpm. Take a look at the documentation – nifr Oct 17 '18 at 16:59
0

i have same problem, i can solved by added links to php-fpm like this:

services:
  php-fpm:
    build:
      context: ./php-fpm
    volumes: 
      - ../www:/var/www/html
    depends_on:
      - database
    links:
      - database:db      

  nginx:
    build:
      context: ./nginx
    volumes:
      - ../www:/var/www/html
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/sites/:/etc/nginx/sites-available
      - ./nginx/conf.d/:/etc/nginx/conf.d     
      - ./nginx/log/:/var/log/nginx
    ports:
      - "8080:80"
      - "443:443"
    depends_on:
      - php-fpm
    links:
      - php-fpm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.