I have created an ASP.NET CORE api and added Docker support to the project (sftt.app
). I also have a MongoDB instance in another container (sftt.db
). In order to ease connecting the application to the Database, I use the name of the Database container in the connection string. This requires that both containers be connected to a Docker Network, which I have named sftt-net
. When I start the container for sftt.app
I would like to automatically connect to sftt-net
as part of it's docker run
just as I do with my database, however I can't find an option for doing this in Visual Studio.
I tried adding a command line argument "commandLineArgs": "--network sftt-net"
in the launchSettings.json
file, but that didn't work. (I also tried connect sftt-net sftt.app
)
How can I add the api container to the Network on startup when it's launched from Visual Studio?
You can specify additional Docker arguments by editing the csproj file and adding the following:
<DockerfileRunArguments>arguments go here</DockerfileRunArguments>
An example of a csproj file edited to connect the Docker container to a network called mongo_cluster
would look as follows
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileRunArguments>--network mongo_cluster</DockerfileRunArguments>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.9" />
<PackageReference Include="MongoDB.Driver" Version="2.11.4" />
</ItemGroup>
</Project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With