Here's my docker image. I want to override the default environment variables being set below from whatever is passed in the docker run command mentioned in the end
FROM ubuntu:16.04
ADD http://www.nic.funet.fi/pub/mirrors/apache.org/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz /usr/local/
RUN cd /usr/local && tar -zxvf apache-tomcat-8.0.48.tar.gz && rm apache-tomcat-8.0.48.tar.gz
RUN mv /usr/local/apache-tomcat-8.0.48 /usr/local/tomcat
RUN rm -rf /usr/local/tomcat/webapps/*
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64
ENV CATALINA_HOME /usr/local/tomcat
ENV CATALINA_BASE /usr/local/tomcat
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
ENV dummy_url defaulturl
ENV database databasedefault
COPY my.war /usr/local/tomcat/webapps/
RUN echo >> /usr/local/tomcat/conf/test.properties
RUN echo dummy_url =$dummy_url >> /usr/local/tomcat/conf/test.properties
RUN echo database =$database >> /usr/local/tomcat/conf/test.properties
ENTRYPOINT ["catalina.sh", "run"]
To run in local :
docker run -p 8080:8080 -e dummy_url=http:google.com -e database=jdbc://mysql allimages/myimage:latest
dummy_url and database do not seem to be getting overridden in the file that I am adding them in - test.properties. Any ideas would be greatly appreciated.
I want to override the default environment variables being set below from whatever is passed in the docker run command mentioned in the end
That means overriding an image file (/usr/local/tomcat/conf/test.properties
) when running the image as a container (docker run
), not building the image (docker build
and its --build-args
option and its ARG
Dockerfile entry).
That means you create locally a script file which:
- modifies
/usr/local/tomcat/conf/test.properties
- calls
catalina.sh run $@
(see also "Store Bash script arguments $@ in a variable" from "Accessing bash command line args $@
vs $*
")
That is:
myscript.sh
#!/bin/sh
echo dummy_url=$dummy_url >> /usr/local/tomcat/conf/test.properties
echo database=$database >> /usr/local/tomcat/conf/test.properties
args=("$@")
catalina.sh run "${args[@]}"
You would modify your Dockerfile to COPY
that script and call it:
COPY myscript.sh /usr/local/
...
ENTRYPOINT ["/usr/local/myscript.sh"]
Then, and only then, the -e
options of docker run would work.
You are confusing what gets executed when building the image and what gets executed when starting the container.
The RUN
command inside the dockerfile is executed when building the image, when running docker build ...
RUN echo dummy_url =$dummy_url >> /usr/local/tomcat/conf/test.properties
RUN echo database =$database >> /usr/local/tomcat/conf/test.properties
Thus when the above execute the file test.properties
will contain the default values specified in the Dockerfile.
When you execute docker run -p 8080:8080 -e dummy_url=http:google.com -e database=jdbc://mysql allimages/myimage:latest
the ENTRYPOINT ["catalina.sh", "run"]
will get executed with
env values dummy_url=http:google.com
and database=jdbc://mysql
.
You can allow values in test.properties to be ovveriden using:
- Move
$dummy_url >> /usr/local/tomcat/conf/test.properties
and $database >> /usr/local/tomcat/conf/test.properties
to the start of catalina.sh
script.
- Override the values when building the image as such:
ARG dummy_url_arg
ARG database_arg
ENV dummy_url $dummy_url_arg
ENV database $database_arg
COPY my.war /usr/local/tomcat/webapps/
RUN echo >> /usr/local/tomcat/conf/test.properties
RUN echo dummy_url =$dummy_url >> /usr/local/tomcat/conf/test.properties
RUN echo database =$database >> /usr/local/tomcat/conf/test.properties
ENTRYPOINT ["catalina.sh", "run"]
And when building the image override the values using docker build --build-arg dummy_url_arg=http:google.com --build-arg database_arg=jdbc://mysql allimages/myimage:latest ...