Jonatas Amaral
Jonatas Amaral

Reputation: 111

pass password from .env on command to mysql inside docker

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.

Upvotes: 2

Views: 3164

Answers (2)

kaznovac
kaznovac

Reputation: 1572

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

Upvotes: 3

hemanth sharma
hemanth sharma

Reputation: 43

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.

Upvotes: 0

Related Questions