简体   繁体   中英

Cannot use NGINX subdomain inside the Docker container

This question is kinda stupid, it's about using the Docker's service names as hostnames, so here's the context:

I am running the following NGINX containers: base, php-fpm and nginx. I also have a Laravel project who is located in the root project, in the /api folder. I also run haproxy on port 5000 for load balancing the requests over php-fpm containers.

The base container contains the linux environment from which i can run commands to phpunit, npm and literally have access to other containers' files that are sent using the volume from docker-compose.

The php-fpm contains the environment for PHP to run.

The nginx contains the NGINX server which is configured to hold two websites: the root website (localhost) and the api subdomain (api.localhost). The api. subdomain points to the /api folder within the root project, and the root website (localhost) points to the /frontend folder within the root project.

The problem is that within the base service container, i cannot run curl command to access the api.localhost website. I tried to use curl to access the nginx using the service name within the docker-compose (which is nginx):

$ curl http://nginx

and it works perfectly, but the frontend folder answers with code from the frontend folder. I have no idea how to use the service name to access the api.localhost wihin the container. I have tried

$ curl http://api.nginx $ curl http://api.localhost

Not even the localhost answers to the curl command:

$ curl http://localhost

Is there any way i can access the subdomain from a NGINX container using the service name as hostname?

I have found out that subdomains are not working well using NGINX and Docker Service name as hostname.

Instead, i had to change the structure of my project so that i don't use subdomains while trying to access URLs using service names as hostnames.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM