When I run docker-compose build && docker-compose up redis
, with environment
specified in docker-compose.yaml
and RUN env
in the Dockerfile
, the environment variables I set don't get printed.
Why does this not work?
I'm using docker-compose
version 1.4.2.
Here are the relevant files:
docker-compose.yaml
with environment
as a list of KEY=value
pairs:
redis:
build: ../storage/redis
ports:
- "6379:6379"
environment:
- FOO='bar'
docker-compose.yaml
with environment
as a dictionary:
redis:
build: ../storage/redis
ports:
- "6379:6379"
environment:
- FOO: 'bar'
Dockerfile
:
FROM redis:2.6
MAINTAINER [email protected]
RUN mkdir -p /var/redis && chown -R redis:redis /var/redis
RUN echo '-------------- env ---------------'
RUN env
COPY redis.conf /usr/local/etc/redis/redis.conf
EXPOSE 6379
ENTRYPOINT ["redis-server", "/usr/local/etc/redis/redis.conf"]
It seems that docker-compose has native support now for default environment variables in file. all you need to do is declare your variables in a file named . env and they will be available in docker-compose. yml.
But docker-compose does not stop at the . env and the host's current environment variables. It's cool that you can simply override values of your . env file, but this flexibility is can also be the source of nasty bugs.
Set environment variable with the --env-file flag If you want to pass a file containing all of your environment variables to a Docker container, you can use the --env-file flag. The --env-file flag allows you to pass a file containing environment variables to a Docker container.
Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.
That's normal
docker-compose
only sets the environment variables specified in the environment
directive in the docker-compose.yaml
file during the run phase of the container, and not during the build phase.
So if you do docker-compose run --entrypoint "/bin/bash" redis -c env
you will be able to see your env variables.
If you want to set variables inside your Dockerfile (to be able to see them during the build phase) you can add inside your dockerfile before your RUN env
:
ENV FOO bar
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