Docker-compose override not overriding connection string

Viewed 5267

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...

2 Answers

Using double underscores worked for me

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

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.

Related