Spaces in environment variables in a stack compose file for microsoft/aspnet cause the servicemonitor.exe to fail

I have a compose file which looks like this:

version: "3"
services:
 core:
    environment:
    - ConnectionString="Data Source=database;Initial Catalog=Core;User Id=core;Password=somepassword"
    image: microsoft/aspnet:4.7.2
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
    networks:
      - webnet

I’m using this command to run this:

docker stack deploy --resolve-image always --compose-file docker-compose.yml core

If I remove the spaces in the connection string the service starts successfully.

If I leave the spaces in the connection string env var the service fails.

Running

docker service logs core_core

Gives me the following:

APPCMD failed with error code 87
Failed to update IIS configuration
Service 'w3svc' has been stopped

My assumption is that servicemonitor.exe fails and exits with an error if it encounters an environment variable with a space in it.

Escaping the space with a backslash \ does not resolve the issue as it appears to be double escaped by the time it reaches the IIS environment space.

It took me a few hours to find out it doesn’t directly has to do with the spaces, but does have to do with the quotes…