skip to Main Content

I’m trying to create a simple docker project and connect PHP and Nginx containers for a test. but i got this error :

Building php
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM php:latest
 ---> 52cdb5f30a05
Successfully built 52cdb5f30a05
Successfully tagged test_php:latest

WARNING: Image for service php was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building nginx
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM nginx:latest
 ---> 55f4b40fe486
Step 2/2 : ADD default.conf /etc/nginx/conf.d/default.conf
 ---> 20190910ffec
Successfully built 20190910ffec
Successfully tagged test_nginx:latest
WARNING: Image for service nginx was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating php ... done
Creating nginx ... done
Attaching to php, nginx
php      | Interactive shell
php      | 
nginx    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx    | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
php      | php > nginx    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx    | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
nginx    | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
nginx    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
php exited with code 0
nginx    | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx    | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx    | 2022/07/10 05:34:07 [emerg] 1#1: host not found in upstream "php" in /etc/nginx/conf.d/default.conf:14
nginx    | nginx: [emerg] host not found in upstream "php" in /etc/nginx/conf.d/default.conf:14
nginx exited with code 1

Here is the full directory structure of the project:

- docker
   -- nginx
      -- default.conf
      -- Dockerfile
   -- php
      -- Dockerfile
- src
  -- index.php
docker-compose.yml

and this is all files and their contents which i use :

# docker/nginx/default.conf
server {
    listen 80;
    index index.php index.htm index.html;

    root /var/www/html;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

# docke/nginx/Dockerfile
FROM nginx:latest
ADD default.conf /etc/nginx/conf.d/default.conf

# docker/php/Dockerfile
FROM php:latest

# src/index.php
<?php
echo phpinfo();

# docker-compose.yml
version: "3.8"
services:
  nginx:
    container_name: nginx
    build: ./docker/nginx
    command: nginx -g "daemon off;"
    links:
      - php
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html
  php:
    container_name: php
    build: ./docker/php
    ports:
      - "9000:9000"
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html

the main problem occurs when I want to connect the PHP container to the project and without PHP, Nginx will work correctly.

3

Answers


  1. You can try and add depends_on: php in your nginx service to at least try to make sure the nginx service doesnt’ start until the php service is Running. Probably the dependency is starting after the main service that requires it. This is a race condition problem, I think.

    Login or Signup to reply.
  2. I had 3 nodes, where nginx and php containers lived on different nodes.

    After trying various methods, such as:

    • define dedicated network for services inside docker-compose
    • in nginx config use upstream definition instead of direct name of the service
    • explicitly adding docker’s 127.0.0.11 resolver to nginx

    neither worked…

    And the reason actually was in a closed ports: https://docs.docker.com/engine/swarm/networking/#firewall-considerations

    Docker daemons participating in a swarm need the ability to communicate with each other over the following ports:

    Port 7946 TCP/UDP for container network discovery.
    Port 4789 UDP for the container overlay network.
    

    After I revert back all changes I did (network, resolver, upstream definition) to original simple setup and open the ports for inner node communication – service discovery begin to work as expected.

    Docker 20.10

    Login or Signup to reply.
  3. Several issues:

    • It seems you have containers that can’t see other.
    • It seems containers exits/fails and it is certainly not because of the first issue; nginx would still work of php-fpm socket is unavailable, it might crash but it should manage such unavailability very well
    • Make sure the php.ini is really opening an fpm socket on port 9000.
    • you index.php file script is not closed with "?>" [but that does not matter here]

    For a summary, you were suggested:

    • to consider docker swarm networking configuration [but it seems you are not using docker swarm]
    • to use depends_on which helps docker decide what to start first, but it should not be an issue in your case, nginx can wait. it will use the socket only upon web user requests.

    So it seems the internal docker name resolution is your issue and It seems defining the network manually is best practice. In my case I wandered too long before just giving the docker-compose file a specific network name and attaching containers to that network.
    If containers are in the same docker-compose file they should be in the same yourserver_default network that is autogenerated for your composed services.

    Have a look at https://blog.devsense.com/2019/php-nginx-docker, they actually define that network manually.

    And eventually redo everything from scratch, if you haven’t solved this yet. Else all the best to you!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search