【发布时间】:2026-02-01 08:35:01
【问题描述】:
我正在尝试对我的 angular + express 应用程序进行 dockerize。我有一个创建两个容器的 docker-compose 文件,我可以从我的主机(使用我的浏览器)中访问容器,但是每当我尝试从 http 访问后端时,我只会得到一个“ERR_NAME_NOT_RESOLVED”我的前端提出的请求。
我查看了这个问题,似乎大多数人建议服务名称和容器端口应该足以在它们位于同一网络上时击中另一个容器。我尝试点击“http://express:8000/user?user=030f0e70-9a8f-11e9-b5d1-f5cb6c0f3616”,鉴于我从其他地方看到的情况,我认为它应该可以工作,但无论如何,我都会遇到同样的错误。
我的 docker-compose 文件看起来像
version: '3' # specify docker-compose version
# Define the services/containers to be run
services:
angular: # name of the first service
build: ./ # specify the directory of the Dockerfile
ports:
- "4200:80" # specify port forewarding
links:
- "express"
depends_on:
- "express"
express: #name of the second service
build: # specify the directory of the Dockerfile
context: ./
dockerfile: dockerfile.be
ports:
- "8000:8000" #specify ports forewarding
expose:
- "8000"
理想情况下,我希望我的前端能够使用设置的端点访问另一个容器,这样我就可以以最少的更改部署应用程序。我会很感激任何建议。我觉得我错过了一些非常简单的东西,但是经过几个小时的修补,我仍然没有抓住它。
谢谢!
【问题讨论】:
-
描述遇到
ERR_NAME_NOT_RESOLVED,ajax时谁向哪个部分发送请求?以及详细的网址。 -
我只是使用 Angular HttpClient 将请求发送到我的快递后端。不使用容器时,我可以点击“localhost:8000/user?user=030f0e70-9a8f-11e9-b5d1-f5cb6c0f3616”并从后端获取数据到我的 Angular 应用程序。
标签: docker docker-compose dockerfile