3

I wanted to create a docker-compose file, but the following seems to fail:

...
services:
  test:
    ...
    volumes:
      - ${FOO}:/bar
...

I've read that environment variables are not evaluated in keys, only in values. But since the $FOO is a value, I thought this would work?

2 Answers 2

3

This works. You have to declare FOO somewhere to get the result. Following code will work:

caller.sh

#!/bin/sh
export FOO=/home
docker-compose up
Sign up to request clarification or add additional context in comments.

6 Comments

Could I also use docker-compose build followed by docker run -it ... ? Or does this maybe not mount the specified volumes?
You can use whatever you want. Just shown you that you need to declare and export this variable before using
The variable is defined in my .bashrc, I'm just trying to find out why /bar is non-existent in my container.
echo $FOO on host -- what is the result?
Okay I found it. When I do docker-compose run container /bin/bash it does add the volume, but docker-compose build container followed by docker run container does not add it. Thanks for your help!
|
1

So apparently it does work to expand environment variables that way.

But docker-compose build && docker run does not mount these volumes automatically, opposed to docker-compose up (or docker-compose build && docker compose run).

If someone could explain this behavior, I would be glad.

1 Comment

Because docker run has nothing to do with your docker-compose.yml file. If you want to mount volumes with docker, not docker-compose, use generic syntax like docker run -v $FOO:/bar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.