I built a Docker image with a simple .Net Core console app that just display messages in a loop. After build, I can see that it is registered with my local Docker Desktop (see screenshot below). But when I run it, it seems running, but I don't see any console display the messages. What am I missing here?
When I click on the console icon on the right, it just displays an empty console as this
Here is my docker file:
FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["TPA-Solution-0.csproj", "."]
RUN dotnet restore "TPA-Solution-0.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "TPA-Solution-0.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TPA-Solution-0.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TPA-Solution-0.dll"]
enter code here
Here is my docker-compose.ymal:
version: '3.4'
services:
tpa-solution-0:
image: ${DOCKER_REGISTRY-}tpasolution0
build:
context: .
dockerfile: Dockerfile
The log doesn't have much in it. So is the console attached to it... I wonder if my program is actually running while the container is running.
double click on container name to see your console logs,
you can also attach to container , if needed using console icon
Part 2:
1.Console App:
while (true)
{
Thread.Sleep(500);
Console.WriteLine("test");
}
DockerFile
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["ConsoleApp2/ConsoleApp2.csproj", "ConsoleApp2/"]
RUN dotnet restore "ConsoleApp2/ConsoleApp2.csproj"
COPY . .
WORKDIR "/src/ConsoleApp2"
RUN dotnet build "ConsoleApp2.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ConsoleApp2.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ConsoleApp2.dll"]
3 Compose File
version: '3.4'
services:
consoleapp2:
image: ${DOCKER_REGISTRY-}consoleapp2
build:
context: .
dockerfile: ConsoleApp2/Dockerfile
Related
I am trying to create a docker image for a .net 6 web api application, and whenever I run the command:
~ sudo docker build -t aspnetapp .
It shows me the following error:
/src/AmplifyAPI.Application/Controllers/Product/ProductController.cs(22,38): error CS0111: Type 'ProductController' already defines a member called 'SaveProduct' with the same parameter types [/src/AmplifyAPI.Application/AmplifyAPI.Application.csproj]
And that error is getting showed for every method of every controller. This is my docker file:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore "AmplifyAPI.Application/AmplifyAPI.Application.csproj" --disable-parallel
WORKDIR "/src/AmplifyAPI.Application"
COPY . .
RUN dotnet build "AmplifyAPI.Application.csproj" -c Release -o /app
FROM build AS publish
WORKDIR "/src/AmplifyAPI.Application"
RUN dotnet publish "AmplifyAPI.Application.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "AmplifyAPI.Application.dll"]
And the application structure is:
--src
-----Dockerfile
-----AmplifyAPI.Data
-----AmplifyAPI.Domain
-----AmplifyAPI.Application
---------Controllers
--tests
-----AmplifyAPI.UnitTests
-----AmplifyAPI.IntegrationTests
Locally the application builds just fine. I am using Linux as operating system.
You copy the source code into the container twice, in different locations. You also both build and publish to /app, which might cause issues.
The normal Dockerfile for a .NET project first copies the .csproj file, then does a restore and then copies the rest of the code and builds, like this
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY AmplifyAPI.Application/AmplifyAPI.Application.csproj ./AmplifyAPI.Application/
COPY AmplifyAPI.Data/AmplifyAPI.Data.csproj ./AmplifyAPI.Data/
COPY AmplifyAPI.Domain/AmplifyAPI.Domain.csproj ./AmplifyAPI.Domain/
RUN dotnet restore "AmplifyAPI.Application/AmplifyAPI.Application.csproj" --disable-parallel
COPY . .
RUN dotnet build "AmplifyAPI.Application/AmplifyAPI.Application.csproj" -c Release
FROM build AS publish
WORKDIR "/src"
RUN dotnet publish "AmplifyAPI.Application/AmplifyAPI.Application.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "AmplifyAPI.Application.dll"]
I try to containerize my .net core application but i have an issue on app container ports. It seems as 0.0.0.0:7001->7001/tcp. I think problem exists because of this. Is there any other problems on there?
My dockerfile and docker-compose file like below
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-nanoserver-1903 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["Blog.Web/Blog.Web.csproj", "Blog.Web/"]
COPY ["Blog.BLL/Blog.BLL.csproj", "Blog.BLL/"]
COPY ["Blog.DAL/Blog.DAL.csproj", "Blog.DAL/"]
COPY ["Blog.Models/Blog.Models.csproj", "Blog.Models/"]
RUN dotnet restore "Blog.Web/Blog.Web.csproj"
COPY . .
WORKDIR "/src/Blog.Web"
RUN dotnet build "Blog.Web.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Blog.Web.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Blog.Web.dll"]
Docker-compose.yaml
version: '3.4'
services:
client:
build:
dockerfile: Blog.Web\Dockerfile
context: .
ports:
- "7001:7001"
Bash Display
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b5e3edcbc969 blogwebapplication_client "dotnet Blog.Web.dll" About a minute ago Up About a minute 0.0.0.0:7001->7001/tcp blogwebapplication_client_1
When I try to access my app on localhost:7001 I get an error:
"This site cant be reach".
I'm new to playing with docker and run into what is properly a stupid issue.
I installed latest docker desktop on a Mac which is installed and running.
I've tried the multi container app on the Microsoft app
https://learn.microsoft.com/en-us/visualstudio/mac/docker-multi-container?view=vsmac-2019
But keep getting 'Connection Refused' errors when trying to run it
Reading other answers, ive dismissed issue with my docker-compose.yml
version: '3.4'
services:
dockerdemofrontend:
image: ${DOCKER_REGISTRY-}dockerdemofrontend
build:
context: .
dockerfile: DockerDemoFrontEnd/Dockerfile
dockerdemoapi:
image: ${DOCKER_REGISTRY-}dockerdemoapi
build:
context: .
dockerfile: DockerDemoAPI/Dockerfile
My DockerDemoAPI Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY DockerDemoAPI/DockerDemoAPI.csproj DockerDemoAPI/
RUN dotnet restore "DockerDemoAPI/DockerDemoAPI.csproj"
COPY . .
WORKDIR "/src/DockerDemoAPI"
RUN dotnet build "DockerDemoAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerDemoAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerDemoAPI.dll"]
My DockerDemoFrontEnd DockerFile
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY DockerDemoFrontEnd/DockerDemoFrontEnd.csproj DockerDemoFrontEnd/
RUN dotnet restore "DockerDemoFrontEnd/DockerDemoFrontEnd.csproj"
COPY . .
WORKDIR "/src/DockerDemoFrontEnd"
RUN dotnet build "DockerDemoFrontEnd.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerDemoFrontEnd.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerDemoFrontEnd.dll"]
In my front end project
if I remove my http call and run the project
var request = new System.Net.Http.HttpRequestMessage();
request.RequestUri = new Uri("http://DockerDemoApi/api/values/1");
var response = await client.SendAsync(request);
ViewData["Message"] += " and " + await response.Content.ReadAsStringAsync();
It runs fine, loads in safari and I can view the page.
If I put the HTTP back in I get the
HttpRequestException: Connection refused
Im lost how to resolve this issue
Thanks
I'm trying to move ASP.NET Core project to docker linux container, however I cannot build up a proper image. When I create default dockerfile (through VisualStudio Add -> DockerSupport) it's setting up paths for COPY to all of the projects. However when I run docker build -t . it pop's out error "no such file or directory". I've tried to stick with official docker documentation with moving asp.net core to docker containers and here is the code I ended with:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ./ ./
RUN dotnet restore "myproject.csproj"
COPY . .
WORKDIR "/src/myproject"
RUN dotnet build "myproject.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "myproject.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myproject.dll"]
This is the command im using to build up an image:
docker build -t myapp -f myproject/Dockerfile .
I'm building it from the parent directory, because when I was using current working directory it wasn't finding all the .csproj files. When I run this image it doesn't work - I know there are wrong paths, however I have no idea where is the error in logic. Current error is "Program does not contain a static 'Main' method suitable for an entry point" and "A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/'. Failed to run as a self-contained app. If this should be a framework-dependent app, specify the appropriate framework in /app/myproject.runtimeconfig.json"
Try the following based off this documentation replace myproject with project name
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", “myproject.dll"]
Then build and run with the following commands:
docker build -t myproject .
docker run -d -p 8080:80 --name myapp myproject
You should not specify RUN dotnet build "myproject.csproj" -c Release -o /app with -o /app which will output in the same folder with build layer.
Try
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["myproject/*.csproj", "myproject/"]
RUN dotnet restore "myproject/myproject.csproj"
COPY . .
WORKDIR /src/myproject
RUN dotnet build "myproject.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "myproject.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myproject.dll"]
I am currently developing a NET Application and I have to run bash scripts that:
1.Build the project
2.dotnet publish -c Release the project
3.copy the publish folder to a path for Dockerfile to take on.
Can this be done directly from the dockerfile?
What I am running now:
Bash-script
dotnet build ../${serverSrcFolder} &&
dotnet publish -c Release ../${serverSrcFolder} --output $(pwd)/${serverPublishDestFolder}
Dockerfile
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
COPY . /app
ENTRYPOINT [ "dotnet" , "./publish/Server.dll" ]
EXPOSE 9400
EXPOSE 6379
The publish folder stays near the dockerfile.
ParentFolder
Dockerfile
publish
The serverPublishDestFolder from the bash script above is this publish folder.
Can I somehow always map the publish folder whenever it changes from within the dockerfile?
You can do three steps above inside the Dockerfile
e.g.
ROM microsoft/dotnet:2.1.5-aspnetcore-runtime AS base
WORKDIR /app
ENV ASPNETCORE_URLS http://+:5000
EXPOSE 5000
FROM microsoft/dotnet:2.1.403-sdk AS build
WORKDIR /src
COPY src/WebApi/WebApi.csproj src/WebApi/
RUN dotnet restore src/WebApi/WebApi.csproj
COPY . .
WORKDIR /src/src/WebApi
RUN dotnet build WebApi.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish WebApi.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApi.dll"]
This Dockerfile can be easily generated by Visual Studio 2017