title: How do I get a connection string into a docker container in heroku securely?
body: <p>I've been losing my mind trying to find a way to securely get a connection string into a docker container when using Heroku.
  This is what <a href="https://devcenter.heroku.com/articles/container-registry-and-runtime" rel="nofollow noreferrer">the docs</a> told me:</p>
  
  <p>"We suggest using ENV for runtime variables (e.g., GEM_PATH) and heroku config for credentials, so that sensitive credentials aren’t accidentally checked into source code control."</p>
  
  <p>In this case for a connection string I'd want to set a variable via heroku config, thats what I did.
  Hit <a href="https://stackoverflow.com/questions/57451607/heroku-cli-commands-pico-not-recognized-on-windows-10">this</a> trying to use heroku config:edit, so I'm using heroku config --json.</p>
  
  <p>This variable is in there:</p>
  
  <pre><code>  "ConnectionStrings__Default": "MyConnectionString"
  
  </code></pre>
  
  <p>Now as for how to pass this in the actual dockerfile I'm not good with docker so a lot of this is guess work in combination with what I found about externally getting connection strings into c# <a href="https://www.benday.com/2017/12/20/ef-core-asp-net-core-read-connections-strings-from-environment-variables/" rel="nofollow noreferrer">here</a>. I am suspecting the problem is somewhere around here so please do tell me if this is incorrect:</p>
  
  <pre><code># publish
  FROM build AS publish
  WORKDIR /src/webapplication/webapplication
  RUN dotnet publish -c Release -o /src/publish
  FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
  WORKDIR /app
  COPY --from=publish /src/publish .
  # heroku uses the following
  CMD Default=$ConnectionStrings__Default ASPNETCORE_URLS=http://*:$PORT dotnet webapplication.dll
  </code></pre>
  
  <p>Am I correct in inserting a configuration variable into an aspnet core application in docker this way? I could find very little examples of this so not sure.</p>
  
  <p>The code getting the connection string (works when using json.config):</p>
  
  <pre><code>        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              string basePath = Directory.GetCurrentDirectory();
              if (!optionsBuilder.IsConfigured)
              {
                  var builder = new ConfigurationBuilder()
         .SetBasePath(basePath)
         .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
  
                  IConfigurationRoot configuration = builder.Build();
  
                  string connectionString = configuration.GetConnectionString("Default");
                  optionsBuilder.UseSqlServer(connectionString)
                      .EnableSensitiveDataLogging();
                  optionsBuilder.UseLazyLoadingProxies();
  
              }
          }
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
  </code></pre>
  
  <p>If anyone could give me some pointers it would mean the world to me because I want to deploy this thing but I have no idea what to do.</p>