2

I am new to docker for Windows and have this setup wherein 2 repos have its own docker-compose.yml file. One is for the API while the other is for the APP.

API docker-compose.yml

version: '3'

networks:
    my_network:
        name: my_network
        ipam:
            config:
                - subnet: 172.23.0.0/24
services:
    my_api:
        build:
            context: .
            dockerfile: .docker/site.dockerfile
        container_name: my_api
        ports:
            - 127.0.12.9:80:80
        volumes:
            - ./:/var/www/html
        networks:
            my_network:
                aliases:
                    - "api.localhost"
    webserver:
        image: nginx:alpine
        container_name: webserver_my_api
        restart: unless-stopped
        tty: true
        ports:
            - 127.0.12.10:80:80
            - 127.0.12.10:443:443
        volumes:
            - ./:/var/www/html
            - ./.docker/nginx/conf.d/:/etc/nginx/conf.d/
        networks:
            - my_network

APP docker-compose.yml

version: '3'

networks:
    my_network:
        external: true
services:
    my_app:
        build:
            context: .
            dockerfile: .docker/site.dockerfile
        container_name: my_app
        ports:
            - 127.0.12.11:80:80
        volumes:
            - ./:/var/www/html
        networks:
            my_network:
                aliases:
                    - "app.localhost"
    webserver:
        image: nginx:alpine
        container_name: webserver_my_app
        restart: unless-stopped
        tty: true
        ports:
            - 127.0.12.12:80:80
            - 127.0.12.12:443:443
        volumes:
            - ./:/var/www/html
            - ./.docker/nginx/conf.d/:/etc/nginx/conf.d/
        networks:
            - my_network

Each has its own docker file as well.

Both API and APP have the same .docker/site.dockerfile

FROM php:7.4-fpm

USER root

WORKDIR /var/www/html

# just a bunch of RUN commands to install PHP dependencies

EXPOSE 9000
CMD ["php-fpm"]

Nginx conf for API .docker/nginx/conf.d/app.conf

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass my_api:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

Then Nginx conf for APP .docker/nginx/conf.d/app.conf

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass my_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;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

After which, I added these lines on my Windows host file.

127.0.12.10     api.localhost
127.0.12.12     app.localhost

Both http://api.localhost and http://app.localhost are able to run on my web browser. However, when the APP do a curl request to the API, I get the following error.

cURL error 7: Failed to connect to api.localhost port 80: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://api.localhost

Both are residing in the same network when I execute docker network inspect my_network.

"Containers": {
    "0d77c5cc22c36f8086eb8e5d580562354c4c6b3c7bdd962e0f8cebfa7a3d844b": {
        "Name": "my_api",
        "EndpointID": "93bb1cd93ccd09b7c1fd2de3e074525b2fc98919eba0cf2cc96c2112d4e1e1b5",
        "MacAddress": "02:42:ac:17:00:09",
        "IPv4Address": "172.23.0.9/24",
        "IPv6Address": ""
    },
    "53432448ea91677c643d2f1ff206dcfdc6209741ba9951da342756f752672ab8": {
        "Name": "webserver_my_app",
        "EndpointID": "09ec9ae65461bcc42a750fb0a4fee8305b299c1ad2f1b8e16c65902119489b0d",
        "MacAddress": "02:42:ac:17:00:03",
        "IPv4Address": "172.23.0.3/24",
        "IPv6Address": ""
    },
    "55c2fef084b249a7b3537c641c7101276d9130c6d81d77b21e88206bd10001ec": {
        "Name": "webserver_my_api",
        "EndpointID": "ad1d3b1bedd140a26aec05de276f6f5edc0631b22fa2e77595f5bdf63d9eabca",
        "MacAddress": "02:42:ac:17:00:08",
        "IPv4Address": "172.23.0.8/24",
        "IPv6Address": ""
    },
    "dedfe7f75ef76e913b4483708b9920b7d94f8d4931db74e9831cc86194113d75": {
        "Name": "my_app",
        "EndpointID": "80f2c8e1b83817dda62c682f9ed8e098d8cd3cf4c6d05b3af145e58660368779",
        "MacAddress": "02:42:ac:17:00:05",
        "IPv4Address": "172.23.0.5/24",
        "IPv6Address": ""
    }
},

I am able to do a request via postman as well with no issues. It is just when the APP do a curl on the API.

Anything I missed out?

Thank you

EDIT:

enter image description here

Added screenshot from docker ps command

2 Answers 2

2

I was able to find the answer after hours of trial and error.

Instead of using the alias, in my case api.localhost, I used the IPv4 set on the container.

To get the IPv4, I ran docker network inspect my_network which gave me this list.

"Containers": {
    "0d77c5cc22c36f8086eb8e5d580562354c4c6b3c7bdd962e0f8cebfa7a3d844b": {
        "Name": "my_api",
        "EndpointID": "93bb1cd93ccd09b7c1fd2de3e074525b2fc98919eba0cf2cc96c2112d4e1e1b5",
        "MacAddress": "02:42:ac:17:00:09",
        "IPv4Address": "172.23.0.9/24",
        "IPv6Address": ""
    },
    "53432448ea91677c643d2f1ff206dcfdc6209741ba9951da342756f752672ab8": {
        "Name": "webserver_my_app",
        "EndpointID": "09ec9ae65461bcc42a750fb0a4fee8305b299c1ad2f1b8e16c65902119489b0d",
        "MacAddress": "02:42:ac:17:00:03",
        "IPv4Address": "172.23.0.3/24",
        "IPv6Address": ""
    },
    "55c2fef084b249a7b3537c641c7101276d9130c6d81d77b21e88206bd10001ec": {
        "Name": "webserver_my_api",
        "EndpointID": "ad1d3b1bedd140a26aec05de276f6f5edc0631b22fa2e77595f5bdf63d9eabca",
        "MacAddress": "02:42:ac:17:00:08",
        "IPv4Address": "172.23.0.8/24",
        "IPv6Address": ""
    },
    "dedfe7f75ef76e913b4483708b9920b7d94f8d4931db74e9831cc86194113d75": {
        "Name": "my_app",
        "EndpointID": "80f2c8e1b83817dda62c682f9ed8e098d8cd3cf4c6d05b3af145e58660368779",
        "MacAddress": "02:42:ac:17:00:05",
        "IPv4Address": "172.23.0.5/24",
        "IPv6Address": ""
    }
},

Since webserver_my_api is my nginx container, I use its IP address of 172.23.0.8. Then I ran curl_setopt($cURLConnection, CURLOPT_URL, 'http://172.23.0.8'); on my app and it now works!!!

Though this solution works, I still don't see it as the actual solution. The problem with this is that when I restart the containers, these IP addresses will change.

Any long term solution for this? Also, I still want to use api.localhost rather than the IP address.

Thank you

Sign up to request clarification or add additional context in comments.

Comments

1

When you bind ports with an IP adresse (e.g. : 127.0.12.12:80:80), you limit the connections to that particular adresse. More here

3 Comments

The reason I have to assign an IP is to set it on my windows host file as I run multiple websites on my local machine. If I do not assign an IP, then it will default to http://localhost:<some_port_here>
I understand. But that does not change the way that works.
May I know what else is needed for the app to be able to do a rest call to the api endpoint? I did try your suggestion and still doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.