12

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.

yamenk
  • 46,736
  • 10
  • 93
  • 87
Righto
  • 855
  • 3
  • 11
  • 32
  • i think you write to file env variables before overriding it. Try to enter into container `docker exec -it ` and run `printenv`. – Bukharov Sergey Jan 11 '18 at 07:49
  • 2
    Again, as I mentioned in https://stackoverflow.com/questions/40873165/use-docker-run-command-to-pass-arguments-to-cmd-in-dockerfile/40873331?noredirect=1#comment83384105_40873331, RUN is compile-time only (docker build). Your properties file is already set. You need your ENTRYPOINT to call a script which will do what your RUN commands do, and call catalina.sh. – VonC Jan 11 '18 at 07:53
  • Yes, I tried using docker exec to see the changed properties but they were still set to default. Will try out what @VonC has mentioned – Righto Jan 11 '18 at 08:13
  • I will post an answer later today (I am at work right now) – VonC Jan 11 '18 at 08:15

2 Answers2

6

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:

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks @VonC. I was able to achieve this yesterday. I am accepting this answer because it addresses the issue exactly and is clear and concise. – Righto Jan 12 '18 at 08:27
3

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:

  1. Move $dummy_url >> /usr/local/tomcat/conf/test.properties and $database >> /usr/local/tomcat/conf/test.properties to the start of catalina.sh script.

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

yamenk
  • 46,736
  • 10
  • 93
  • 87
  • Thanks! How can I create a new script instead, which includes the move commands above and then calls catalina.sh? What would the entrypoint look like? I tried doing this using the build command like you mentioned but because I would deploying an ECS task and passing these parameters from there, I wanted to avoid doing this as a build step – Righto Jan 11 '18 at 08:30
  • Also, instead of that, if I set all these properties under CATALINA_OPTS="-Ddummy_url=http://google.com -Ddatabase=somedb", how can I ensure that these get picked up and test.properties gets updated. I would prefer using the original catalina.sh and either use CATALINA_OPTS or a separate .sh which triggered catalina.sh – Righto Jan 11 '18 at 08:48
  • create a new script with following `$dummy_url >> /usr/local/tomcat/conf/test.properties $database >> /usr/local/tomcat/conf/test.properties sh ./catalania.sh run` and change `ENTRYPOINT ["script.sh", "run"]` – yamenk Jan 11 '18 at 08:51
  • I get this weird error - standard_init_linux.go:185: exec user process caused "no such file or directory" My .sh file starts with #!/bin/bash followed by all echo property1=$property1 >> /usr/local/tomcat/test.properties statements and then just adding $CATALINA_HOME/bin/catalina.sh run – Righto Jan 11 '18 at 08:59
  • I also tried #!/bin/sh on top of the script I wrote but getting the same error – Righto Jan 11 '18 at 09:44
  • Ok, this worked after I followed some instructions here on the error above - http://willi.am/blog/2016/08/11/docker-for-windows-dealing-with-windows-line-endings/ thanks for your help! – Righto Jan 11 '18 at 10:44