0

I have the following 4 containers:

  1. php-fpm for Laravel API backend
  2. node.js container for the Next.js frontend
  3. nginx container
  4. mysql container

I am able to call the Laravel API and get the correct JSON data from Postman using http://localhost:8088/api/products , but when I try from the Node container, I get FetchError: request to http://localhost:8088/api/ failed, reason: connect ECONNREFUSED 10.0.238.3:8088. I am not sure if it's the nginx configuration issue, or docker-compose.yml configuration issue. I also tried to call the API from the node container using several other options (none of them worked):

  1. http://php:8088/api/products
  2. http://localhost:8088/api/products
  3. http://php:9000/api/products - gives a different error: FetchError: request to http://php:9000/api/products/ failed, reason: read ECONNRESET

This is the docker-compose.yml:

networks:
    laravel:
        driver: bridge

services:
    nginx:
        image: nginx:stable-alpine
        container_name: nginx
        ports:
            - "8088:80"
        volumes:
            - ./laravel-app:/var/www/html 
            - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
        
        depends_on:
            - php
            - mysql           
            - node             
        networks:
            - laravel
            
    mysql:
        image: mysql
        container_name: mysql
        restart: unless-stopped
        tty: true
        ports:
            - "4306:3306"
        volumes:
            - ./mysql:/var/lib/mysql
        environment:
            MYSQL_DATABASE: laravel          
            MYSQL_ROOT_PASSWORD: password
            SERVICE_TAGS: dev
            SERVICE_NAME: mysql            
        networks:
            - laravel
                     
    
    php:
        build:
            context: .
            dockerfile: Dockerfile
        container_name: php
        volumes:
            - ./laravel-app:/var/www/html
        ports:
            - "9000:9000"
        networks:
            - laravel

    node:
        build:
            context: ./nextjs
            dockerfile: Dockerfile
        container_name: next
        
        volumes:
            - ./nextjs:/var/www/html
        ports:
            - "3000:3000"
            - "49153:49153"

        networks:
            - laravel

And this is the nginx default.conf file:

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    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 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;
    }
    
}
pileup
  • 1
  • 2
  • 18
  • 45
  • 1
    Also posted on https://forums.docker.com/t/cant-call-my-laravel-api-from-the-node-js-container-but-i-can-call-it-from-postman/113996 – Arjan Aug 22 '21 at 08:24

1 Answers1

3

If you want to reach your Laravel API from the node service, you must use http://nginx/your_api_endpoint.

Nginx acts as a reverse proxy here, it takes the traffic on the port 80 (listen 80;) and redirects it to your Laravel container (fastcgi_pass php:9000;).

So your target is not the Laravel container itself, it is nginx.

http://nginx/api/products/ should works if everything else is ok.

Anthony Aslangul
  • 3,589
  • 2
  • 20
  • 30
  • I can't believe, earlier I tried `http://nginx:8088/api/products/` with the wrong port (didn't even put it on the list in the post). It works now! thank you! – pileup Aug 18 '21 at 17:42
  • 1
    No problem. FYI, if it doesn't work with the port, that's because when you are inside your containers, your are inside a private network (the `laravel` you declared inside your `docker-compose.yml`). The port `8088`, which is mapped to the `80`, is here to say "map the host port 8088 to the 80 inside the container". In short, this is done to access the container from the host (your machine)... but inside the node container, you are not in your host network. Alternatively, `http://nginx:80/api/products/` shoud also works. – Anthony Aslangul Aug 18 '21 at 17:48
  • Thank you, may I ask you one more thing? I made this `docker-compose.yml` file after watching a tutorial on how to create an environment for developing websites with Laravel, but here the Laravel is only the API, and the actual pages are the Next.js files on the `node` container, is there a way to make nginx serve the frontend, rather than directly access the from them host? As you can see right now it's mapping 3000:3000 so I access my website directly from the host, not from the nginx as proxy (As I do with the Laravel API) – pileup Aug 18 '21 at 17:51
  • 1
    It is possible yes, but it may be over complicated if it is just for development purposes. You could map a port from your nginx container (lets say 3000) to another port inside the container, and then make a vhost that listen to that port and redirect the traffic to your node container, on the port your next app listen to (like you did with php, but without all the `try_files` stuff...). It's hard to explain in a limited size comment without line breaks :p But if you are just experimenting, you really don't need it... but it's possible yes. – Anthony Aslangul Aug 18 '21 at 18:02
  • 1
    The idea is to remove the mapping `"3000:3000"` from your node container (because you'll not access it from your host) (btw you can also remove `"9000:9000"` from you php container since... well, you are not accessing it from your host either). Then you add "a port" (doesn't matter which one) to your nginx container (`"xxxx":"8888"`) so you can type http://localhost:xxxx to your browser and access your app. After that, you make a new server block, listening to the 8888 port and doing a `fastcgi_pass node:3000;`. If you do it just with these small explanations, well done :) – Anthony Aslangul Aug 18 '21 at 18:10
  • Well I tried, but failed :), here is my new question with what I tried to do, if you can help: https://stackoverflow.com/questions/68838363/adding-second-server-block-as-reverse-proxy-breaks-the-server – pileup Aug 18 '21 at 19:48