0

I'm trying to make a GET/POST request by hostname like http:://super.localhost instead of container name.

My super.localhost (nginx container) website available from internet browser, but from php container I can't make curl GET request (connection refused). Only by container name works. PHP script has 100+ hosts by CURL all of them on nginx container.

> CURL -i nginx is works. > CURL -i super.localhost doesn't work. Any ideas hot to reach the response?

Everything on the one network. Ports for nginx 80:80

version: '3'
services:
    nginx:
        container_name: webshop-nginx
        build:
            context: .
            dockerfile: ./nginx/nginx.docker
        ports:
            - "80:80"
            - "8080:80"
        volumes:
            - ./app:/var/www/app
            - ./nginx/site.conf:/etc/nginx/conf.d/default.conf
        working_dir: /var/www/app
        depends_on:
            - php-fpm
        networks:
            - local
Adobe
  • 558
  • 5
  • 23
  • Seems this solution will work for you:https://stackoverflow.com/questions/49719949/stack-service-containers-dynamic-hostname – vinay Aug 05 '21 at 14:53

2 Answers2

0

You can add the hostname configuration option in your compose file, like this example:

version: '3'
services:
    nginx:
        image: nginx
        container_name: nginx-1
        hostname: nginx-1
        ports:
            - "8080:80"
    nginx2:
        image: nginx
        container_name: nginx-2
        hostname: super.localhost
        ports:
            - "8081:80"

So, when in the nginx-1 container you make curl -i http://super.localhost/index.html you can reach the nginx-2 container.

Enrique Tejeda
  • 356
  • 1
  • 6
  • Yeah, I thought about this case too. It's going only if you have a few servers, then you can bind them by different service-hosts. But it's not appropriate case if you have dynamic host. `host1.local`, `host2.local`, ... `host9999.local` – Adobe Aug 04 '21 at 18:09
0

Actually for available hosts from inside of container need to use the aliases

Aliases need to add in web server service of docker-compose.yml

   networks:
        local:
            aliases:
                - host1.localhost
                - host2.localhost
                - host3.localhost
Adobe
  • 558
  • 5
  • 23