Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose override not overriding connection string

I have an ASP.NET web app which connects to a SQL database.

In appsettings.Development.json the connection string is defined as

"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=WebRecipes-LocalDev;Trusted_Connection=True;ConnectRetryCount=0"
  }

In my docker-compose.yml file I get the latest mssql-server-linux

services:
  sql.data:
      image: microsoft/mssql-server-linux:2017-latest

  webrecipes.api:
     image: ${DOCKER_REGISTRY-}webrecipesapi
     build:
       context: .
       dockerfile: src/WebRecipes.Api/Dockerfile

In docker-compose.override.yml I set the username and password for sql.data and override the connection string for my asp.net core app

services:
  sql.data:
    environment:
      - SA_PASSWORD=Pass@word
      - ACCEPT_EULA=Y
    ports:
      - "5533:1433"

  webrecipes.api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
      - ASPNETCORE_HTTPS_PORT=44302
      - ConnectionString=Server=sql.data;database=WebRecipes-LocalDev;User Id=sa;Password=Pass@word
    ports:
      - "2282:80"
      - "44302:443"
    volumes:
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
    depends_on:
      - sql.data
  • Edit: I also tried setting the connection string to:

- ConnectionStrings:DefaultConnection=sql.data;database=WebRecipes-LocalDev;User Id=sa;Password=Pass@word

When I run the app and it first tries to hit the db I get error saying LocalDb is not supported as it is using the connection string defined in appsettings.Development.json

Can anyone help with this? I don't see what I'm doing wrong...

like image 254
Vinyl Warmth Avatar asked Jun 23 '19 08:06

Vinyl Warmth


2 Answers

Using double underscores worked for me

ConnectionStrings__DefaultConnection=sql.data;database=WebRecipes-LocalDev;User Id=sa;Password=Pass@word

like image 93
Greg Avatar answered Sep 23 '22 14:09

Greg


The ASP.NET Core official docs on environment variable configuration has the following explanation:

When working with hierarchical keys in environment variables, a colon separator (:) may not work on all platforms (for example, Bash). A double underscore (__) is supported by all platforms and is automatically replaced by a colon.

like image 32
Vinyl Warmth Avatar answered Sep 22 '22 14:09

Vinyl Warmth