Visual Studio is growing better every year and the Docker Tools are no exception, released a few years ago are improved continuously with more and more functionalities like docker compose support.
Sometimes when you want to customize how containers are run you can feel lost because there are no much docs to read on how things are executed inside Visual Studio when using Docker and how to customize.
In my specific case, I have a few WordPress websites running inside Pengwin a Linux Distribution based on Debian that run on WSL (Windows Subsystem for Linux). These sites all have the default WordPress rest API enabled with some custom endpoints.
This is why I needed to make them reachable from a docker container for my ASP.NET Core application which is running an HostedBackgroundService that scrape some data an then it should post the new data inside WordPress creating a few posts and other stuff.
But I found myself unable to do so because the domains where mapped inside the Windows HOST file with the local IP and this are not reachable inside my own container. But after a lot of pain, I have found out that I could just add a tag inside my csproj so I could pass all the custom parameters I need, which currently is only the –add-host param.
Can I just use the host network so all my local domains are discovered automatically?
This is the first thing I looked for because it would have been the best and practical way to do it, all my domain that where specify on host file would have been picking up automatically. But unfortunately, this does not work either on Windows or MacOS, this because on these OS we have to run a VM with Linux in the background, and thus this only work on Linux when docker is running directly on the host.
Specify one or more custom host mapping for docker
You can pass multiple custom host-to-ip mapping, this way you can easily make accessible your multiple or just one test/dev domains inside your container.
So open your csproj file and then after the first PropertyGroup, or just anywhere inside the root Project tag add the following snippet inside your csproj, that was previously configured with docker support.
<PropertyGroup>
<DockerfileRunArguments>--add-host mydomain.local:192.168.95.1 --add-host www.mydomain.local:192.168.95.1</DockerfileRunArguments>
</PropertyGroup>
With just these few lines now you can easily access all your local domains inside your docker container. Then you can just hit F5 to debug, and this time your code should be able to reach those domains.
Thanks to –add-host your custom domain is being added to the container /etc/hosts file, and this should also allow you to make reachable some others domain inside your network, that you usually statically map on your dev box.
Can i specify other custom Run arguments inside it?
Yes, sure you can! but beware as the network argument to share with your host doesn’t work on Windows there is the possibility that others command won’t work with on Windows or could cause some issues on Visual Studio, so your best bet is to try and see. Some use case, for example, could be setting a memory limit for your container to try how your app will work on a memory-restricted scenario.