Reputation: 23
I am trying to run a command from a docker-compose service, and I want to to use the environment variables defined in my container like this:
In my Makefile:
sqlcmd:
docker-compose run sqlcmd sqlcmd -S sqlserver -U "$SQL_HOST" -P "$SQL_PASSWORD"
Then I run make sqlcmd
.
But it's printing out truncated words. What I want it to do is use the environment variables $SQL_HOST
and $SQL_PASSWORD
, which are defined in the container.
How do I get this to escape properly?
Upvotes: 2
Views: 3092
Reputation: 25344
To get a literal $, use $$.
docker-compose run sqlcmd sqlcmd -S sqlserver -U "$$SQL_HOST" -P "$$SQL_PASSWORD"
If SQL_HOST and SQL_PASSWORD are variables defined earlier in your Makefile, (rather than environment variables) you need to write it like this:
SQL_HOST=localhost
SQL_PASSWORD=hunter2
sqlcmd:
docker-compose run sqlcmd sqlcmd -S sqlserver -U "$(SQL_HOST)" -P "$(SQL_PASSWORD)"
PS: Welcome to the site.
EDIT: To delay expansion of the variables until the container has started, you need to prevent your shell from expanding them. If a variable is enclosed in single quotes, the shell will not try to expand it.
Here's an example:
docker-compose run sqlcmd sqlcmd -S sqlserver -U '"$$SQL_HOST"' -P '"$$SQL_PASSWORD"'
EDIT EDIT: I guess you need this too, to tell Docker to use the shell to expand the variables:
docker-compose run sqlcmd sh -c 'sqlcmd -S sqlserver -U "$$SQL_HOST" -P "$$SQL_PASSWORD"'
Upvotes: 1