Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is my docker container ASP.NET core app not available after ending debugging in Visual Studio

My title explains most of it but want to understand why it is that I can access https://localhost:32770/ and get my API endpoints when I am debugging in Visual Studio but when I end debugging it becomes unavailable.

I'm currently in the thick of spending a few days wrapping my head around Docker and Kubernetes and this is stumping me a bit, and I'd really love to fill this gap in my knowledge.

The container remains running after being created so what has changed?

I noticed this is run at the start of the build:

docker exec -i 0f855d9b4c801bf8c52da48e6dd02ffdf0fe7242fde22fb9a221616e4b2900f9 /bin/sh \
-c "if PID=$(pidof dotnet); then kill $PID; fi"

but I don't see how that changes what happens after the debugging ends when this is before the dockerfile is run and everything. I don't understand the -c in the command, but I do understand that the script in the quotation marks after it is run in the container following docker exec syntax docker exec [OPTIONS] CONTAINER COMMAND [ARG...]. it seems this script kills the existing build of the code before the new one is created.

This is run before the dockerfile is run

docker build -f "F:\Dev\API_files\API_name\Dockerfile" 
--force-rm 
-t API_name:dev 
--target base  
--label "com.microsoft.created-by=visual-studio" 
--label "com.microsoft.visual-studio.project-name=API_name" "F:\Dev\API_name"

I don't see anything here that would change how the container is running, rm in this instance 'removes intermediate containers after a build (default true)' according to docker build --help

the dockerfile is run next and it is pretty much the default one for ASP.NET core Applications, it has

EXPOSE 80
EXPOSE 443

and the rest are simple build steps. After all this I can't seem to find much indication of what is going on. My guesses are that it has to do with IIS Express but really I don't know much of what goes on with it and when visual studios is debugging. Whats going on behind the scenes that was running while I was debugging to open the localhost port for the docker container?

Edit: I found a docker run command that may have something to do with it, but maybe not. The docker run command has the -P flag to 'Publish all exposed ports to random ports' but the container never stops running so should I not be able to find these ports and connect to the API?

like image 259
Adam M. Avatar asked Apr 27 '20 18:04

Adam M.


People also ask

How do I keep my docker container running for debugging?

Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.

Can ASP NET application be run in docker container?

Command to Deploy the ASP.NET Application in Docker Once you have built your application successfully, open a command window in administrator mode and execute the following command to create the Docker image: docker build -t dockerdemo . And, that is it!


1 Answers

During the debugging, if you run this command:

docker exec -it containerName bash -c 'pidof dotnet'

You will noticed, that the dotnet process is running, and when you stop the debugging and run it again, you are going to see that, the process was finalized.

If you want to start your application in the container, without run the debugger again, you just need to run the start the dotnet process inside the container.

You could do that, running a script like this:

#Set these 3 variables
$containerName = "MyContainer"
$appDll = "myApp.dll"
$appDirectory = "/app/bin/debug/netcoreapp3.1"

$args = "/c docker exec -it $containerName bash -c 'cd $appDirectory;dotnet $appDll'"
Start-Process -FilePath "powershell" -ArgumentList $args -NoNewWindow

You can check if it worked, by running this script again:

docker exec -it containerName bash -c 'pidof dotnet'
like image 128
Bruno Warmling Avatar answered Oct 20 '22 13:10

Bruno Warmling