简体   繁体   English

如何使用容器名称而不是静态 IP 在 Angular 9 容器和 ASP.NET Core 3.1 容器之间建立通信?

[英]How to establish communication between Angular 9 container and ASP.NET Core 3.1 container using container names instead of static IP?

So I have 2 containers running on my local machine.所以我有 2 个容器在我的本地机器上运行。 I would like them to communicate internally through the networking of docker itself and not have to go through the host.我希望他们通过 docker 本身的网络进行内部通信,而不必通过主机。

for example: I would like to make a call to an API from the angular container to the backend container and I want to achieve something like this in the url that I am writing inside my angular code例如:我想从 angular 容器调用一个 API 到后端容器,我想在我在我的 angular 代码中编写的 url 中实现类似的功能

"backend-container-name/api/resource" instead of " http://natIP:port/api/resource " “后端容器名称/api/resource”而不是“ http://natIP:port/api/resource

If you're using Docker compose to provision the containers, you can use the service names as the container names and communicate.如果您使用 Docker compose 来配置容器,则可以使用服务名称作为容器名称并进行通信。

Here is an example of docker compose file.这是 docker compose 文件的示例。

version: "3"版本:“3”

services:
    web:
        build: .
        ports:
            - "8000:80"
        depends_on:
            - db
    db:
        image: "mcr.microsoft.com/mssql/server"
        environment:
            SA_PASSWORD: "Your_password123"
            ACCEPT_EULA: "Y"

And you can refer the SQL Server name as db in the connection string.您可以在连接字符串中将 SQL Server 名称称为 db。 Similarly you can create an API server and use the service name.同样,您可以创建 API 服务器并使用服务名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM