Patrick
Patrick

Reputation: 476

Docker CLI env var value override not respected by container on startup

I'm using the following Dockerfile to build an image. The image compiles just fine.

FROM java:8u40-jdk

ENV CATALINA_HOME="/usr/local/tomcat" 
ENV PATH=$CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"

WORKDIR $CATALINA_HOME

ENV TOMCAT_MAJOR 8
ENV TOMCAT_VERSION 8.0.20
ENV TOMCAT_DL_URL http://archive.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz
ENV TOMCAT_ENV local
RUN set -x \
    && curl -fSL "$TOMCAT_DL_URL" -o tomcat.tar.gz \
    && tar -xvf tomcat.tar.gz --strip-components=1 \
    && rm bin/*.bat \
    && rm tomcat.tar.gz

### remove the closing tag, then use the echo|tee pattern to build it back in
RUN sed -i 's|</tomcat-users>| |' $CATALINA_HOME/conf/tomcat-users.xml
RUN echo "<role rolename=\"admin\" />" | tee -a $CATALINA_HOME/conf/tomcat-users.xml \
    && echo "<user username=\"${MANAGER_USER:-admin}\" password=\"${MANAGER_PW:-password}\" roles=\"standard,manager,admin,admin-gui,manager-gui,manager-status,manager-script\"/>" | tee -a $CATALINA_HOME/conf/tomcat-users.xml \
    && echo "</tomcat-users>" | tee -a $CATALINA_HOME/conf/tomcat-users.xml

### setup setenv.sh
RUN echo "CATALINA_PID=\"\$CATALINA_HOME/bin/catalina.pid\"" | tee $CATALINA_HOME/bin/setenv.sh
RUN echo "CATALINA_OPTS=\"\$CATALINA_OPTS -Xms512m -Xmx1024m -Denv=$TOMCAT_ENV \
  -Dlogging_override=file://$CATALINA_HOME/logging_override.xml \
  -Doverride_file=$CATALINA_HOME/override.properties\"" | \
    tee -a $CATALINA_HOME/bin/setenv.sh \
    && chmod 755 $CATALINA_HOME/bin/setenv.sh

EXPOSE 8080

CMD ["catalina.sh", "run"]

When I run the image, I often want/need to change the value of the TOMCAT_ENV environment variable declared in the dockerfile. However, when I run a command such as:

docker run -p 8080:8080 -d -e TOMCAT_ENV=dev --name tc tomcat

The overridden value provided on the command line isn't respected, and the default value "local" is used.

I'm using Docker 1.7.0.

If you know a little about Tomcat, you can see that I'm somewhat clunkily trying to establish a login for the Manager UI, and trying to assemble a setenv.sh file without adding a file from a filesystem. Perhaps I should do this a different way - if you're aware of a "better" way please educate me. The goal of my asking this question is to have the runtime override setting of this variable be respected.

Thanks in advance.

Upvotes: 2

Views: 2265

Answers (1)

OrangeTux
OrangeTux

Reputation: 11471

The TOMCAT_ENV environment variable is used during build time to create setenv.sh. It looks something like this:

ATALINA_OPTS="$CATALINA_OPTS -Xms512m -Xmx1024m -Denv=local -Dlogging_override=file:///logging_override.xml -Doverride_file=/override.properties"

You are changing this variable at runtime, but setenv.sh already created at this time and the environment is set to local.

If you want to change the environment at runtime you should not write the value of the variable TOMCAT_ENV to setenv.sh, but a reference to the variable. You can do that by escaping the variable in your Dockerfile by prepeding $TOMCAT_ENV with a slash to \$TOMCAT_ENV:

RUN echo "CATALINA_OPTS=\"\$CATALINA_OPTS -Xms512m -Xmx1024m - Denv=\$TOMCAT_ENV \
  -Dlogging_override=file://$CATALINA_HOME/logging_override.xml \
  -Doverride_file=$CATALINA_HOME/override.properties\"" | \
    tee -a $CATALINA_HOME/bin/setenv.sh \
    && chmod 755 $CATALINA_HOME/bin/setenv.sh

Now your setenv.sh looks like this:

CATALINA_OPTS="$CATALINA_OPTS -Xms512m -Xmx1024m -Denv$TOMCAT_ENV -Dlogging_override=file:///logging_override.xml -Doverride_file=/override.properties"

Upvotes: 5

Related Questions