Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect Docker container to Docker Network when started in Visual Studio

Tags:

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?

like image 947
Lord Drake Avatar asked Aug 06 '20 02:08

Lord Drake


1 Answers

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>

like image 78
Steve Avatar answered Oct 06 '22 01:10

Steve