PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

[FIXED] How do I replace appsettings.json values with .env variable in docker-compose command to launch an ASP.NET Core MVC app?

 November 05, 2022     appsettings, asp.net-core-mvc, c#, docker, environment-variables     No comments   

Issue

I have a C# / ASP.NET Core MVC app that I have been deploying via Docker. I have been hard-coding variables in the appsetting.json file and accessing them via calls like this for example

services.Configure<ApiEndpoints>(Configuration.GetSection("ApiEndpoints"));

in my Startup.cs.

But recently I started adding variables for deploying into a .env file and deploying like this docker-compose --env-file .env up. But I can't access the variables in my .env file in the .NET app.

For example I tried this

Console.WriteLine(Configuration.GetSection("APPSETTINGS_DB_STR"));

in Startup.cs where in the .env file I added this variable APPSETTINGS_DB_STR=isThisLoggingCorrectly and it logged as

amaranth_main               | Microsoft.Extensions.Configuration.ConfigurationSection

in my console... So how do I get the asp.net to access the docker .env variable?

This is my docker-compose.yml

version: '3.8'

volumes:
  data:

services:
  postgresql_MENTIONvlt_bg:
    image: postgres
    # explicit container name
    container_name: postgresql_vlt_bg
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    ports:
      - 5432:5432
    volumes:
      - data:/var/lib/postgresql_vlt_bg
  amaranth_main:
    container_name: amaranth_main
    links:
      - postgresql_MENTIONvlt_bg
    depends_on:
      - postgresql_MENTIONvlt_bg
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8000:5000
      - 8001:5001
    environment:
      - ASPNETCORE_ENVIRONMENT=Release
      - ASPNETCORE_Kestrel__Certificates__Default__Password=${Kestrel_Cert_Password}
      - ASPNETCORE_Kestrel__Certificates__Default__Path=${Kestrel_Cert_Rel_Path}
      - APPSETTINGS_DB_STR=${APPSETTINGS_DB_STR}
    volumes:
      - ${Kestrel_Cert_Abs_Path}:/https:ro

P.S. POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB are being passed through just fine.


Solution

The fix was to first make APPSETTINGS_DB_STR into APPSETTINGS__DB__STR. Then I had to access the variable like this in my c# app code. Environment.GetEnvironmentVariable(“APPSETTINGS__DB__STR”)



Answered By - ChristianOConnor
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing