简体   繁体   English

使用 dockerfile 在多个 docker 实例之间共享 tomcat 配置?

[英]Share tomcat configuration across multiple docker instances using dockerfile?

I am running multiple Docker containers with bitnami/tomcat image and I want to share the configuration read only across all instances.我正在使用bitnami/tomcat映像运行多个 Docker 容器,并且我想在所有实例之间共享只读配置。

Every instance will contain this same configuration like specific libraries, datasources and so on.每个实例都将包含相同的配置,如特定的库、数据源等。 I already have proved and tested configuration that is being used in physical tomcat servers.我已经证明并测试了在物理 tomcat 服务器中使用的配置。 I see this configuration can be ready before any instance is run.我看到这个配置可以在运行任何实例之前准备好。

The application can be deployed to every instance (copied once actually since configuration is shared) and it can be done by copying the WAR file to host folder ${persistence_path}/tomcat/data that points to /bitnami folder in container, provided I use该应用程序可以部署到每个实例(实际上复制一次,因为配置是共享的)并且可以通过将 WAR 文件复制到主机文件夹${persistence_path}/tomcat/data来完成,该文件夹指向容器中的/bitnami文件夹,前提是我使用

-v ${persistence_path}:/bitnami

when running the instance . 运行实例时

The question问题

I am looking for an reproducible way of creating instances so it would be by script or dockerfile rather than commiting a new image , since it does not provide a reliable record of how the image was created.我正在寻找一种可重现的创建实例的方法,因此它将通过脚本或 dockerfile 而不是提交新图像,因为它不提供图像创建方式的可靠记录。 By using the former I avoid the chance of images getting corrupt or lost in production.通过使用前者,我避免了图像在生产中损坏或丢失的可能性。

Can I even share the configuration in multiple instances, as described?我什至可以在多个实例中共享配置吗?

Is this the best approach given I will not be modifying any tomcat settings after instances are started and if so I can re-run the instances with the new configuration as to change once and reflect in every instance?这是最好的方法,因为我不会在实例启动后修改任何 tomcat 设置,如果是这样,我可以使用新配置重新运行实例,以便更改一次并反映在每个实例中?

Can it be done with a dockerfile instead of a set of commands ?可以用 dockerfile 而不是一组命令来完成吗? I don't think -v is fully interchangeable with VOLUME .我认为-v不能与VOLUME完全互换。

You can mount a directory as read-only using the :ro option of -v您可以使用-v:ro选项将目录挂载为只读

docker run -v /your/host/dir:/bitnami:ro YOUR_IMAGE

You can create your own tomcat image starting from bitnami/tomcat including copying your artifacts and configuration at image build time.您可以从bitnami/tomcat开始创建自己的 tomcat 映像,包括在映像构建时复制工件和配置。 Then you can override them at runtime using the -v options.然后您可以在运行时使用-v选项覆盖它们。

If you want immutable containers (build time data locked in) you should build an image for every service you have.如果您想要不可变的容器(锁定构建时间数据),您应该为您拥有的每个服务构建一个映像。 Otherwise you can use the volume binding approach, as described in the image description .否则,您可以使用卷绑定方法,如图像描述中所述

The bitnami/tomcat image uses a simple mechanism in order to decide whether restore data from a volume or initialize a volume. bitnami/tomcat镜像使用一种简单的机制来决定是从卷恢复数据还是初始化卷。

On startup the container will check if a /bitnami/tomcat/.initialized file exists.启动时,容器将检查/bitnami/tomcat/.initialized文件是否存在。

  • If this file does not exist : The container will configure Tomcat and will move the /opt/bitnami/tomcat/{conf,data} folders to the volume (this happens at first run).如果此文件不存在:容器将配置 Tomcat 并将/opt/bitnami/tomcat/{conf,data}文件夹移动到卷(这发生在第一次运行时)。 Then, it will link the original folders to the ones in the volume.然后,它将原始文件夹链接到卷中的文件夹。

Tomcat folder after first run:首次运行后的Tomcat文件夹:

root@2346ab1f5c88:/# ls -la /opt/bitnami/tomcat/{conf,data}
lrwxrwxrwx 1 root root 20 Sep 28 13:49 /opt/bitnami/tomcat/conf -> /bitnami/tomcat/conf
lrwxrwxrwx 1 root root 20 Sep 28 13:49 /opt/bitnami/tomcat/data -> /bitnami/tomcat/data
  • If this file does exist : The container will only link the Tomcat conf and data folder back to the folders in the volume (and maybe fix permissions).如果此文件确实存在:容器只会将 Tomcat conf 和 data 文件夹链接回卷中的文件夹(并且可能修复权限)。

Therefore, the volume structure follows the following schema.因此,卷结构遵循以下架构。

root@2346ab1f5c88:/# tree -L 1 -a /bitnami/tomcat
/bitnami/tomcat
├── conf
├── data
└── .initialized

2 directories, 1 file

If you want to add your own configuration you should either override the default configuration in /bitnami/tomcat/conf after the Tomcat initialization takes place (at runtime) or override the default configuration in /opt/bitnami/tomcat/conf at build time.如果你想添加自己的配置,你要么覆盖缺省配置/bitnami/tomcat/conf Tomcat的初始化时(在运行时)或覆盖默认配置中/opt/bitnami/tomcat/conf在构建时。

This means you could either:这意味着您可以:

  • Extend from bitnami/tomcat and copy your configuration to /opt/bitnami/tomcat/conf .bitnami/tomcat扩展并将您的配置复制到/opt/bitnami/tomcat/conf
  • Mount your configuration to the container at runtime (to a known folder) and modify the app-entrypoint.sh file to override the /bitnami/tomcat/conf folder with it.在运行时将您的配置挂载到容器(到已知文件夹)并修改app-entrypoint.sh文件以使用它覆盖/bitnami/tomcat/conf文件夹。
  • Copy your configuration to the container at runtime (same as above, but copying with docker cp )在运行时将您的配置复制到容器(与上面相同,但使用docker cp复制)
  • Run the Tomcat container at least once, then override the conf folder in the local volume with your configuration.至少运行一次 Tomcat 容器,然后使用您的配置覆盖本地卷中的 conf 文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM