Dra*_*ica 5 core-api docker docker-compose angular docker-network
首先,我是 docker 容器的新手,我不太了解它们是如何运行的,但我有这样的设置:
该 API 可通过https://localhost:44384/api/weatherforecast 访问 容器化角度应用程序可通过https://localhost:4200访问
如果我在没有 docker 的情况下运行 Angular 应用程序,则在使用代理修复 CORS 问题后,我可以连接到https://localhost:44384/api/weatherforecast。但是,当我运行 dockerized 版本时,我收到以下错误:
Failed to load resource: the server responded with a status of 504 (Gateway Timeout)
Run Code Online (Sandbox Code Playgroud)
在 VS 控制台中我看到以下错误:
clearance_1 | [HPM] Error occurred while trying to proxy request /api/weatherforecast from loccoalhost:4200 to https://localhost:44384 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
Run Code Online (Sandbox Code Playgroud)
这似乎是一个连接问题,所以我在互联网上进行了一些挖掘,并尝试将这两个容器置于同一网络下。
步骤如下: 1. 创建桥接“测试”网络
docker network create test
Run Code Online (Sandbox Code Playgroud)
docker网络连接测试[容器ID 1]
docker网络连接测试[容器ID 2]
其他潜在有用的东西:
docker-compose.yml :
version: "3.7"
services:
clearance:
build:
# network: host
context: .
dockerfile: DockerFileLocal
ports:
- 4200:4200
volumes:
- /app/node_modules
- .:/app
Run Code Online (Sandbox Code Playgroud)
代理.conf.json
{
"/api/*": {
"target": "https://localhost:44384",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
Run Code Online (Sandbox Code Playgroud)
我缺少什么?
我遇到了类似的问题,一个 Angular 应用程序和一个 API 都在单独的 Docker 容器中运行。
通过以下设置,这两个应用程序都可以单独正常工作:
Angular运行在http://localhost:4200
API运行在http://localhost:8080
Angular 应用程序无法访问 API。
下面的代码总是给我带来网络相关的错误。
this.http.get('http://localhost:8080').subscribe(console.log);
Run Code Online (Sandbox Code Playgroud)
链接
链接到另一个服务中的容器。同时指定服务名称和链接别名 (SERVICE:ALIAS),或者仅指定服务名称。可以通过与别名相同的主机名或服务名称(如果未指定别名)访问链接服务的容器。
当一个容器需要通过网络到达另一个容器时,我们需要创建一个链接。我最终在服务定义中
创建了指向该api服务的链接angulardocker-compose.yml
version: "2"
services:
api:
build:
context: .
dockerfile: ./api/Dockerfile
volumes:
- ./api:/usr/src/app
ports:
- "8080:8080"
angular:
build:
context: .
dockerfile: ./angular/Dockerfile
volumes:
- ./angular:/usr/src/app
ports:
- "4200:4200"
links:
- api
Run Code Online (Sandbox Code Playgroud)
然后我通过在 Angular 应用程序中localhost替换来修复网络错误。api
this.http.get('http://api:8080').subscribe(console.log);
Run Code Online (Sandbox Code Playgroud)
你不需要代理。但是,您可能需要调整您的配置才能使其正常工作。
| 归档时间: |
|
| 查看次数: |
10068 次 |
| 最近记录: |