I have the following .env file:
MY_SECRET_POSTGRES_PASSWORD=hello
I installed dotenv
to load variables from this env file and run the docker command as follows:
dotenv -- docker run --name postgresql-container -p 5432:5432 -e POSTGRES_PASSWORD=$MY_SECRET_POSTGRES_PASSWORD -d postgres
When I run the command, I get the following error:
Error: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
However, when I just run the following command to ensure that the env is loaded, it shows up fine:
dotenv -- bash -c 'echo "$MY_SECRET_POSTGRES_PASSWORD"'
I know I can use --env-file
to pass in the entire env file but I rather like to pick the values that I pass is so that I do not expose anything unnecessary to the container.
CodePudding user response:
See the difference in output:
MIHAI=test bash -c "echo Var is: $MIHAI"
Var is:
MIHAI=test bash -c 'echo Var is: $MIHAI'
Var is: test
The difference is the single and double quotes.
When you run this:
dotenv -- docker run --name postgresql-container \
-p 5432:5432 -e POSTGRES_PASSWORD=$MY_SECRET_POSTGRES_PASSWORD -d postgres
MY_SECRET_POSTGRES_PASSWORD
gets evaluated before it is sent to dotenv
. So dontenv does not receive a $...
but an empty value.
Correct:
dotenv -- bash -c 'docker run --name postgresql-container -p 5432:5432 -e POSTGRES_PASSWORD=$MY_SECRET_POSTGRES_PASSWORD -d postgres'
This way what is between the single quotes is sent as is to dotenv.