8

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

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 depends_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 everything works.

9
  • 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.
    – Jon Buys
    Oct 11, 2018 at 19:57
  • The machine is just a simple centos7 vps, nothing installed except yum update ..how do I maka php-fpm run after the nginx container?
    – samayo
    Oct 15, 2018 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 Oct 17, 2018 at 16:59
  • 1
    oh man, yes i figured it out but I forgot :) I remember spending days but somehow either i found out by chance or someone told me and I can't remember. But I have a docker-compose for lemp stack which i made after i resolved the issue and now I just pull and run compose and it works. it has php-fpm, nginx, mariadb ... let me know if interested.
    – samayo
    Jul 16, 2019 at 10:45
  • 1
    Ok, I have been meaning to push this but didn't have time. I did a quick dusting and tested it, it works. Just clone and run docker-compose up -d check link github.com/samayo/docker-simple-lemp
    – samayo
    Jul 16, 2019 at 16:50

2 Answers 2

9

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; 
4
  • 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
    – samayo
    Oct 15, 2018 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. Oct 17, 2018 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
    – samayo
    Oct 17, 2018 at 21:23
  • Adding also proxy_pass $upstream; solved the problem for me.
    – rémy
    Jan 20, 2020 at 7:45
1

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

Not the answer you're looking for? Browse other questions tagged .