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
Related
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>
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/
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 faced some teething issue when trying to deploy my Docker image which contains a simple streamlit app to Heroku. My issue is that I am unable to access my Docker after deployment. On closer look, I discovered the following error:
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
I had researched and understood that this is because the port is unavailable, since Heroku will dynamically assign port number.
I had made sure that this will not happen by putting the following my Dockerfile.
Dockerfile:
FROM python:3.7
COPY . /app
WORKDIR /app
RUN pip install streamlit
ENTRYPOINT ["streamlit","run", "--server.enableCORS", "false" ,"--server.port", "$PORT"]
CMD ["app.py"]
I am now able to see that the Network URL and External URL port number are assigned by Heroku as it is not the typical 5901 number.
What puzzled me, however, is why is the container unable to bind to the given dynamic port number? I thought the app would be using the given dynamic number?
The problem is that $PORT does not get replaced with the corresponding environment variable when the Docker run is executed on the Heroku Docker Registry.
An alternative is to create a Docker file which invokes .sh script
FROM python:3.7
COPY . /app
WORKDIR /app
RUN pip install streamlit
ENTRYPOINT "/startup.sh"
and the startup.sh
echo PORT $PORT
streamlit run --server.enableCORS false --server.port $PORT app.py
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