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.
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.
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
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
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