Docker容器正在使用我的主机DNS而不是Docker内部DNS
我有一个docker-compose文件,该文件构建了两个基本应用程序。 前端,有角度和Adonis的后端。
version: "3.9"
services:
backend_login:
build:
context: ./apis/apis/login_api
ports:
- "3333:3333"
sendit_frontend:
build:
context: ./frontend/frontend/external
ports:
- "4500:4200"
depends_on:
- api_backend_login
当我运行Docker-Compose时,这两个应用程序会增加,它们在各自的端口中工作。 当我使用服务名称(在这种情况下为Backend_login)调用前端应用程序的API时,问题出在问题。
我将终端附加到两个容器上,并在它们之间进行了telnet测试,它们似乎可以在y解决名称并连接到相应端口的情况下工作,
最后我发现,当从Firefox调用时,前端应用程序试图尝试使用主机DN,而不是内部Docker DNS解决名称。
现在,我不知道问题在哪里,是否在代码中或以我构建容器的方式。
编辑:我意识到该应用程序正在主机计算机的浏览器中运行(我是带有容器的新手),显然它使用了我的主机DNS。
有办法解决这个问题吗?
I have a docker-compose file that builds two basic application.
A front-end with angular and a backend with adonis.
version: "3.9"
services:
backend_login:
build:
context: ./apis/apis/login_api
ports:
- "3333:3333"
sendit_frontend:
build:
context: ./frontend/frontend/external
ports:
- "4500:4200"
depends_on:
- api_backend_login
When i run the docker-compose up the two applications go up and they work in their respective ports.
The problems comes when i call the api from the front-end app using the name of the service (in this case backend_login).
I attached my terminal to both containers and did a telnet test between them, they seems to work as the y resolve the name and connect to the respective ports
Finally, i discovered that the front-end app, when is called from firefox, tries to resolve the name using the host dns and not the internal docker dns.
Now i don't know where is the problem, if it is in the code or in the way that i build the container.
Edit: I realize that the app is running in the browser of my host machine (i'm a newbie with containers) and obviously it uses my host DNS.
Is there a way to workaround this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Docker组合设置了一个docker网络,用于撰写文件中的所有服务,以允许其启动您正在谈论的DNS规则,但仅从服务到服务。如您指出的那样,您的Angular应用程序在技术上是从浏览器(例如Firefox)“运行”的,这意味着它不在网络中。
从容器环境内部运行水疗中心或静态站点(例如Angular)没有一种实用的方法,但是围绕问题的简单方法是将前端指向
http:// localhost:3333
。您已将端口映射到主机,因此应该直接从主机网络可用。注意:当您到达那里时,这将是产品的问题。当涉及到动态的后端地址时,水疗和静态站点可能很棘手。大多数水疗框架建议您每次部署到新环境中重新构建应用程序,以便可以将后端地址内置到工件中,但是这往往是以容器为中心的开发的方法,这些开发倾向于维护哲学的“构建一次,在任何地方部署”。
docker compose sets up a docker network for all services inside the compose file that allows it to instrument the DNS rules you're talking about, but only from service to service. As you noted, your Angular application is technically "run" from the browser (e.g. Firefox) which means it's not inside the network.
There's not a practical way to run SPAs or static sites like Angular from inside the container environment, but the simple way around your problem would be to point the frontend to
http://localhost:3333
. You've mapped the port to the host so it should be available from the host network directly.Note: this will be a problem for prod when you get there too. SPAs and static sites can be tricky when it comes to dynamic backend addresses. Most SPA frameworks recommend that you re-build the app every time you deploy to a new environment so that the backend address can be built into the artifact, but that tends to be at-odds with container-centric development which tend to maintain the philosophy of "build once, deploy anywhere".