what about the ability to configure the port via an environment variable?
f1-outsourcing opened this issue · 3 comments
I know you can do this with the docker run (i think). But for other orchestration platforms like mesos it could be handy.
The port publishing feature is done through the Docker engine, not the container's initialization.
So even if you mount Docker's socket and make a script to tell Docker to publish the port; environment variables wouldn't be able to affect its own running container since it's only "when you create a container"
I am currently doing it like this
Dockerfile
FROM tomcat:8-slim
WORKDIR ${CATALINA_HOME}
ENV TOMCAT_USER="nobody" \
TOMCAT_UID=99 \
TOMCAT_HTTP_PORT="8080" \
TOMCAT_HTTPS_PORT="8443" \
OPENFIRE_SERVER="192.168.122.250"
# clean up examples
RUN rm -rf ${CATALINA_HOME}/webapps/*
# config changes
RUN groupadd $TOMCAT_USER -g $TOMCAT_UID \
&& chmod g+w ${CATALINA_HOME}/temp ${CATALINA_HOME}/webapps \
&& chgrp ${TOMCAT_USER} ${CATALINA_HOME}/temp ${CATALINA_HOME}/webapps \
&& sed -i '/<Connector port="8080"/a \ Server=" "' ${CATALINA_HOME}/conf/server.xml \
&& sed -i 's/"8080"/"${envHttpPort}"/' ${CATALINA_HOME}/conf/server.xml \
&& sed -i 's/"8443"/"${envHttpsPort}"/' ${CATALINA_HOME}/conf/server.xml \
&& sed -i 's/shutdown="SHUTDOWN"/shutdown="${envShutdown}"/g' ${CATALINA_HOME}/conf/server.xml
COPY entrypoint.sh /sbin/entrypoint.sh
EXPOSE ${TOMCAT_HTTP_PORT}/tcp
CMD ["/sbin/entrypoint.sh"]
And in entrypoint.sh
export TOMCAT_SHUTDOWN=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 18 | head -n 1)
export JAVA_OPTS="$JAVA_OPTS -DenvHttpPort=$TOMCAT_HTTP_PORT -DenvHttpsPort=$TOMCAT_HTTPS_PORT -DenvShutdown=$TOMCAT_SHUTDOWN
As I wrote, there is more than just docker.
As you've pointed out, it requires modification of XML configuration to change the port -- we currently do not do anything to the default XML configuration provided by Tomcat upstream, and we don't have any plans to do so.
I would recommend petitioning Tomcat upstream to add support for specifying the port to listen on via an environment variable instead, or using exactly the type of workaround you've provided here.