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.
Related
I am not sure how to debug container start process.
I created a templated Blazor wasm hosted app (.NET 6) in VS 2022 and added docker support. Docker file configuration is default - I did not change anything in it
I have pushed this image to docker hub.
I have cleared all the images and containers from cache, docker images -a and docker ps -a return no data.
I execute docker pull on both developmen computer (Win11) and ubuntu server.
I run the container on both Win11 and ubuntu: docker run -dit -p 5000:80 username/imagename
On Win11 container is running in the background, status: UP and I can access my app on localhost:5000
However, on ubuntu container runs and exists (status: Exited (0) About 3 secs ago).
docker logs containername returns no data
docker inspect returns in config section: "Architecture": "amd64", "Os": "linux", "Entrypoint": [ "dotnet", "App.Server.dll" ] - so that looks OK
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 ["TestApp/Server/TestApp.Server.csproj", "TestApp/Server/"]
COPY ["TestApp/Shared/TestApp.Shared.csproj", "TestApp/Shared/"]
COPY ["TestApp/Client/TestApp.Client.csproj", "TestApp/Client/"]
RUN dotnet restore "TestApp/Server/TestApp.Server.csproj"
COPY . .
WORKDIR "/src/TestApp/Server"
RUN dotnet build "TestApp.Server.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "TestApp.Server.csproj" -c Release -o
/app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestApp.Server.dll"]
Any ideas why I have different behavior on ubuntu server, and how can I debug this?
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"]
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 successfully run "docker run". How do I browse my web app?
I have tried localhost:8000 and localhost:81 but no success.
Updated with docker ps -a
My dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["WebApplication4/WebApplication4.csproj", "WebApplication4/"]
RUN dotnet restore "WebApplication4/WebApplication4.csproj"
COPY . .
WORKDIR "/src/WebApplication4"
RUN dotnet build "WebApplication4.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebApplication4.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication4.dll"]
You are able to access localhost:52010 since the container that exposed the port 52010 is currently running, however the other containers have all exited. This is probably due to the fact that container ea7 was run with the persistent tail -f /dev/null command, while the other containers were run with the non-persistent bash command.
The reason for this could be that the image you are trying to run does not have any command that persists and as soon as the container is created all running tasks in it are completed and the container exits.
Please make sure webapplication4:dev has a persistent command in it to keep the container from exiting.
Hope this helps !
Here is my Dockerfile:
FROM microsoft/aspnetcore-build:1.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 -c Release -o out
# build runtime image
FROM microsoft/aspnetcore:1.0
WORKDIR /app
COPY --from=build-env /app/out .
EXPOSE 58912
ENTRYPOINT ["dotnet", "SgaApi.dll"]
I used this command to build: docker build -f Dockerfile -t sga .
Run: docker run -e "ASPNETCORE_URLS=http://+:58912" -it --rm sga
Application starts successfully
I can't access it from the browser. When I run the application using "dotnet run" it works fine.
You have exposed the port, but you haven't published it. You can either use -P to publish all exposed ports or -p and then specify port mapping. For example:
docker run -p 58912:58912 -e "ASPNETCORE_URLS=http://+:58912" -it --rm sga