I basically know nothing about docker. And not that much more about bash neither. So:
There’s a command in the README of a Laravel project i’m working on, that shows how to fill some data on local MySQL docker image, by sending a queries from a file located in the HOST.
docker exec -i {image} mysql -uroot -p{password} {database} < location/of/file.sql
What i want to do is "hide" the password from README, and make it read from .env
file
So, i want to do something like this:
docker exec --env-file=.env -i {image} mysql -uroot -p$DB_PASSWORD {database} < location/of/file.sql
I’ve tested that docker ... printenv
does show the variables from the file. But echoing one of then outputs a blank line: docker ... echo $DB_PASSWORD
and running MySQL command using it, gets me "Access denied for user ‘root’@’localhost’"
I’ve tried run the MySQL command "directly": docker ... mysql ... < file.sql
and also "indirectly": docker bash -c "mysql ..." < file.sql
.
2
Answers
It could possibly be two cases.
You should prevent your shell from expanding the local variables (by single-quoting, or by escaping
$
)This should be passed to containers shell and expanded there: