Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Tomcat parameters to Docker

I am new to Docker and I have a question that I can't seem to find the answer to. I am taking a Docker image (consol/tomcat-7.0) and wrote a Dockerfile that loads this image, copies my war files and copies a server.xml, with unique database connection details and default host, into a new image. If I am running many containers with this image on, what is the proper way to have each one use the same war files but connect to different databases and have different URLs in server.xml? I am currently building the image using the Dockerfile with different details each time I want to a new instance and this seems a waste.

So each time I want a new instance, I run 'build' using this Dockerfile:

FROM consol/tomcat-7.0:latest
MAINTAINER xxx
LABEL version="1.0"
EXPOSE 80 443
RUN mkdir /vhost/
COPY FILES /vhost/ /vhost/    # my war files - same on every instance
COPY FILES/server.xml /opt/tomcat/conf/ # my config file - different on each instance

And then run this new image.

What is the proper way of doing this?

like image 261
user2492861 Avatar asked Dec 01 '15 10:12

user2492861


People also ask

How do I pass environment variables to docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

What are environment variables in docker?

Environment variables can be specified as key-value pairs in the Docker pod specifications in Platform ASC. In addition to user-defined environment variables, the Docker Controller configures the following set of environment variables for every working container in the pod: Table 1.


2 Answers

The typical method for docker containers is passing via environment variables.

Expanding on a solution to pass the port via command line the server.xml needs to be modified so it takes in properties from JAVA_OPTS

For example in server.xml

<GlobalNamingResources>
    <Resource Name="jdbc/Addresses"
        auth="Container"
        type="javax.sql.Datasource"
        username="auser"
        password="Secret"
        driverClassName="com.mysql.jdbc.Driver"
        description="Global Address Database"
        url="${jdbc.url}" />
</GlobalNamingResources>

Then you can pass value of ${jdbc.url} from properties on the command line.

JAVA_OPTS="-Djdbc.url=jdbc:mysql:mysqlhost:3306/"

When running the docker image you use the -e flag to set this environment variable at run time

$ docker run -it -e "JAVA_OPTS=-Djdbc.url=jdbc:mysql:mysqlhost:3306/" --rm myjavadockerimage /opt/tomcat/bin/deploy-and-run.sh

Optionally also add a --add-host if you need to map mysqlhost to a specific ip address.

like image 161
jeedo Avatar answered Oct 15 '22 02:10

jeedo


There are at least two options I can think of:

  • If server.xml supports environment variables, you could pass database connection details to the container via --env or even --env-file. Note that this has certain security implications.
  • Another option would be to mount server.xml for a particular instance into the container via --volume.
like image 41
Evgeny Chernyavskiy Avatar answered Oct 15 '22 04:10

Evgeny Chernyavskiy