I have defined a subdomain on my local server and I want to request to subdomain from docker container but the container can not resolve the subdomain. local server uses nginx and is not a docker container.
Local nginx config
server {
listen 80;
root /data/www/html/sites/car/public;
index index.php index.html index.htm;
server_name car.localhost;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Local server hosts - /etc/hosts
127.0.0.1 car.localhost
Curl error in docker container
echo shell_exec("curl http://car.localhost 2>&1");
Fatal error: Uncaught Exception: [0] cURL error 6: Could not resolve host: car.localhost
CodePudding user response:
Docker containers have their own hosts file and do not care what you have in your host's /etc/hosts
.
If you want the container to have extra hosts you can pass that as an argument to the docker run see here: https://docs.docker.com/engine/reference/run/#managing-etchosts
Or in docker-compose file see here: https://docs.docker.com/compose/compose-file/#extra_hosts
Moreover depending on the network - the container's 127.0.0.1
might not point to the host machine. If you're using host
network then it should work. But if you're using a bridge or the default config then it will be different. In this case you can use docker.internal
on MAC/PC or host.docker.internal
with the spceial --add-host=host.docker.internal:host-gateway
on linux.