Docker Aspnet core Setting physical directory path - asp.net

I am trying to run the aspnet core application In docker container. I am having issues with physical file provider.
In my application startup.cs I am using following code to for a physical file provider and map with alias
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider("G:\\Work\\LMS\\lms-data"),
RequestPath = new PathString("/lms-data"),
EnableDirectoryBrowsing = false
});
Now My docker file is
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
ENV ASPNETCORE_ENVIRONMENT=Development
ENV DOTNET_USE_POLLING_FILE_WATCHER=1
WORKDIR /app
EXPOSE 5000
EXPOSE 5001
COPY ["SharedKernal/SharedKernal.csproj", "SharedKernal/"]
COPY ["LMS.Entities/LMS.Entities.csproj", "LMS.Entities/"]
COPY ["LMS.Core/LMS.Core.csproj", "LMS.Core/"]
COPY ["LMS.Infrastructure/LMS.Infrastructure.csproj", "LMS.Infrastructure/"]
COPY ["LMS.Web/LMS.Web.csproj", "LMS.Web/"]
RUN dotnet restore "LMS.Web/LMS.Web.csproj"
RUN mkdir /lms-data
COPY . .
WORKDIR "/app/LMS.Web"
CMD [ "/bin/bash","-c","dotnet restore && dotnet watch run" ]
My docker compose file is:
version: "3.4"
services:
lmsapp:
image: lmsapp
container_name: lmsappv1
build:
context: .
dockerfile: Dockerfile
working_dir: "/app/LMS.Web"
volumes:
- ".:/app"
ports:
- "5000:5000"
- "5001:5001"
networks:
- mongo_network
mongodb:
image: mongo
container_name: mongo_db
networks:
- mongo_network
ports:
- "27017:27017"
networks:
mongo_network:
driver: bridge
Now when ever i run the the command docker-compose up after running docker-compose build
I receive following error
System.ArgumentException: The path must be absolute. (Parameter 'root')
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
at LMS.Web.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in /app/LMS.Web/Startup.cs:line 130
How to solve this error?
The other issue I am facing when ever I run the docker-compose up it always restore packages. How to avoid that?

Instead of this:
FileProvider = new PhysicalFileProvider("G:\\Work\\LMS\\lms-data"),
Try this:
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), #"lms-data"))

Related

ASP.NET Core on docker returns ERR_EMPTY_RESPONSE when browsing at localhost

When I try to run my ASP.NET Core application on docker through http://localhost:5004 I'm getting this response from my browser: ERR_CONNECTION_REFUSED
Here's my docker file
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:5.0.404 AS build
WORKDIR /code
COPY . .
# copy everything else and build app
RUN dotnet publish -c release -o /app
# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:5.0.13
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "ProductCatalogApi.dll", "--server.urls", "http://+:5004"]
my docker-compose.yml file (do not mind the password format):
version: "5.0.4"
networks:
frontend:
backend:
services:
catalog:
build:
context: .\src\Services\ProductCatalogApi
dockerfile: Dockerfile
image: shoes/catalog
environment:
- DatabaseServer=mssqlserver
- DatabaseName=CatalogDb
- DatabaseUser=sa
- DatabasePassword=(passwordhere)
- ASPNETCORE_URLS=http://+:5004
- ASPNETCORE_ENVIRONMENT=Production
container_name: catalogapi
ports:
- "5004:80"
networks:
- backend
- frontend
depends_on:
- mssqlserver
mssqlserver:
image: "mcr.microsoft.com/mssql/server:2019-latest"
ports:
- "1445:1433"
container_name: mssqlcontainer
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=(passwordhere)
- MSSQL_PID=Developer
networks:
- backend
CreateHostBuilder in Program.cs:
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
// webBuilder.UseKestrel(options => { options.Listen(IPAddress.Any, 5000); });
webBuilder.UseKestrel().UseUrls(Environment.GetEnvironmentVariable("ASPNETCORE_URLS"));
});
}
}
I can't figure what's missing or wrong on my configuration that I can't access the app on my local machine through localhost:5004(or whatever port ex. 5000 )
Note: I run this commands in the following order
docker-compose build
docker-compose up mssqlserver
docker-compose up catalog
When you set ASPNETCORE_URLS=http://+:5004, your app will listen on port 5004. So that's the port you should map to a port on the host. You've mapped port 80.
Change your docker-compose file to
ports:
- "5004:5004"
Now you should be able to access it. Remember that Swagger by default isn't available in the Production environment, so you won't be able to use the Swagger pages.
You've tried to configure what port the app listens on in a lot of different ways. A good idea might be to remove it all and only configure it in the launchSettings.json file. Then it'll listen on the port specified in launchSettings.json when you run it locally during development and it'll listen on port 80 when run in the container.
The reason it'll listen on port 80 when run in a container is that Microsoft set the ASPNETCORE_URLS environment variable to http://+:80 in the aspnet images.

docker-compose doesn't work from Visual Studio

I have three web projects in one solution, they work together on gRPC. I am trying to bring all three projects up with docker-compose, but the problem is when the command
docker-compose up --build
This is how everything works, but when I try to start using the Visual Studio interface with a debugger connected, it does not work
When you run the application via docker-compose in Visual Studio, then when you start the containers themselves, an error appears with the following content:
Can't find a program for debugging in the container
And then it immediately pops out this:
The target process exited without raising an event fired by CoreCLR. Make sure the target process is configured to use .NET Core. This may be necessary if the target process has not started in .NET Core.
This message appears in the container logs:
Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
* You intended to execute a .NET program:
The application '/app/bin/Debug/net5.0/Votinger.Gateway.Web.dll' does not exist.
* You intended to execute a .NET SDK command:
It was not possible to find any installed .NET SDKs.
Install a .NET SDK from:
https://aka.ms/dotnet-download
This is a Dockerfile which is auto-generated by Visual Studio, it is the same for every project except for the paths to the project
#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:5.0 AS base
WORKDIR /app
EXPOSE 5000
EXPOSE 5001
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Votinger.Gateway/Votinger.Gateway.Web/Votinger.Gateway.Web.csproj", "Votinger.Gateway/Votinger.Gateway.Web/"]
RUN dotnet restore "Votinger.Gateway/Votinger.Gateway.Web/Votinger.Gateway.Web.csproj"
COPY . .
WORKDIR "/src/Votinger.Gateway/Votinger.Gateway.Web"
RUN dotnet build "Votinger.Gateway.Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Votinger.Gateway.Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
FROM mcr.microsoft.com/dotnet/sdk:5.0
ENTRYPOINT ["dotnet", "Votinger.Gateway.Web.dll"]
docker-compose.yml
version: '3.4'
services:
votinger.authserver.db:
image: mysql:8
container_name: Votinger.AuthServer.Db
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
votinger.pollserver.db:
image: mysql:8
container_name: Votinger.PollServer.Db
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
votinger.authserver.web:
image: ${DOCKER_REGISTRY-}votingerauthserverweb
container_name: Votinger.AuthServer.Web
build:
context: .
dockerfile: Votinger.AuthServer/Votinger.AuthServer.Web/Dockerfile
links:
- votinger.authserver.db:authdb
votinger.gateway.web:
image: ${DOCKER_REGISTRY-}votingergatewayweb
container_name: Votinger.Gateway.Web
build:
context: .
dockerfile: Votinger.Gateway/Votinger.Gateway.Web/Dockerfile
ports:
- 5000:5000
links:
- votinger.authserver.web:authserver
- votinger.pollserver.web:pollserver
votinger.pollserver.web:
image: ${DOCKER_REGISTRY-}votingerpollserverweb
container_name: Votinger.PollServer.Web
build:
context: .
dockerfile: Votinger.PollServer/Votinger.PollServer.Web/Dockerfile
links:
- votinger.pollserver.db:polldb
docker-compose.override.yml
version: '3.4'
services:
votinger.authserver.web:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:5000;http://+:5001
- ASPNETCORE_Kestrel__Certificates__Default__Password=password
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/https:ro
votinger.gateway.web:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:5000;http://+:5001
- ASPNETCORE_Kestrel__Certificates__Default__Password=password
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/https:ro
votinger.pollserver.web:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:5000;http://+:5001
- ASPNETCORE_Kestrel__Certificates__Default__Password=password
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/https:ro
I will also post a link to GitHub where this project is located
(dev branch)
https://github.com/SeanWoo/Votinger
I will forgive your help
I found a solution, the problem was that in the docker-compose.vs.debug.yml file is generated by Visual Studio, full paths to the project and debugger are indicated there, and my path went through a folder named as C #, and Visual Studio decided to calculate the symbol # unnecessary and deleted it, it turned out to be a path with the C folder, which led to a completely different place

I can't connect my ASP .NET app from Docker-container to my computer host database with "host.docker.internal:some-port"

I lost amount of time trying to connect my app container with my database Azure Cosmos DB Emulator. I am using loggers object to know where my app break, and I found that the problem is in the connection of the container out of him. I tried to use the famous host.docker.internal direction to connect my host but using my container name (the public IP and DNS internal server of docker).
Here is my appsettings.Development configuration:
"DocumentDb": {
"TenantKey": "Default",
"Endpoint": "https://project-cosmos-container:8081",
"AuthorizationKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
},
Here my Dokerfile-Cosmos (here I copy my app .ddl that I created before with dotnet build and dotnet publish):
FROM microsoft/dotnet:2.2-aspnetcore-runtime
# We create the folder inside the container
WORKDIR /local-project
# We are coping all project executables that we created with dotnet build and dotnet publish
COPY ./bin/Release/netcoreapp2.2/publish/* ./
EXPOSE 8000
EXPOSE 8081
# We indicate to execute the program in the executable of the project
ENTRYPOINT ["dotnet", "Local.Proyect.Core.dll"]
And finnally my docker-compose where I run the app:
version: '3.1'
services:
local-Proyect:
image: project-cosmos-image
container_name: project-cosmos-container
ports:
- 127.0.0.1:7000:8000
environment:
ASPNETCORE_ENVIRONMENT: Development
ASPNETCORE_URLS: http://+:8000
Maybe the problem is in the ports, I don't Know. You can see that I am trying use my port 7000 on my computer host to connect the container and the port 8081 (azure cosmos port)
Unsing the following configuration with host.docker.internal:8081it works.
"DocumentDb": {
"TenantKey": "Default",
"Endpoint": "https://host.docker.internal:8081",
"AuthorizationKey": "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
},
So using the container name like DNS direction not works. I also have to do in Develpment environment because in other diferent host.docker.internal not works...
version: '3.1'
services:
local-Proyect:
image: project-cosmos-image
container_name: project-cosmos-container
ports:
- 127.0.0.1:7000:433
- 127.0.0.1:7001:80
environment:
ASPNETCORE_ENVIRONMENT: Development
ASPNETCORE_URLS: http://+:433;http://+:80

running dotnet docker container with docker-compose

I have gone through the documentation but running across issues as I'm launching the containers through docker-compose.
I'm using docker-compose because I have more than one containers that directly correlates to each csproj file within the location, also the docs refer to attributes using Dockerfile which itself adds another layer of complexity.
docker-compose.yml
version: '3'
services:
app1:
image: mcr.microsoft.com/dotnet/core/sdk:2.2
container_name: app1
restart: on-failure
working_dir: /service
command: bash -c "dotnet build && dotnet bin/Debug/netcoreapp2.2/App1.dll"
ports:
- 5001:5001
- 5000:5000
volumes:
- "./App1:/service"
app2:
image: mcr.microsoft.com/dotnet/core/sdk:2.2
container_name: app2
restart: on-failure
working_dir: /service
command: bash -c "dotnet build && dotnet bin/Debug/netcoreapp2.2/App2.dll"
ports:
- 5001:5001
- 5000:500
Project Structure
Microservice.sln
App1/App1.csproj
App2/App2.csproj
Issue
My IDE starts to complain as soon as I run the containers for all kinds of syntax errors, and a local build simply fails.
is there a way to be able to compile the app locally as well as in the docker?
Edit

Docker compose : error copying nginx.conf fron nginx image

I am using a docker-compose.yml in which I describe 2 services , one for frontend app and the second one is for backend :
version: '2'
services:
cdl-front:
build: cdl-web/.
ports:
- "80:80"
depends_on:
- cdl-rest
cdl-rest:
build: cdl-rest/.
ports:
- "7777:7777"
When I try to launch my docker-compose configuration using IntelliJ IDEA configured with Docker plugin I get this error :
ERROR: Service 'cdl-front' failed to build: COPY failed: stat
/var/lib/docker/tmp/docker-builder939775883/nginx.conf: no such file
or directory Failed to deploy 'Compose: docker-compose.yml':
docker-compose process finished with exit code 1
Below is my 2 dockerfiles describing the 2 services :
cdl-front contain this dockerfile :
FROM nginx
WORKDIR .
COPY nginx.conf /etc/nginx/nginx.conf
COPY cdl-frontend/cdl /usr/share/nginx/html
cdl-rest contain this dockerfile
# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine
# Add Maintainer Info
LABEL maintainer="ghassen1khalil#gmail.com"
# Add a volume pointing to /tmp
VOLUME /tmp
# Make port 8080 available to the world outside this container
EXPOSE 9091
# The application's jar file
ARG JAR_FILE=target/cdl-rest-1.0-SNAPSHOT.jar
# Add the application's jar to the container
ADD ${JAR_FILE} cdl-rest.jar
# Run the jar file
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom -Dfile.encoding=UTF-8","-jar","/cdl-rest.jar"]
CMD ["--env=prod"]

Resources