Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose not setting environment variables

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"]
like image 658
Neil Avatar asked Oct 13 '15 15:10

Neil


People also ask

Can I use environment variables in Docker compose file?

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.

Does Docker compose override environment variables?

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.

How pass env variable in Docker compose command?

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.

How do I set an environment variable in Docker?

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.


1 Answers

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
like image 61
Aurélien Bottazini Avatar answered Sep 21 '22 17:09

Aurélien Bottazini