Docker + Nginx + PHP-FPM error: [emerg] 1#1: host not found in upstream [closed]

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.


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;