7

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.

womble
  • 95,029
  • 29
  • 173
  • 228
samayo
  • 277
  • 3
  • 5
  • 17
  • Did you ever figure this out? I have run into this same issue. Docker compose and my nginx config works fine on Windows, but not when I try to use the exact same things on my Mac. I have noticed that, for me, the issue only started when I switched from doing `image: nginx:latest` in the yml file to using `build: context: .nginx` and having a dockerfile in the my nginx folder. – Jyosua Jul 16 '19 at 01:46
  • 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 '19 at 10:45
  • Yeah, if you don't mind dropping me a link to it, I would greatly appreciate it!!! – Jyosua Jul 16 '19 at 15:04
  • 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 https://github.com/samayo/docker-simple-lemp – samayo Jul 16 '19 at 16:50
  • Also I remember the error was related to a network. A docker network must be created to avoid the upstream error, let me know if my repo doesn't work or if it works – samayo Jul 16 '19 at 16:52
  • Yeah, in my case, I already had a network defined and it worked on windows, but not on mac, which is why it was weird. On my mac, it also worked when I used image instead of build, too. I'll check out your repo tonight and see if it works for me on my mac. – Jyosua Jul 16 '19 at 18:51
  • So, that error looks to me like it's trying to resolve what "php-fpm:9000" is, and failing. [This line](https://github.com/matchish/skeleton/blob/f37b87273f2a37ce873abf68c772dba0ff983f3d/nginx/Dockerfile#L12) 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 '18 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 '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](https://docs.docker.com/compose/compose-file/#depends_on) – Nicolai Fröhlich Oct 17 '18 at 16:59

2 Answers2

7

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 – samayo 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. – Nicolai Fröhlich 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 – samayo Oct 17 '18 at 21:23
  • Adding also `proxy_pass $upstream;` solved the problem for me. – rémy Jan 20 '20 at 07: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
Ari Pratomo
  • 111
  • 2