itsbehind itsbehind - 3 months ago 48
Bash Question

Alpine linux in docker container ignoring shell script arguments

I'm trying to create a docker image which sets a custom tomcat port (I know you can set an external port with the docker flag "-p 8888:8080" but for my use case I want to change the internal port as well).

When I try to start catalina.sh the run argument is being ignored for some reason.

Dockerfile:

# Tomcat 8 alpine dockerfile copied here (URL below)... minus the CMD line at the end
# https://github.com/docker-library/tomcat/blob/5f1abae99c0b1ebbd4f020bc4b5696619d948cfd/8.0/jre8-alpine/Dockerfile

ADD server.xml $CATALINA_HOME/conf/server.xml
ADD start-tomcat.sh /start-tomcat.sh
RUN chmod +x /start-tomcat.sh
ENTRYPOINT ["/bin/sh","/start-tomcat.sh"]


The tomcat file, server.xml, is the same as the default except for the line:

<Connector port="${port.http.nonssl}" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />


start-tomcat.sh:

#!/bin/sh
export JAVA_OPTS=-Dport.http.nonssl=${PORT}
catalina.sh run


The image builds successfully, but when I run with

docker run -p 8888:8888 -e PORT=8888 customtomcat


I just get a list of catalina.sh commands as if I didn't give it an argument. I've also tried

/usr/local/tomcat/bin/catalina.sh run

sh -c "catalina.sh run"

sh -c "/usr/local/tomcat/bin/catalina.sh run"

cd /usr/local/tomcat/bin
./catalina.sh run


I'm pretty sure I'm missing something simple here. I'd guess it has something to do with the syntax, but maybe it has something to do with docker or alpine that I'm not aware of. This is my first time using alpine linux.

---Edit 1---

To explain my use case... I'm setting PORT after the docker image is created because it's being set by an apache mesos task. For my purposes I need to run the docker container (from marathon) in host mode, not bridged mode.

---Edit 2---

I modified things to only focus on my main issue. The docker file now only has the following appended to the end:

ADD start-tomcat.sh /start-tomcat.sh
RUN chmod +x /start-tomcat.sh
ENTRYPOINT ["/bin/sh","/start-tomcat.sh"]


And start-tomcat.sh:

#!/bin/bash
catalina.sh run


Still no luck.

Answer

Update: for the "catalina.sh run" to fail with an invalid option, first check for linefeeds from a Windows system. They'll cause errors when the shell script is read in a Linux environment.


Looking at catalina.sh, I believe you want CATALINA_OPTS, not JAVA_OPTS:

# Control Script for the CATALINA Server
#
# Environment Variable Prerequisites
#
#   Do not set the variables in this script. Instead put them into a script
#   setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
#
#   CATALINA_HOME   May point at your Catalina "build" directory.
#
#   CATALINA_BASE   (Optional) Base directory for resolving dynamic portions
#                   of a Catalina installation.  If not present, resolves to
#                   the same directory that CATALINA_HOME points to.
#
#   CATALINA_OUT    (Optional) Full path to a file where stdout and stderr
#                   will be redirected.
#                   Default is $CATALINA_BASE/logs/catalina.out
#
#   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
#                   "run" or "debug" command is executed.
#                   Include here and not in JAVA_OPTS all options, that should
#                   only be used by Tomcat itself, not by the stop process,
#                   the version command etc.
#                   Examples are heap size, GC logging, JMX ports etc.
#
#   CATALINA_TMPDIR (Optional) Directory path location of temporary directory
#                   the JVM should use (java.io.tmpdir).  Defaults to
#                   $CATALINA_BASE/temp.
#
#   JAVA_HOME       Must point at your Java Development Kit installation.
#                   Required to run the with the "debug" argument.
#
#   JRE_HOME        Must point at your Java Runtime installation.
#                   Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME
#                   are both set, JRE_HOME is used.
#
#   JAVA_OPTS       (Optional) Java runtime options used when any command
#                   is executed.
#                   Include here and not in CATALINA_OPTS all options, that
#                   should be used by Tomcat and also by the stop process,
#                   the version command etc.
#                   Most options should go into CATALINA_OPTS.
Comments