I have 2 applications in NET 6 an api that connects to the database and an MVC application that connects to the api.
These applications are with Docker compatibility. When I compile the Docker file these are executed correctly. But when I do it with docker-compose it just runs the MVC app and it obviously fails because it can't connect to the api container.
Here my files:
Dockerfile API
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
RUN sed -i 's/TLSv1.2/TLSv1/g' /etc/ssl/openssl.cnf
EXPOSE 81
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Apis/XXXXXXX/XXXXXXX.csproj", "Apis/XXXXXXX.Apis/"]
COPY ["application/XXXXXXX.Application/XXXXXXX.Application.csproj", "application/XXXXXXX.Application/"]
COPY ["domain/XXXXXXX.Domain/XXXXXXX.Domain.csproj", "domain/XXXXXXX.Domain/"]
COPY ["infrastructure/XXXXXXX.Infrastructure.Persistence/XXXXXXX.Infrastructure.Persistence.csproj", "infrastructure/XXXXXXX.Infrastructure.Persistence/"]
RUN dotnet restore "Apis/XXXXXXX.Apis/XXXXXXX.Apis.csproj"
COPY . .
WORKDIR "/src/Apis/XXXXXXX.Apis"
RUN dotnet build "XXXXXXX.Apis.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "XXXXXXX.Apis.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "XXXXXXX.Apis.dll"]
Dockerfile MVC
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Web/WebRentabilidad/WebXXXXXX.csproj", "Web/WebXXXXXX/"]
RUN dotnet restore "Web/WebXXXXXX/WebXXXXXX.csproj"
COPY . .
WORKDIR "/src/Web/WebXXXXXX"
RUN dotnet build "WebXXXXXX.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebXXXXXX.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebXXXXXX.dll"]
docker-compose.yml
version: '3.4'
services:
WebXXXXX:
image: webXXXXX:latest
container_name: webXXXXX_container
ports:
- "8181:80"
environment:
- ASPNETCORE_ENVIRONMENT=Development
networks:
XXXXX_docker_network:
ipv4_address: "172.16.238.10"
build:
context: .
dockerfile: ./Web/WebXXXXX/Dockerfile
ApiRentabilidad:
image: XXXXXapis:latest
container_name: apiXXXXX_container
ports:
- "8282:81"
environment:
- ASPNETCORE_ENVIRONMENT=Development
networks:
XXXXX_docker_network:
ipv4_address: "172.16.238.11"
build:
context: .
dockerfile: Apis/XXXXX.Apis/Dockerfile
networks:
XXXXX_docker_network:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
HomeController
public async Task<IActionResult> Index()
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://172.16.238.11:8282/api/v1/{id}/post"");
ViewBag.Post= null;
if (response.IsSuccessStatusCode)
{
var post= await response.Content.ReadFromJsonAsync<Post>();
ViewBag.Post= post;
}
return View();
}
I appreciate any help
CodePudding user response:
On the docker network, you access the containers using the unmapped port numbers. The mapped port numbers are only for accessing the containers from the host.
You haven't changed the port number for the API container, so it listens on port 80 which is the default. EXPOSE doesn't do anything and should only be used for documentation.
So to connect to it from the MVC container, you can omit the port number, since port 80 is the default for http
client.GetAsync("http://172.16.238.11/api/v1/{id}/post");
You can also use the container name rather than the IP address, so it becomes
client.GetAsync("http://apiXXXXX_container/api/v1/{id}/post");
In your docker-compose file, you map the port from the container. That's only necessary if you need to access the API container from the host machine. If you do need to access it, the mapping should be of port 80, so you'd do
ports:
- "8282:80"