'nginx and laravel docker container error 31#31: *1 connect() failed (111: Connection refused) while connecting to upstream

I have three containers nginx and two laravel, i am trying to setup nginx container to serve the two laravel applications. The first laravel application load fine on http://localhost but i got the error 31#31: *1 connect() failed (111: Connection refused) while connecting to upstream, when i try to access the second laravel app on http://localhost:8000

nginx.conf

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    root /var/www/html/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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



server {
    listen 8000;
    index index.php index.html;
    server_name localhost;
    root /var/www/html/secondapp/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass second_app:1000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
first_app:
    image: firstapp:1.0
    container_name: firstapp
    restart: unless-stopped
    depends_on:
      - main_db
    tty: true
   
    ports:
      - 9000:9000
    volumes:
      - ./src:/var/www/html
      - ./firstapp_ph/laravel.ini:/usr/local/etc/php/conf.d/laravel.ini
    networks:
      - back-tier
      - front-tier
  
  second_app:
    image:secondapp:1.0
    container_name:secondapp
    restart: unless-stopped
    depends_on:
      - second_db
    tty: true
   
    ports:
      - 1000:1000
    volumes:
      - ./secondapp:/var/www/html/secondapp
      - ./secondapp_php/laravel.ini:/usr/local/etc/php/conf.d/laravel.ini
    networks:
      - back-tier
      - front-tier
      
  #Nginx Service
  webserver:
    image: nginx:1.0
    depends_on:
      - first_app
      - second_app
      - second_db
      - main_db
    container_name: webserver
    restart: always
    tty: true
    ports:
      - "80:80"
      - "8000:8000"
    networks:
      - front-tier
      - back-tier
    volumes:
      - ./src:/var/www/html
      - ./secondapp:/var/www/html/secondapp


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source