简体   繁体   中英

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. I would like them to communicate internally through the networking of docker itself and not have to go through the host.

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

"backend-container-name/api/resource" instead of " 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.

Here is an example of docker compose file.

version: "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. Similarly you can create an API server and use the service name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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