I'm trying to dockerize my .net core rest api app.
I refer to MS Menual which give docker sample and commands guidance and It worked well.
So, to try [dockerizing] myself, I created .Net Core Rest API app and checked 'Use Docker Support'.
I built image with 'Dockerfile' had been given by MS.
Below is Dockerfile
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["dockersupport.csproj", ""]
RUN dotnet restore "./dockersupport.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "dockersupport.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "dockersupport.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "dockersupport.dll"]
And I build it with
>$ docker build -t docksupport .
And run with
>$ docker run --rm -it -p 5000:80 --name dockersupport dockersupport
And server was running well. However I can't access to API swagger page.
When I tried to build/run with visual studio only, It showed swagger page well.
I tried to access to web page with url : 'localhost:5000/swagger/index.html' docker and local both.
I can't understand why MS sample app works well but my app doesn't. I wonder what is the differece between their and mine.
Would you share your knowledge or how can I access web page of my app?
In my code, swagger UI would be shown if environment is Development. However, I just set my docker environment Production.
I refer to this post Swagger UI gives 404 when app is running in Docker container
and I deleted if condition, It works well
Related
Can anyone please help me to create a docker file for blazor webassembly ?
I am trying to creating docker file for blazor webassembly.
I have checked many docs and I have tried with many docker files. Every time my docker image build successful but when I run that image at that time I am getting error like below
"It was not possible to find any installed .NET Core SDKs
Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from: https://aka.ms/dotnet-download"
I am using below code
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 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 mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "BlazorDemo.dll"]
This is my build command and it's working fine
docker build --tag blazordemo .
Below run command I have tried and here I am getting error
docker run --publish 8001:80 --detach --name blazortestapp blazordemo
docker run -p 8080:80 blazordemo
Note - Blazor WebAssembly target framework is .NET Standard 2.1
I have created a .net core 3.1 grpc project using Visual Studio(VS) template and added docker support (targe:Linux os) to it.
With the sayhello code already in the template, I ran it with VS IDE and it worked fine and given the http default page.
However when we run it with docker build and docker run commands in console, the console shows its listening and the container is running. however service is not accessible and the error received is site cant be reached.
I have grabbed and issued the build and run commands from the VS container tools output window, still the service is not accessible nor the default page is shown. Has anyone came across the same issue, any help would be much appreciated.
Docker file
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 "GrpcService2.csproj" .
RUN dotnet restore "GrpcService2.csproj"
COPY . .
RUN ls -R
WORKDIR "/src"
RUN dotnet build "GrpcService2.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "GrpcService2.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "GrpcService2.dll"]
Maybe you have to look up the port that's exposed by docker. See the docker dashboard. I haven't found out how to configure a fix port. At the moment at each start of the service a new port is taken by docker...
i created 2 default projects ( WEB API ) from visual studio , one with .net core 2.1 and the other with .net core 3.1! generated dockerfiles for both using visual studio !
dockerfile for both is like bellow
#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/core/aspnet:2.1 AS base // version 3.1 for my other project
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.1 AS build // version 3.1 for my other project
WORKDIR /src
COPY ["WebApplication2.csproj", ""]
RUN dotnet restore "./WebApplication2.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApplication2.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication2.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication2.dll"]
and this is my launchSetting
"applicationUrl": "http://localhost:80"
these are the exact command i use to build and run my dockerfile
docker build -t mohammadjavani/app21 .
docker run -p 8080:80 mohammadjavani/app21
docker build -t mohammadjavani/app31 .
docker run -p 8080:80 mohammadjavani/app31
after i run my app21 i could view my app in my brower in localhost:80
but as mohammadjavani/app31 after i stopped app21 container and run app31 i couldnt view my app in brower !
dockerfiles are generated in visual studio and commands i used are the exact same for both project
what could be wrong with my .net core 3.1 dockerfile project ??
Can you please try using the environment variable ASPNETCORE_URLS="https://+:443;http://+:80"
docker run -p 8080:80 mohammadjavani/app31 -e ASPNETCORE_URLS="https://+:443;http://+:80"
I'm fresh to Docker, trying to get a proof of concept container running on my machine. I generated a .NET Core 3.0 Web API project using the command dotnet new webapi. I've added a controller endpoint that simply returns a string "Hello World."
using System;
using Microsoft.AspNetCore.Mvc;
namespace DockerHelloWorld.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
[HttpGet]
public string Get()
{
return "Hello World!";
}
}
}
I am able to successfully build and run this project using dotnet build and dotnet run respectively.
I am now trying to get this to run in a Docker container. Here is my Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.0 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 --framework netcoreapp3 --configuration Release --output out
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DockerHellWorld.dll"]
This is copied and slightly modified from here.
I also have the following .dockerignore, just in case it is relevant:
bin\
obj\
*.md
*.png
I run the following commands:
docker build -t dockerhelloworldimage .
docker create -p 3000:80 --name dockerhelloworldcontainer dockerhelloworldimage
docker start dockerhelloworldcontainer
It seems like each step is being ran based on the output of docker build. A hash is returned after docker create and "dockerhelloworldcontainer" is returned after docker run.
The container stops immediately. I see this by using the docker container ls -a command.
docker logs dockerhelloworldcontainer has the following output: "It was not possible to find any installed .NET Core SDKs. Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from: https://aka.ms/dotnet-download" but I don't really know how to make sense of that.
It's worth mentioning, I got something very similar to this working with a .NET Core 2.2 app.
Any ideas? What could I be missing?
The issue as #Zied mentioned in the comment, is a typo in the Assembly name you want to call from the entrypoint command.
If you fix that on the docker file:
# All other content remains the same
ENTRYPOINT ["dotnet", "DockerHelloWorld.dll"]
You'll get your container successfully started.
Listing running containers: docker ps shows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7dcb5c122445 dockerhelloworldimage "dotnet DockerHelloW…" 5 minutes ago Up 4 minutes 0.0.0.0:3000->80/tcp dockerhelloworldcontainer
Now, this can look like a misleading error message, but if you try to actually run a wrong dotnet executable from the CLI (not from docker):
> dotnet MyNonExistingExeName.dll
You'll get the below message:
Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET Core program, but dotnet-.\bin\Debug\netcoreapp3.0\DockerHellWorld.dll does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
I believe that the second and third bullets apply to this case. What that means is that if you have a dotnet executable (or a global/local tool) you can run them with the dotnet command so the issue is that your command is being interpreted as a non-existent tool or command and hence the error.
Hope this helps!
Run the below command to deploy sample Dotnet web API code on docker
docker-compose build
docker-compose up
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
COPY ./SampleWebApi3.0.csproj /SampleWebApi3.0/
RUN dotnet restore ./SampleWebApi3.0/SampleWebApi3.0.csproj
COPY . ./SampleWebApi3.0/
WORKDIR /SampleWebApi3.0/
RUN dotnet build "SampleWebApi3.0.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "SampleWebApi3.0.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SampleWebApi3.0.dll"]
docker-compose.yml
version: '3.5'
services:
sample-web-api:
build: SampleWebApi3.0/
restart: always
ports:
- "8085:80"
Sample web API code 3.0 along with Dockerfile on Github
I want to run multiple instances of .net core API on windows server 2016 using windows docker container. I am able to create image and container successfully, but on invoking docker start the container are not running Up instead it exited with code (2147516566).
Here is my docker file content which is in published API directory
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-nanoserver-sac2016
COPY / app/
ENTRYPOINT ["dotnet", "app/MyAPI.dll"]
I didn't spend long on it, but I didn't have good luck running binaries I built myself. The docker add in for visual studio always performs the build inside a container. I have adapted to this. Here is an example Dockerfile I have anonymized. Hopefully I didn't break anything:
# Base image for running final product
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-nanoserver-sac2016 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# build asp.net application
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-nanoserver-sac2016 AS build
WORKDIR /src
COPY ["Test.Docker.Windows/Test.Docker.Windows.csproj", "Test.Docker.Windows/"]
RUN dotnet restore "Test.Docker.Windows/Test.Docker.Windows.csproj"
COPY . .
WORKDIR "/src/Test.Docker.Windows"
RUN dotnet build "Test.Docker.Windows.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Test.Docker.Windows.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
# startup.bat contains dotnet test.Docker.Windows.dll
CMD ./startup.bat