Docker run command like in Visual Studio - docker

When running a Standard ASP.NET Core Web API project created by Visual Studio 2022 with docker option enabled, connecting to the server via browser is possible by using https://localhost:65215/swagger/index.html (which even pops up by itself).
When running the container with specifying the mapping of port 80 and 443 (such as 65214 and 65215), reaching Swagger is not possible on the specified ports. So there must clearly be more to it.
How can one run the docker container from the console, using a docker run command? And how can the port be set or at least identified?
Dockerfile
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 ["WebApiContainerized/WebApiContainerized.csproj", "WebApiContainerized/"]
RUN dotnet restore "WebApiContainerized/WebApiContainerized.csproj"
COPY . .
WORKDIR "/src/WebApiContainerized"
RUN dotnet build "WebApiContainerized.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApiContainerized.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApiContainerized.dll"]
Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) {
app.UseSwagger();
app.UseSwaggerUI(); }
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Project creation

Assuming your code contains something like this...
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
}
... Swagger will only be available in development environment mode that is controlled by the ASPNETCORE_ENVIRONMENT environment variable.
The easiest is to set this to Development as part of your run command:
docker run -p 65215:80 -e ASPNETCORE_ENVIRONMENT=Development yourimage:yourtag

By default, Swagger is only available when you run your app in the 'development' environment.
By default, a containerized app runs in production.
To change it, you set the ASPNETCORE_ENVIRONMENT variable to 'Development'. You can either do that on the docker run or you can set it in the Dockerfile.
As for the ports, Microsoft set ASPNETCORE_URLS to http://+:80 in the aspnet images, causing the app to listen on port 80.
Add a line to set ASPNETCORE_ENVIRONMENT to your 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 ["WebApiContainerized/WebApiContainerized.csproj", "WebApiContainerized/"]
RUN dotnet restore "WebApiContainerized/WebApiContainerized.csproj"
COPY . .
WORKDIR "/src/WebApiContainerized"
RUN dotnet build "WebApiContainerized.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApiContainerized.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApiContainerized.dll"]
Build and run with
docker build -t myimage .
docker run -d -p 65214:80 myimage
You should now be able to reach the Swagger page at http://localhost:65214/swagger

Related

Dev certificate on aspnetcore docker container

I'm new to Docker and I have run into a trouble running my app in docker due missing ssl certificates. How do I run aspnetcore application in docker with non-development environment using dev certificates?
I have the following Dockerfile generated by Visual Studio:
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 ["MyApp/MyApp.csproj", "MyApp/"]
COPY [...other libraries...]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]
When I run it from the Visual Studio with the default setup (development), it connects Docker and everything runs fine. However, I want to use a different appsettings file (appsettings.docker.json) because I need some different values when running in Docker and I'm already using my appsettings.development.json for my standard run from the VS. Thus I set ASPNETCORE_ENVIRONMENT=Docker. This is causing a pain as I'm suddenly getting an InvalidOperationException:
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
Based on this answer, I have figured out that dotnet generates the dev certs automagically when the environment variable is Development. How can that be fixed for a differently called environment?
I did try to use the dotnet dev-certs https command in the Dockerfile, however, build failed saying that there is no sdk in the image and thus doesn't know the command.
Try adding these 2 lines
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
# Add the line below ---------------------------------------------------
RUN dotnet dev-certs https
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
COPY [...other libraries...]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
# Add the line below ---------------------------------------------------
COPY --from=publish /root/.dotnet/corefx/cryptography/x509stores/my/* /root/.dotnet/corefx/cryptography/x509stores/my/
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]

.Net 6 Web Api docker port forwarding issue

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"]

Docker run does not binding host port

I'm trying to run simplest asp.net core 3.1 WebApplication inside docker as simple as possible but, docker does not binding host port.
Host: Windows 10
Container target: Linux
Docker version: 20.10.7
Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
EXPOSE 80
EXPOSE 443
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY . .
RUN dotnet restore "WebApplication.csproj"
RUN dotnet build "WebApplication.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication.dll"]
Docker build command (ok)
docker build -t webapplication .
Docker run command (ok)
docker run webapplication -p 5000:80 -p 5001:443
When I try to access website on my localhost (http://localhost:5000) I'm getting This site can’t be reached. Running the docker ps returned ports information without host mapping port
As #marzelin correctly pointed out, you need to add the flag right after run.

How do I open a site using ASP.net Core in Docker on Ubuntu on Google.Cloud?

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

Net Core on Docker for Windows SQL Server not working

I'm creating a new empty ASP.NET Core 2.2 with Docker support enabled (Docker for Windows).
When I run it on Docker, it does not connect to SQL Server. I was expecting that the nanoserver had a SQL Server in it. Am I wrong?
How should I connect this instance to an SQL Server?
I was trying to avoid creating a second Docker instance for the SQL Server. Is this the only way to achieve what I'm looking for?
Here's the auto generated Dockerfile:
FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1803 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk-nanoserver-1803 AS build
WORKDIR /src
COPY ["DockerTest2/DockerTest2.csproj", "DockerTest2/"]
RUN dotnet restore "DockerTest2/DockerTest2.csproj"
COPY . .
WORKDIR "/src/DockerTest2"
RUN dotnet build "DockerTest2.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "DockerTest2.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DockerTest2.dll"]

Resources