skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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.

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