host.docker.internal will not resolve in docker-compose - c#

So I have a .NET solution with two projects. These work fine when running with dotnet run, but I'm having issues with my docker compose. When adding env variables with url paths to talk between containers, i generally use something like host.docker.internal to resolve the path to the other container, but for some reason that doesn't resolve and just gets used as, for instance, https://host.docker.internal:49833/connect/authorize instead of https://localhost:49833/connect/authorize.
This doesn't make sense to me as i literally have another project on my machine that runs with this setup just fine. what am i missing?
project 1 has a dockerfile like this:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ["RecipeManagement/src/RecipeManagement/RecipeManagement.csproj", "./RecipeManagement/src/RecipeManagement/"]
#COPY ["SharedKernel/SharedKernel.csproj", "./SharedKernel/"]
RUN dotnet restore "./RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"
# Copy everything else and build
COPY . ./
RUN dotnet build "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/build
FROM build-env AS publish
RUN dotnet publish "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "/app/RecipeManagement.dll"]
project 2 has a dockerfile like this:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
RUN apt install -y nodejs
# Copy csproj and restore as distinct layers
COPY ["AuthServerWithDomain/AuthServerWithDomain.csproj", "./AuthServerWithDomain/"]
#COPY ["SharedKernel/SharedKernel.csproj", "./SharedKernel/"]
RUN dotnet restore "./AuthServerWithDomain/AuthServerWithDomain.csproj"
# Copy everything else and build
COPY . ./
RUN dotnet build "AuthServerWithDomain/AuthServerWithDomain.csproj" -c Release -o /app/build
FROM build-env AS publish
RUN dotnet publish "AuthServerWithDomain/AuthServerWithDomain.csproj" -c Release -o /app/out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .
ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "/app/AuthServerWithDomain.dll"]
and the compose looks like this:
version: '3.7'
services:
recipemanagement-db:
image: postgres
restart: always
ports:
- '63230:5432'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: dev_recipemanagement
volumes:
- recipemanagement-data:/var/lib/postgresql/data
recipemanagement-api:
build:
context: .
dockerfile: RecipeManagement/src/RecipeManagement/Dockerfile
ports:
- "63231:8080"
environment:
ASPNETCORE_ENVIRONMENT: "Development"
ASPNETCORE_URLS: https://+:8080;
ASPNETCORE_Kestrel__Certificates__Default__Path: "/https/aspnetappcert.pfx"
ASPNETCORE_Kestrel__Certificates__Default__Password: "password"
DB_CONNECTION_STRING: "Host=recipemanagement-db;Port=5432;Database=dev_recipemanagement;Username=postgres;Password=postgres"
"AUTH_AUDIENCE": "recipe_management"
"AUTH_AUTHORITY": "https://host.docker.internal:49833"
# ^^^^^^^^^^^ not sure why this and the below don't resolve *************************************************************************
"AUTH_AUTHORIZATION_URL": "https://host.docker.internal:49833/connect/authorize"
"AUTH_TOKEN_URL": "https://host.docker.internal:49833/connect/token"
"AUTH_CLIENT_ID": "recipe_management.swagger"
"AUTH_CLIENT_SECRET": "974d6f71-d41b-4601-9a7a-a33081f80687"
"RMQ_HOST": "localhost:TBDRMQPORT"
"RMQ_VIRTUAL_HOST": "/"
"RMQ_USERNAME": "guest"
"RMQ_PASSWORD": "guest"
volumes:
- ~/.aspnet/https:/https:ro
recipemanagement-authserver:
build:
context: .
dockerfile: AuthServerWithDomain/Dockerfile
ports:
- "49833:8080"
environment:
"ASPNETCORE_ENVIRONMENT": "Development"
ASPNETCORE_URLS: "https://+:8080;"
ASPNETCORE_Kestrel__Certificates__Default__Path: "/https/aspnetappcert.pfx"
ASPNETCORE_Kestrel__Certificates__Default__Password: "password"
volumes:
- ~/.aspnet/https:/https:ro
volumes:
recipemanagement-data:

See #DavidMaze comment. Best to search on Networking in Compose as the starting point.
(The Dockerfile's should be irrelevant to your question.)
There are a couple of options, with trial and error to get the exact behavior you want. I'd encourage you to find better explanations on the following suggestions:
In your compose file, at the 'service' level, you can add extra_hosts
my-service:
extra_hosts:
host.docker.internal:host-gateway
#host.docker.internal:127.0.0.1 for linux
But when using compose, a better option is to have docker create a network specific to your containers with
docker network create --driver bridge my_recipe_ntwk
and then at the top level of your compose:
services:
volumes:
networks:
my-private-ntwk:
external:
name: my_recipe_ntwk
and then at the 'service' level
my-service-1:
networks:
- my-private-ntwk
my-service-2:
networks:
- my-private-ntwk
For postgres running on the host, see this answer and there should be a few other similar answers.
I believe if all services are configured on the same bridge network, you actually use the service name, e.g. my-service-1:8080 and don't inclue the https. I might be wrong. (In my case, my containers connect to Postgres on the host itself, and that requries 'extra_hosts' and it's own special workaround.) You are hosting a Postgres container, so I believe your connection host and port, when using the bridge network, would simply be my-private-ntwk:5432

Related

Unable to load the service index for source error for a CodeArtifact feed using a Dockerfile

It's my first question here so let me know if I have to change something or add more information.
We’re using AWS CodeArtifact for storing our packages and when we try to build a Docker image from our Dockerfile it fails because it's unable to load the source during the restore process.
We have a web API in .Net we want to deploy using AWS Fargate. This project runs smoothly from Visual Studio 2022 using Docker but we can’t build the image from PowerShell after adding our packages from CodeArtifact.
Our approach to include the credentials in the build is to pass the NuGet.Config stored on the host using Buildkit.
This’s our Dockerfile:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:49151
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
WORKDIR /src
COPY ["Src/Presentation/Project.API/Project.API.csproj", "Src/Presentation/Project.API/"]
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
--mount=type=secret,id=nugetconfig \
dotnet restore "Src/Presentation/Project.API/Project.API.csproj" \
--configfile /run/secrets/nugetconfig
COPY . .
WORKDIR "/src/Src/Presentation/Project.API"
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet build "Project.API.csproj" -c Release -o /app/build \
--no-restore
FROM build AS publish
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish "Project.API.csproj" -c Release -o /app/publish \
--no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Project.API.dll"]
Our script in PowerShell:
docker buildx build --secret id=nugetconfig,src=$HOME\AppData\Roaming\NuGet\NuGet.Config -f "Src\Presentation\Project.API\Dockerfile" -t my-dotnet-image .
Output:
#14 [build 6/9] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages --mount=type=secret,id=nugetconfig dotnet restore "Src/Presentation/Project.API/Project.API.csproj"
--configfile /run/secrets/nugetconfig
#14 1.175 Determining projects to restore...
#14 2.504 /src/Src/Presentation/Project.API/Project.API.csproj : error NU1301: Unable to load the service index for source
https://domain-123456789012.d.codeartifact.us-east-2.amazonaws.com/nuget/repository/v3/index.json.
What are we missing here? Once we have the Dockerfile working we want to use CDK for deploying the Docker image alongside our infrastructure.
We’re using aws codeartifact login command to authenticate with the service. We work on windows with a Linux container

Project not found when build docker container on Azure DevOps

I run commands to build and publish the docker image to registry but it fails with this error:
MSBUILD : error MSB1009: Project file does not exist.
Switch: ./Project.Api/Project.Api.csproj
Project structure
src
|--Project.Api
| |--Project.Api.csproj
|-- Dockerfile
tests
azure-pipelines.yml
Docker task in pipeline
stages:
...
- stage: build
pool:
vmImage: 'ubuntu-latest'
jobs:
- job: build_container
displayName: 'Build docker container image'
steps:
- bash: |
docker login -u $(REGISTRY_USER) -p $REGISTRY_PASSWORD $(REGISTRY_URL)
docker build -t $(REGISTRY_URL)/$(IMAGE_REPOSITORY):kkk . -f $(build.sourcesDirectory)/src/Dockerfile
docker push $(REGISTRY_URL)/$(IMAGE_REPOSITORY):kkk
displayName: 'Build and push docker image'
env:
REGISTRY_PASSWORD: $(REGISTRY_PASSWORD)
my Dockerfile:
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 . /src
RUN dotnet restore ./Project.Api/Project.Api.csproj
RUN dotnet publish ./Project.Api/Project.Api.csproj --no-restore -c Release -o /app
FROM build AS publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Project.Api.dll"]
Does anyone know that is the cause of this error and how to fix it? Thank you.
Follow standard troubleshooting steps: Check what directory you're running that bash command out of. Put a RUN ls or similar command in your Dockerfile so you can check the directory structure within your container during build and confirm it's what you expect.
Your Docker context is likely set a directory level higher than you think it is.
You can set your working directory appropriately from there.
- bash: |
docker ..
workingDirectory: $(build.sourcesDirectory)/src
I'd guess that your code is ending up in the directory /src/src/ in your Dockerfile.

Dotnet Cannot Restore Project Correctly While Using Docker

I have a .Net webapi project, and I want to dockerize it. Without docker, dotnet sdk(6.0) can restore everything correctly and there is no obstacle for me to publish the app. But when I use docker to do that, it gives me tones of errors with the error codes CS0246, CS8714, CS0103, CS0535, CS0738. You can find my project folder structure and relevant information below:
src
|-->API
| |-->API.csproj
| |-->Some other stuff
tools
|-->Docker
| |-->Dockerfile
docker-compose.yml
#./src/Docker/Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source
COPY *.sln ./
COPY ./src/API src/API
WORKDIR /src/API
RUN dotnet restore
RUN dotnet publish -c release -o /src/API
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /src/API
COPY --from=build /src/API ./
EXPOSE 80/tcp
ENV ASPNETCORE_URLS http://*:80
ENTRYPOINT ["dotnet", "API.dll"]
#./docker-compose.yml
version: '3'
services:
api:
build:
context: .
dockerfile: tools/Docker/Dockerfile
ports:
- "5009:5009"
environment:
ASPNETCORE_ENVIRONMENT: "prod"
the .NET 6.0 SDK image is not official yet
you can use some RC version instead
try
ARG REPO=mcr.microsoft.com/dotnet/aspnet
FROM $REPO:6.0.0-rc.1-alpine3.14-amd64
follow here for updates:
https://hub.docker.com/_/microsoft-dotnet-sdk?tab=description

Azure functions v3 attach debugger to docker container in local started with docker-compose (dotnet)

I`m trying to incorporate the azure function (c#) in the already existing docker-compose file. By reverse engineering from how visual studio starting container and build image, I have ended up with something like this:
version: '3.4'
services:
azure.storage.emulator:
image: "mcr.microsoft.com/azure-storage/azurite:latest"
container_name: azure.storage.emulator
ports:
- 10000:10000
- 10001:10001
volumes:
- azurite_data:/data mcr.microsoft.com/azure-storage/azurite
networks:
- internal_network
azure.function:
build:
context: .
dockerfile: FunctionApp/Dockerfile
volumes:
- 'C:\Users\MyComputer\vsdbg\vs2017u5:/remote_debugger:rw'
- 'C:\Users\MyComputer\Desktop\docker infrastructure\TestFunction\FunctionApp\FunctionApp:/home/site/wwwroot'
- 'C:\Users\MyComputer\Desktop\docker infrastructure\TestFunction\FunctionApp:/src/'
- 'C:\Users\MyComputer\AppData\Local\AzureFunctionsTools\Containers_2109029\Releases\3.20.0\linuxCLI:/functions_debugging_cli:ro'
- 'C:\Users\MyComputer\.nuget\packages\:/root/.nuget/fallbackpackages2'
- 'C:\Program Files\dotnet\sdk\NuGetFallbackFolder:/root/.nuget/fallbackpackages'
ports:
- 8081:80
container_name: Azure.Function
networks:
- internal_network
entrypoint:
- tail
command:
- -f
- /dev/null
depends_on:
- azure.storage.emulator
environment:
- DOTNET_USE_POLLING_FILE_WATCHER=1
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://*:10123
- NUGET_PACKAGES=/root/.nuget/fallbackpackages2
- NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages;/root/.nuget/fallbackpackages2
- WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT=1,
- FUNCTIONS_WORKER_RUNTIME=dotnet
- AzureWebJobsStorage=DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://azure.storage.emulator:10000/devstoreaccount1;QueueEndpoint=http://azure.storage.emulator:10001/devstoreaccount1;
# NETWORKS
networks:
internal_network:
# VOLUMES
volumes:
azurite_data:
Hitting localhost/8081 I can verify that function is running, but I can not confirm that it does its job.
Another big problem is that when I attach to function with a remote debugger I can not debug it, because symbols are not loaded.
I would really appreciate it if anybody can help me with running and debugging the Azure function started through a docker-compose file.
There are several articles on the web, but I haven't had luck with the steps described in them.
my docker file looks like this:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/azure-functions/dotnet:3.0-appservice AS base
WORKDIR /home/site/wwwroot
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["FunctionApp/FunctionApp.csproj", "FunctionApp/"]
RUN dotnet restore "FunctionApp/FunctionApp.csproj"
COPY . .
WORKDIR "/src/FunctionApp"
RUN dotnet build "FunctionApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "FunctionApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /app/publish .
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
To summarize, I`m asking for help with starting and debugging azure functions v3 started by docker-compose
I have ended up with a custom image. It works fine in the local environment and debugger can be attached to it, but I`m not sure if suitable to deploy image to azure, probably not.
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
RUN apt-get update
RUN apt-get -y install curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_11.x | bash -
RUN apt-get -y install nodejs
RUN npm i -g azure-functions-core-tools#3 --unsafe-perm true
RUN apt-get install -y unzip
RUN curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg
WORKDIR /src
COPY ["FunctionApp/FunctionApp.csproj", "FunctionApp/"]
RUN dotnet restore "FunctionApp/FunctionApp.csproj"
COPY . .
WORKDIR "/src/FunctionApp"
ENTRYPOINT ["func","host","start","--verbose"]
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
In this case, I believe you'll have to do the profiling. You will have to follow these:
If its not a blessed image, then first you would have to install SSH if you want to get into the container.
Then you will have to make use of tools such as cProfile or other related python modules to profile the code.
Here is a documentation for windows application. You might want to take a look : https://azureossd.github.io/2017/09/01/profile-python-applications-in-azure-app-services/index.html
This issue has been tracked : https://github.com/Azure/azure-functions-docker/issues/17

.Net Core Web Api where is this random port assignment coming from?

I am using docker-compose to host a .Net Core Web application created using the template from .Net Core.
I have my docker-compose as follows:
version: '3.4'
services:
db:
image: mysql:5.7
container_name: mysql_db
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_HOST: "localhost"
MYSQL_ROOT_HOST: "%"
todoservice:
image: ${DOCKER_REGISTRY}todoservice
ports:
- "8000:80"
build:
context: .
dockerfile: ToDoService/Dockerfile
depends_on:
- db
volumes:
db_data:
In which I declare that my host machines port of 8000 will map to port 80 on this docker container. As well I have this dockerfile that defines how to build my .Net Core Web Api.
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY ToDoService/ToDoService.csproj ToDoService/
RUN dotnet restore ToDoService/ToDoService.csproj
COPY . .
WORKDIR /src/ToDoService
RUN dotnet build ToDoService.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish ToDoService.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ToDoService.dll"]
After I run docker-compose up -d I see that my ToDoService is listing this information for PORTS:
0.0.0.0:8000->80/tcp, 0.0.0.0:32772->80/tcp
So where is that random 32772 port assignment coming from?
When I specify the :443 it runs the debug (docker compose set to VS) consistently using the port I set (here I picked my port as 51836). Also I get the Swagger endpoint on the same port: https://localhost:51836/swagger/index.html
My docker-compose.yml:
version: '3.4'
services:
company.api.authservices:
image: ${DOCKER_REGISTRY-}company-api-auth-services
build:
context: .
dockerfile: Company.Api.AuthServices/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Production
ports:
- "51830:80"
- "51836:443"

Categories

Resources