zed101
zed101

Reputation: 323

How to place a connection string in an env file

I have a .net core application to be dockerized. Along with it is an env file where I am trying to define a connection string with the following format:

Server=localhost;Port=3306;Database=myDb;Uid=root;Pwd=root;allow user variables=true;SslMode=none;maxpoolsize=100;Convert Zero Datetime=true;

I have tried the following two in env file with no success:

connectionString="Server=localhost;Port=3306;Database=myDb;Uid=root;Pwd=root;allow user variables=true;SslMode=none;maxpoolsize=100;Convert Zero Datetime=true;"

connectionString='Server=localhost;Port=3306;Database=myDb;Uid=root;Pwd=root;allow user variables=true;SslMode=none;maxpoolsize=100;Convert Zero Datetime=true;'

Upvotes: 2

Views: 20234

Answers (1)

muratiakos
muratiakos

Reputation: 1092

file approach were you using, but I recommend using named env-files, so you can differentiate, you can follow this example:

Create myenv-dev.list file with your env var definition let say for your dev environment, and a subsequent one for all your environments - dev/test/uat/prod, eg:

ASPNETCORE_ENVIRONMENT=dev
connectionString="Server=localhost;Port=3306;Database=myDb;Uid=root;Pwd=root;allow user variables=true;SslMode=none;maxpoolsize=100;Convert Zero Datetime=true;"
...

And then pass this to docker - let's say with a printenv to validate this:

docker run -it --env-file ./myenv-dev.list microsoft/aspnetcore printenv

And then as a results would look something, like for all your case-sensitive environment variables:

ASPNETCORE_ENVIRONMENT=dev connectionString="Server=localhost;Port=3306;Database=myDb;Uid=root;Pwd=root;allow user variables=true;SslMode=none;maxpoolsize=100;Convert Zero Datetime=true;"

...

If you wish to implement this with a compose file, you can do this in your docker-compose.yml file as well per environment:

version: '3'
services:
  myservice:
    image: 'microsoft/aspnetcore'
    env_file:
     - ./myenv-dev.list

See more examples at:

On the other hand you may want to consider using a secret-management system like AWS Parameter Store or Secrets Manager later to store your database passwords and other sensitive info at a safe place and fetch those dynamically with something like pstore:

Upvotes: 1

Related Questions