简体   繁体   中英

.NET Core 3.1 Web API with Docker Desktop. Access container without running project in VS.NET

I've created a .net core web api project and am successfully running it under Docker Desktop for Window. It works fine as long as I'm running the project in Visual Studio. I run the "Docker" profile and hit it with localhost:8000/. I've configured it to use port 8000...

But when I stop the visual studio debugger, I can no longer access it even though when it still shows up as "RUNNING" in Docker Desktop. How can I access the service in the container, without running the project in Visual Studio?

I'm a newbie in this area, but if an image has been built and that image is running in a container, I'd think I'd be able to access it without needing to run the visual studio project. Isn't the container a "compiled, built, stood-up instance of my image, running in a docker host"???

Thanks, Brent

"Isn't the container a "compiled, built, stood-up instance of my image, running in a docker host"???" Yes, it is, you're 100% right, but in the Visual Studio, they made some performance adjustments so everything can run so smoothly, without any overhead of stopping and removing the container, updating the image and running it etc. In fact, under the hood, your binaries and source code are mapped via volume, so there's no need to juggle building new images etc. every time you run the app. If you run docker inspect on the container created by the Visual Studio, you'll be able to see that that mapping in the "Mounts" section of the JSON output.

I created a brand new ASP.NET Core web API app from the template with Docker enabled. I noticed that the container lifetime is linked with the Visual Studio process. If I close the Visual Studio the container is stopped and removed and as soon as I open the solution with the previously mentioned project in the Visual Studio the container is immediately up and running even though I haven't even started debugging yet.

If you want to run a container outside of the Visual Studio, you can always build the image from the Dockerfile in your project and run it.

It could look like this:

cd "path/with/your/Dockerfile"
docker build -t "your-image-name" .
docker run -it -p 8000:80 -n "your-container-name" "your-image-name"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM