Home > Enterprise >  Cannot access to dockerized net 6 api
Cannot access to dockerized net 6 api

Time:01-23

My api project (my-custom-api) is using rabbitmq instance. Since both are running in docker I created custom docker network and run both containers on that same network.

docker create my-network

docker run -d --hostname my-rabbitmq --network my-network --rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3.11-management

docker run --network my-network --publish 8090:8080 -d my-custom-api

on docker ps -a both containers are up and running and inside docker log for my-custom-api there is no errors or warnings

However when I hit localhost:8090/swagger/index.html I'm getting This site can’t be reached. Using curl localhost:8090 I'm getting url: (52) Empty reply from server

Here's the content of Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /App

# Copy everything
COPY . ./
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /App
COPY --from=build-env /App/out .
ENTRYPOINT ["dotnet", "My-Custom-Api.dll"]

Again, I'm not getting any errors in the docker log when trying to access localhost:8090 but still cannot access api from this address.

What I'm doing wrong here.

CodePudding user response:

Swagger is not available by default when your program runs in a container.

In your Program.cs, you should see an if statement that makes it so Swagger is only available when your program runs in a development environment. A container is - by default - not considered development.

To run the program in development mode, you need to set the ASPNETCORE_ENVIRONMENT variable to 'Development', like this

docker run --network my-network --publish 8090:8080 -d -e ASPNETCORE_ENVIRONMENT=Development my-custom-api

When you do that, Swagger will be available.

CodePudding user response:

You need to tell Docker to expose the container port from within the Dockerfile itself.

Somewhere you need to include

EXPOSE {yourPort}
  • Related