skip to Main Content

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


  1. It could possibly be two cases.

    1. Check the key name in your env file and the docker run command
    2. Check the path of the env file you are mapping to.
    Login or Signup to reply.
  2. 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:

    docker exec --env-file=.env -i {image} bash -c 'mysql -uroot -p$DB_PASSWORD {database}' < location/of/file.sql
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search