I've created an image on my local with my IdentityServer project.
Simply , I did build my dockerfile using
docker build -t identityserver .
DOCKERFILE
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
COPY /IdentityServer/*.csproj ./
RUN dotnet restore
COPY /IdentityServer ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "IdentityServer.dll"]
Then after the build, I did run the image in the container
docker run -d -p 5443:5443 <image_id>
THE PROBLEM IS:
I'm trying to connect with the container using https://localhost:5443 but it doesn't work. The site cannot be reach. It is not existing. I've tried to look on the logs (see below) except that I think it runs in a production environment. I pretty sure it is one to fix because I'm just running in my local. But why can't I even find the page in the browser?
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app/
because your application is running on 80 port inside container.
Now listening on: http://[::]:80
you should map your local 5443 port to container 80 port.
docker run -d -p 5443:80 <image_id>
Related
I am trying to run a dotnet core Web Api application on a docker container on Windows 10
This is the project structure on the left side of the image. The Dockerfile is one level up the .csproj file, so its something like this.
/WebApi {Root folder}
- WebApi {folder container the WEbApi.csproj and other files folders}
- Dockerfile
- .dockerignore
- WebApi.sln
This the Dockerfile contents
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# Copy csproj and restore as distinct layers
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
COPY WebApi/*.* src/
RUN dotnet restore src/WebApi.csproj
RUN dotnet build "src/WebApi.csproj" -c Release -o /app/build
# publish the project
FROM build AS publish
RUN dotnet publish src/WebApi.csproj -c Release -o /app/out
# Build runtime image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/out .
ENTRYPOINT ["dotnet", "WebApi.dll"]
I build the image using this docker build -t webapi-img . in the same folder that has the Dockerfile.
This is the build log
Then I run the container using docker run -p 8090:8071 --name webpi-cntr webapi-img which gives the following output
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:8071
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app
The container runs but when I try to access the service on the host machine using http://localhost:8090/WeatherForecast, I get a 404 error.
When I access the container CLI and run the curl command using the port of the application I get no response as you can see in the screenshot below
Back on the host when I telnet localhost 8090, the telnet client connects to the port.
It looks like the container is running correctly and the mapping has also happened correctly. My assumption is that the core dotnet app is not running correctly, but I don't know what could be causing that or any ways to probe this further. Any pointers?
PS: I am using Dockerfile because I want to take this project later to an AWS ECS container via CDK and using a pipeline. Not sure if I can use docker compose there, but thats for later.
You should add EXPOSE 8071 to your Dockerfile so that your container exposes that port.
The problem was in this line
COPY WebApi/*.* src/
This was only copying the files in the root folder and not the folder which had the controllers. This is why on compiling, it was not able to find the path, /WeatherForecast but was standing up the server anyways.
The correction to the line that got everything working was ...
COPY WebApi/** src/
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.
It is impossible for me to access container with ASP.NET Core 3.1 application running inside.
Goal is to run application in container on port 5000. When I'm running it locally using standard VS profile I navigate to http://localhost:5000/swagger/index.html in order to load swaggerUI. I would like to achieve same thing using docker.
Steps to reproduce my issue:
Add dockerfile with exposed 5000 port and ENV ASPNETCORE_URLS variable:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:5000
EXPOSE 5000
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["myapp/myapp.csproj", "myapp/"]
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"]
Build image
docker build -t myapp .
Run docker image:
docker run myapp -p 5000:5000
Running commands above with specific docker file results in this:
[21:28:42 INF] Starting host.
[21:28:42 INF] Now listening on: http://[::]:5000
[21:28:42 INF] Application started. Press Ctrl+C to shut down.
[21:28:42 INF] Hosting environment: Production
[21:28:42 INF] Content root path: /app
However, I can't access container using http://localhost:5000/swagger/index.html because of ERR_CONNECTION_REFUSED -> This site can't be reached.
I did get into container to check if host is running for sure, using:
docker exec -it containerId /bin/bash
cd /app
dotnet myapp.dll
what resulted in following error:
Unable to start Kestrel.
System.IO.IOException: Failed to bind to address http://[::]:5000: address already in use.
Conclusion is that port inside the container is used, application is alive, it's just not accessible from outside.I don't know how to get inside of it.
Please point me into right direction.
UPDATE
Issue is solved, answer is posted below. However explanation why it was needed and how it works would be nice!
To solve the issue I had to manually add "--server.urls" to entrypoint like shown below:
ENTRYPOINT ["dotnet", "myapp.dll", "--server.urls", "https://+:5000"]
I solved the same issue in the following way:
Added the following in appsettings.json to force Kestrel to listen to port 80.
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://+:80"
}
}
}
Exposed the port in dockerfile
ENV ASPNETCORE_URLS=http://+:80
EXPOSE 80
ENTRYPOINT ["dotnet", "EntryPoint.dll"]
Ran the container using the below command.
docker run -p 8080:80 <image-name>:<tag>
The app exposed on http://localhost:8080/
I have my Dockerfile:
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /build
COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o output
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build /build/output .
ENTRYPOINT ["dotnet","TestDockerApi.dll"]
I am creating an image using :
docker build -t testdocker/api .
and then running a container from image using :
docker run testdocker/api
I can see following message on my console:
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
I am trying to access using http://localhost/app/TestDockerApi/Values , but it does not work.
Do I need to use docker image IP to access that .
I can see few tutorials suggesting to do this in Entrypoint :
ENTRYPOINT ["dotnet","TestDockerApi.dll","--server.urls","http://0.0.0.0:5000"]
And then while running the container, mapping the port:
docker run -p 80:5000 testdocker/api
Is there any way I could access the API with out using portforwarding? I am just trying to get the basics right , why and what should I do.
The Dockerfile does not manage network configuration outside of the container at all. If you want docker to listen on your host port of 80, you need to bind it when you run your container.
docker run -80:80 testdocker/api
For more description about mapping and exposing ports, you can read here:
- https://www.ctl.io/developers/blog/post/docker-networking-rules/
Alternatively you can create your own service composition where you specify these details and specify this in a docker-compose.yml file
api:
image: testdocker/api
ports:
- "80:80"
And then you can simply run with
docker-compose up
More information is at:
https://docs.docker.com/compose/reference/overview/#command-options-overview-and-help
I have a default asp.net core dockerfile (as created by VS Tools for Docker):
FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "myapp.dll"]
When i run my image using docker run myimage i get this message in an interactive console:
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
If i then press Ctrl+C and type docker start <imgid>, then i no longer see this message, and my bash console is not blocked.
How can i do docker run bypassing this annoying message?
You can use -d flag with docker run so that the image starts in detached mode. In this case you will not see any output but docker will be running in background.
docker run -d