We have hosted .net core api in linux server using docker. so issue is reading application logs from Logs folder which we are not getting in container. our docker code is :
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 50147
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["Services/Services.csproj", "Services/"]
COPY ["Portal.Common/Portal.Common.csproj", "Portal.Common/"]
COPY ["Portal.Domains/Portal.Domains.csproj", "Portal.Domains/"]
COPY ["Portal.Business/Portal.Business.csproj", "Portal.Business/"]
COPY ["Portal.DataAccess/Portal.DataAccess.csproj", "Portal.DataAccess/"]
RUN dotnet restore "PortalServices/PortalServices.csproj"
COPY . .
WORKDIR "/src/PortalServices"
RUN dotnet build "PortalServices.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "PortalServices.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "PortalServices.dll"]
so inside app path only dll files are there. So how can we read logs from folder???
We implemented serilog to solution and saving to solutionpath/Logs/log.txt
In local it is storing in this path.
Please help!!
Change folder permission to 777 where you want to store logs.
chmod 777 .
docker run -d -t -i -p 80:80 -v /usr/Logs:/app/Logs --name
testContainer testImage
Related
I have an empty .NET 6 web api solution with swagger installed. I have generated a docker file from Visual Studio which looks like this:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]
I have created a docker image and pushed to my docker hub account.
I have then run this command to pull and run the image locally:
docker container run -d --name mynewapi -p 8000:443 myusername/myreponame:mycontainername
However when I browse to http://localhost:433/swagger/index.html I get:
This site can’t be reached localhost refused to connect.
I have also tried to make a GET request using postman to the sample endpoint but i just get the same error.
What am i doing wrong here?
When you run the container, you map port 443 in the container to port 8000 on the host. So you need to access it using port 8000.
Since you map port 443 which is the https port, you should be using https. So your URL should be https://localhost:8000/swagger/index.html
But that still isn't enough. By default Swagger is only available when your solution is running in Development mode. You control that using the environment variable ASPNETCORE_ENVIRONMENT which needs to be set to 'Development'. You can do that in your docker command when you run the container like this
docker container run -d --name mynewapi -p 8000:443 -e ASPNETCORE_ENVIRONMENT=Development myusername/myreponame:mycontainername
or you can add it to the Dockerfile like this
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Development
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Api.Permissions/Api.Permissions.csproj", "Api.Permissions/"]
COPY ["Api.Permissions.Models/Api.Permissions.Models.csproj", "Api.Permissions.Models/"]
COPY ["Api.Permissions.Services/Api.Permissions.Services.csproj", "Api.Permissions.Services/"]
RUN dotnet restore "Api.Permissions/Api.Permissions.csproj"
COPY . .
WORKDIR "/src/Api.Permissions"
RUN dotnet build "Api.Permissions.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Api.Permissions.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Api.Permissions.dll"]
I'm working on a Blazor Wasm (ASP.Net Core hosted consisting on the usual 3 projects Client, Server and Shared) app that I want to deploy to Linux using docker.
I'm copying a nginx.conf file to the Server project root folder but when trying to publish to Azure App Service Containers I'm getting:
failed to compute cache key: "/nginx.conf" not found: not found
This is my Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["MyLocalShop/Server/MyLocalShop.Server.csproj", "MyLocalShop/Server/"]
COPY ["MyLocalShop.Services.MongoDb/MyLocalShop.Services.MongoDb.csproj", "MyLocalShop.Services.MongoDb/"]
COPY ["MyLocalShop.Server.Contracts/MyLocalShop.Server.Contracts.csproj", "MyLocalShop.Server.Contracts/"]
COPY ["MyLocalShop/Shared/MyLocalShop.Shared.csproj", "MyLocalShop/Shared/"]
COPY ["MyLocalShop/Client/MyLocalShop.Client.csproj", "MyLocalShop/Client/"]
RUN dotnet restore "MyLocalShop/Server/MyLocalShop.Server.csproj"
COPY . .
WORKDIR "/src/MyLocalShop/Server"
RUN dotnet build "MyLocalShop.Server.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyLocalShop.Server.csproj" -c Release -o /app/publish
FROM nginx:alpine AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY nginx.conf /etc/nginx/nginx.conf
ENTRYPOINT ["dotnet", "MyLocalShop.Server.dll"]
If I run the dotnet publish command to check the ouput directory within app/publish, I can see the file is actually there.
What am I missing?
This might happen when the file you want to COPY is also in the .dockerignore
Are you setting the context of the docker build to the directory that contains the nginx.conf and not a directory below that?
e.g.
nginx.conf
|-MyLocalShop
Bad: docker build -t something:latest -f ./DockerFile ./MyLocalShop
Good: docker build -t something:latest -f ./DockerFile .
Maybe you lack of nginx.conf file in your project.
When I run docker with Asp.net Core locally everything is ok. But when I use the Ubuntu virtual machine in cloud.google, I kind of start the container and everything works. But I don't know how to open the site. Login via External ip doesn't open anything. I just have "Can't access the site" Although everything opens locally. Am I missing any settings?
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]
Command: sudo docker run -d -p 8000:80 gamehomm/webapplication1
I got it!
docker run -d -p 80:80/tcp gamehomm/webapplication1
Then I changed VM instance details
Firewalls
Allow HTTP traffic
Allow HTTPS traffic
I have created the Dockerfile using visual studio for my dotnet api project but how can we integrate the client app in the docker file. I have created the docker file which looks like:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["Locomote-e-Client.Service/Locomote-e-Client.Service.csproj", "Locomote-e-Client.Service/"]
COPY ["Locomote-e-Client.Core/Locomote-e-Client.Core.csproj", "Locomote-e-Client.Core/"]
COPY ["Locomote-e-Client.Common/Locomote-e-Client.Common.csproj", "Locomote-e-Client.Common/"]
COPY ["Locomote-e-Client.Data/Locomote-e-Client.Data.csproj", "Locomote-e-Client.Data/"]
RUN dotnet restore "Locomote-e-Client.Service/Locomote-e-Client.Service.csproj"
COPY . .
WORKDIR "/src/Locomote-e-Client.Service"
RUN dotnet build "Locomote-e-Client.Service.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Locomote-e-Client.Service.csproj" -c Release -o /app/publish
FROM node:10.21 as nodebuilder
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
COPY Locomote-e-ClientApp/package.json /usr/src/app/package.json
RUN npm install
COPY Locomote-e-ClientApp/. /usr/src/app
RUN npm run build
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
RUN mkdir -p /app/ClientApp/dist
COPY --from=nodebuilder /usr/src/app/dist/. /app/ClientApp/dist/
ENTRYPOINT ["dotnet", "Locomote-e-Client.Service.dll"]
Whenever I have created the image by using this docker file then it will create the image successfully. But when I tried to run that image using container then it is not running in container.
Command to build the image
docker build -t ashishrajput194/locomate:v1 .
Command to run as container
docker run -p 80:80 -d ashishrajput194/locomate:v1
It is not showing any error but the container exits automatically. I am not able to run the application.
I need to access test result files in the host from the container. I know that I need to create a volume which maps between host and container, like below, but I get nothing written to the host.
docker run --rm -it -v <host_directory_path>:<container_path> imagename
Dockerfile:
FROM microsoft/dotnet:2.1-sdk AS builder
WORKDIR /app
COPY ./src/MyApplication.Program/MyApplication.Program.csproj ./src/MyApplication.Program/MyApplication.Program.csproj
COPY nuget.config ./
WORKDIR ./src/MyApplication.Program/
RUN dotnet restore
WORKDIR /app
COPY ./src ./src
WORKDIR ./src/MyApplication.Program/
RUN dotnet build MyApplication.Program.csproj -c Release
FROM builder as tester
WORKDIR /app
COPY ./test/MyApplication.UnitTests/MyApplication.UnitTests.csproj ./test/MyApplication.UnitTests/MyApplication.UnitTests.csproj
WORKDIR ./test/MyApplication.UnitTests/
RUN dotnet restore
WORKDIR /app
COPY ./test ./test
WORKDIR ./test/MyApplication.UnitTests/
RUN dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
ENTRYPOINT ["dotnet", "reportgenerator", "-reports:coverage.cobertura.xml", "-targetdir:codecoveragereports", "-reportTypes:htmlInline"]
The command at the entry point is working correctly. It is writing the output to the MyApplication.UnitTests/codecoveragereports directory, but not to the host directory.
My docker run looks as follows:
docker run --rm -it -v /codecoveragereports:/app/test/MyApplication.UnitTests/codecoveragereports routethink.tests:latest
What could I be doing wrong?
Looks like a permission issue.
-v /codecoveragereports:/app/***/codecoveragereports is mounting a directory under the root / which is dangerous and you may not have the permission.
It's better to mount locally, like -v $PWD/codecoveragereports:/app/***/codecoveragereports, where $PWD is an environment variable equal to the current working directory.