简体   繁体   中英

docker-compose not overriding dockerfile environment variables

I am trying to set basic postgres info from my docker-compose. The container starts but the variables from the Dockerfile are not overridden when I run docker-compose up . Please help.

FROM mine/debian7

## START: UPDATES & INSTALLS ###########################################################################################
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" >> /etc/apt/sources.list.d/pgdg.list && \
    wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | apt-key add -  && \
    apt-get update && \
    apt-get upgrade && \
    apt-get install -y python-software-properties software-properties-common postgresql-9.4 postgresql-client-9.4 postgresql-contrib-9.4 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
## END: UPDATES & INSTALLS #############################################################################################

ENV DB_USER_NAME test
ENV DB_PASSWORD test
ENV DB_NAME test

## START: CONCFIGURATION ###############################################################################################
# start postgres at boot
#RUN     echo "/etc/init.d/postgresql start" >> ~/.bashrc
USER    postgres
RUN /etc/init.d/postgresql start && \
    psql --command "CREATE USER $DB_USER_NAME WITH SUPERUSER PASSWORD '$DB_PASSWORD';" && \
    createdb -O $DB_NAME $DB_NAME
RUN     echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.4/main/pg_hba.conf
RUN     echo "listen_addresses='*'" >> /etc/postgresql/9.4/main/postgresql.conf
VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
#USER root
#RUN update-rc.d postgresql defaults
## END: CONCFIGURATION #################################################################################################


EXPOSE 5432

CMD service postgresql start && tail -f /var/log/postgresql/postgresql-9.4-main.log

And my docker-compose file:

tomcat:
  image: clegge/tomcat
  ports:
    - "8080:8080"
  volumes:
    - sample.war:/opt/tomcat7/webapps/sample.war

postgres:
  build: /Users/clegge/Dockers/docker-postgres-base/
  ports:
    - "5432:5432"
  stdin_open: true
  tty: true
  environment:
   - DB_USER_NAME=legge_crud
   - DB_PASSWORD=legge_crud
   - DB_NAME=test

What am I missing?

In your Dockerfile you use that environment variables in RUN statements. Those are executed during build time while you build your image.

When you start your container with docker run it just starts a container based on the image that you just built. The image at that point in time already exists and the RUN statements are not executed again.

So setting those environment variables during runtime will have no effect.

The docker image build process is intended to be self contained. That is, the process of building an image shouldn't take any inputs other than the Dockerfile and the Dockerfile directory. This is intended to make it hard for the image to accidentally depend on the build environment.

Setting environment at docker-compose only affects environment variables at run time, not image build time.

You should initialize your database at run time.

Btw, there's already a very good official postgres image .

Yes @Lie Ryan pointed out that there is an official postgres image out there. Nonetheless, the question/issue was not addressed, which was...

docker-compose not overriding Dockerfile environment variables

Which I intend to address now.

Don't use ENV but ARG for your expected behavior.

ARG is well documented .

For example...

- Dockerfile

ARG NODE_ENV=production # This will default to "production"

- docker-compose.yaml

build:
  context: "../path-to-Dockerfile-folder"
  args: [ "NODE_ENV=development" ]

This way, NODE_ENV will be development when ran with docker-compose up

for the record i had to use the docker-entrypoint.sh and hack my dockerfile to match:

https://github.com/docker-library/postgres/tree/a82c28e1c407ef5ddfc2a6014dac87bcc4955a26/9.4

doing this i was able to inject the variables i need on my docker-compose yml file...

In the Dockerfile you should have :

ENV DB_USER_NAME=${DB_USER_NAME}
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_NAME=${DB_NAME}

Then values from docker-compose.yml will be used and as you want some default value in your Dockerfile you can do this trick

Dockerfile :

ARG DB_USER_NAME=test
ARG DB_PASSWORD=test
ARG DB_NAME=test

ENV DB_USER_NAME=${DB_USER_NAME}
ENV DB_PASSWORD=${DB_PASSWORD}
ENV DB_NAME=${DB_NAME}

And your docker-compose.yml should have these values (the current docker-compose.yml in the question is then right) :

environment:
    - DB_USER_NAME=legge_crud
    - DB_PASSWORD=legge_crud
    - DB_NAME=test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM