将Tomcat参数传递给Docker [英] Passing Tomcat parameters to Docker

查看:318
本文介绍了将Tomcat参数传递给Docker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手,我有一个问题,似乎找不到答案。
我正在拍摄一个Docker映像(consol / tomcat-7.0),并编写了一个Dockerfile来加载该映像,复制我的war文件并复制具有唯一数据库连接详细信息和默认主机的server.xml到一个新映像中。
如果我运行着许多带有该映像的容器,那么使每个容器使用相同的war文件但连接到不同的数据库并在server.xml中使用不同的URL的正确方法是什么?
我当前每次使用新实例时都使用Dockerfile构建具有不同详细信息的映像,这似乎很浪费。

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.

所以每次我想要一个新实例,我使用以下Dockerfile运行 build:

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

然后运行此新映像。

执行此操作的正确方法是什么?

What is the proper way of doing this?

推荐答案

docker容器的典型方法是通过环境变量传递。

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

通过命令行传递端口的解决方案 server.xml 需要进行修改,以使其接受来自 JAVA_OPTS

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

的属性,例如 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>

然后您可以传递 $ {jdbc.url}

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

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

运行docker映像时,使用 -e 标志在运行时设置此环境变量

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

(可选)还添加 -添加主机 (如果您需要映射 mysqlhost 到特定的IP地址。

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

这篇关于将Tomcat参数传递给Docker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆