简体   繁体   中英

How do I get a connection string into a docker container in heroku securely?

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 the docs told me:

"We suggest using ENV for runtime variables (eg, GEM_PATH) and heroku config for credentials, so that sensitive credentials aren't accidentally checked into source code control."

In this case for a connection string I'd want to set a variable via heroku config, thats what I did. Hit this trying to use heroku config:edit, so I'm using heroku config --json.

This variable is in there:

  "ConnectionStrings__Default": "MyConnectionString"

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# here . I am suspecting the problem is somewhere around here so please do tell me if this is incorrect:

# 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

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.

The code getting the connection string (works when using json.config):

        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();

            }
        }

The error:

System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')

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.

After .AddJsonFile() you need to call .AddEnvironmentVariables() which will overwrite any settings from your settings file with environment variables that match.

Your code should look like this

var builder = new ConfigurationBuilder()
    .SetBasePath(basePath)
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables();

Another option is to use the -e flag when calling docker run to set environment variables in the container

https://docs.docker.com/engine/reference/run/#env-environment-variables

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