我在本地服務器上定義了一個子域,我想從 docker 容器請求子域,但容器無法決議子域。本地服務器使用 nginx 并且不是 docker 容器。
本地 nginx 配置
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;
}
}
本地服務器主機 - /etc/hosts
127.0.0.1 car.localhost
docker容器中的卷曲錯誤
echo shell_exec("curl http://car.localhost 2>&1");
Fatal error: Uncaught Exception: [0] cURL error 6: Could not resolve host: car.localhost
uj5u.com熱心網友回復:
Docker 容器有自己的 hosts 檔案,并不關心您的主機中的/etc/hosts.
如果您希望容器有額外的主機,您可以將其作為引數傳遞給 docker run,請參見此處:https ://docs.docker.com/engine/reference/run/#managing-etchosts
或者在 docker-compose 檔案中看到這里:https ://docs.docker.com/compose/compose-file/#extra_hosts
此外,取決于網路 - 容器127.0.0.1可能不指向主機。如果您使用的是host網路,那么它應該可以作業。但是,如果您使用的是網橋或默認配置,那么它會有所不同。在這種情況下,您可以docker.internal在 MAC/PC 上使用或在 linux 上host.docker.internal使用 spceial --add-host=host.docker.internal:host-gateway。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/487229.html
