Running the command docker-compose run -e TYPE=result mongo_db_backup should give me the value of the given TYPE variable:
mongo_db_backup:
image: 'mongo:3.4'
volumes:
- '/backup:/backup'
command: sh -c '$$(echo $TYPE)'
But instead I get the error The TYPE variable is not set. Defaulting to a blank string. What am I doing wrong
It happens that Compose expands $TYPE before it gets to the inside of the container. Compose looks for the $TYPE environment variable in the shell or host environment and substitutes its value in.
This will work with the following terminal command:
docker-compose.yml
command: sh -c 'echo $TYPE'
terminal command
TYPE='hello world' docker-compose run web
When there is no $TYPE environment variable in the host machine, Compose sets the value of $TYPE to an empty string and outputs a warning.
Compose needs to be informed not to expand $TYPE since we want it expanded inside of the shell running in the container.
For this use
docker-compose.yml
command: sh -c "echo $$TYPE"
Prepending a dollar symbol to
$TYPEescapes it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With