我对 Docker 还很陌生。我创建了一个 docker-compose,它有 1 个主应用程序 (laravel) 站点和 2 个 api。
我正在尝试从 laravel 站点访问 api,但不断收到:
cURL error 7: Failed to connect to 0.0.0.0 port 8082: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
我的 docker-compose.yml 是:
version: '3'
services:
web:
image: nginx:1.10
volumes:
- ./vhost.conf:/etc/nginx/conf.d/default.conf
ports:
- "8080:80"
depends_on:
- app
restart: always
app:
build: . #run the dockerfile to install whatever
env_file: .env
volumes:
- ./:/var/www
depends_on:
- database
web-api2:
image: nginx:1.10
volumes:
- ../api/api2/vhost.conf:/etc/nginx/conf.d/default.conf
ports:
- "8081:81"
depends_on:
- app-api2
app-api2:
build: . #run the dockerfile to install whatever
env_file: .env
volumes:
- ../api/api2:/var/www
depends_on:
- database
web-api1:
image: nginx:1.10
volumes:
- ../api/api1/vhost.conf:/etc/nginx/conf.d/default.conf
ports:
- "8082:82"
depends_on:
- app-api1
app-api1:
build: . #run the dockerfile to install whatever
env_file: ../api/api1/.env
volumes:
- ../api/api1:/var/www
depends_on:
- database
database:
image: mysql:5.7
environment:
- "MYSQL_ROOT_PASSWORD=IAmSoSecure"
- "MYSQL_DATABASE=my_app"
ports:
- "33061:3306"
volumes:
- my_app:/var/lib/mysql
restart: always
volumes:
wildfire:
我研究过 docker 网络,但我的尝试失败了。我有一个网络设置,并尝试使用子网,但没有成功:
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
我也尝试访问我启用的端口:
"Ports": {
"443/tcp": null,
"80/tcp": null,
"82/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8082"
}
]
},
我像往常一样通过 Guzzle 请求通话:
$client = new Client();
$res = $client->get('127.0.0.1:8082/accounts');
dd($res->getBody()->getContents());
我尝试了不同的 IP,但找不到正确的 IP。
我当前的 vhost.conf 是:
server {
listen 80;
index index.php server.php;
root /var/www/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html server.php index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
include /etc/nginx/mime.types;
}
location ~ /\.(?!well-known).* {
deny all;
}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
}
最佳答案
在 docker compose 创建的默认网络上,编排器管理的每个容器都可以通过其服务名称进行访问,该服务名称恰好与 docker-compose.yaml
中为其指定的名称相同。文件。因此,在您的情况下,同一网络上的每个容器可能会达到 app-api2
正如您的问题详细信息:
http://app-api2:82/accounts
要获得更完整的答案,请使用 127.0.0.1
从容器内意味着您正在尝试访问容器本身内的服务并监听(在您的情况下)82
。由于您没有在同一容器内运行并监听该端口的其他可执行文件,因此您正确接收 Connection refused
.
了解您并不严格需要公开 docker-compose.yaml
中的端口可能对您也很有用。文件供同一网络上的其他容器进行通信。公开的端口使您能够从主机发出请求(实际上您将使用 127.0.0.1
)。
如果出于某种原因您确实想要使用容器的 IP 地址在容器之间进行通信,那么您可以使用 docker inspect <container name>
找到特定容器的当前 IP。 。但请注意,此 IP 是短暂的,并且每次容器启动时都可能会发生变化。
关于laravel - Docker Web 和 api 拒绝连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52246250/