Home > OS >  configuring communication between docker apps
configuring communication between docker apps

Time:10-04

my app is composed of dotnet backend, and angular frontend. During creation I ran them both separately (dotnet run, ng serve) in separate terminals. they were both running on localhost.

Now in order to deploy them I have containerized each one of them as a docker image on docker hub. I can get each one of them to run separately with docker run - but I can't get them to communicate.

I don't completely understand how localhost works in conjunction with containers and virtual machines, and am not sure how I need to configure them in order to make them work together. I have read up a lot, but haven't been able to find anything that touches exactly on this. any help would be amazing.

thanks!

I'm not sure which files deal with this so I am adding all the things that seem relevant:

the dockerfile for the angular:

FROM node:latest as node
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build --prod

FROM nginx:alpine
COPY --from=node /app/dist/web-pet-shop /usr/share/nginx/html

angular- environment.ts:

export const environment = {
  production: false,
  apiUrl: 'https://localhost:5001/api/'
};

environment.prod.ts:

export const environment = {
  production: true,
  apiUrl: 'api/'
};

proxy.conf.json:

{
    "/api": {
      "target": "https://localhost:5001",
      "secure": false
    }
  }

from the backend-

the dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS runtime
WORKDIR /app
COPY published/ ./
ENTRYPOINT ["dotnet", "API.dll"]

from startup->configure

            app.UseCors(policy => policy
            .AllowAnyHeader()
            .AllowAnyMethod()
            .WithOrigins("https://localhost:4200"));

launchsettings.json:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:29610",
      "sslPort": 44320
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "API": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

CodePudding user response:

You need to see the 2 containers as 2 different machines (computers/laptops). So when one of them says localhost then it is that host alone. It doesn't know about the other one.

For these 2 containers to see each other they need to be on the same network and then they can call each other by name.

# create a network
docker network create my_network

# start frontend container
docker run \
  -d --name frontend \
  --network my_network \
  -p <your_exposed_port> \
  <frontend_image>

# start backend container
docker run \
  -d --name backend \
  --network my_network \
  <backend_image>

With this setup, the frontend can talk to the backend on http://backend:<port>.

You can also use docker-compose then to make things easier.

  • Related