3

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

6
  • Try sh -c "echo $TYPE" Commented Sep 27, 2017 at 19:25
  • I'm getting the same error and a blank like. I don't understand why TYPE is empty as it is set in the command... Commented Sep 27, 2017 at 19:27
  • Can you change command to env | grep TYPE= and reply with what gets printed to the console? Commented Sep 27, 2017 at 19:28
  • TYPE=result is my output for that... as expected... Commented Sep 27, 2017 at 19:30
  • try to use {} for env variable command: sh -c '$$(echo ${TYPE})' Commented Sep 27, 2017 at 19:40

1 Answer 1

11

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 $TYPE escapes it.

Reference:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.