多个 Docker 容器,相同的镜像,不同的配置 [英] Multiple Docker containers, same image, different config

查看:217
本文介绍了多个 Docker 容器,相同的镜像,不同的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Docker 完全陌生,所以感谢您的耐心等待.

I'm totally new to Docker so I appreciate your patience.

我正在寻找一种使用相同映像部署多个容器的方法,但是我需要向每个容器传递不同的配置(文件)?

I'm looking for a way to deploy multiple containers with the same image, however I need to pass in a different config (file) to each?

现在,我的理解是,一旦你构建了一个镜像,这就是被部署的东西,但对我来说,问题是我没有看到构建同一应用程序的多个镜像的意义,因为它只是配置不同的容器.

Right now, my understanding is that once you build an image, that's what gets deployed, but the problem for me is that I don't see the point in building multiple images of the same application when it's only the config that is different between the containers.

如果这是常态,那么我将不得不处理它,但是如果有另一种方式,那么请让我摆脱痛苦!:)

If this is the norm, then I'll have to deal with it however if there's another way then please put me out of my misery! :)

谢谢!

推荐答案

我认为查看易于理解的示例可以给你最好的画面.

I think looking at examples which are easy to understand could give you the best picture.

你想要做的是完全有效的,一个镜像应该是你需要运行的任何东西,没有配置.

What you want to do is perfectly valid, an image should be anything you need to run, without the configuration.

要生成配置,您可以:

在容器启动期间使用卷并挂载文件 docker run -v my.ini:/etc/mysql/my.ini percona(与 docker-compose 类似).请注意,您可以根据需要多次重复此操作,因此将多个配置装入容器(即图像的运行时版本).您将在运行容器之前在主机上创建这些配置,并且需要将这些文件与容器一起传送,这是这种方法的缺点(可移植性)

use volumes and mount the file during container start docker run -v my.ini:/etc/mysql/my.ini percona (and similar with docker-compose). Be aware, you can repeat this as often as you like, so mount several configs into your container (so the runtime-version of the image). You will create those configs on the host before running the container and need to ship those files with the container, which is the downside of this approach (portability)

大多数高级 docker 镜像确实提供了一个复杂的所谓入口点,它使用您在启动镜像时传递的 ENV 变量,为您创建配置,例如 https://github.com/docker-library/percona/blob/master/5.7/docker-entrypoint.sh

Most of the advanced docker images do provide a complex so called entry-point which consumes ENV variables you pass when starting the image, to create the configuration(s) for you, like https://github.com/docker-library/percona/blob/master/5.7/docker-entrypoint.sh

所以当你运行这个镜像时,你可以执行 docker run -e MYSQL_DATABASE=myapp percona 这将启动 percona 并为你创建数据库 percona.这一切都是由

so when you run this image, you can do docker run -e MYSQL_DATABASE=myapp percona and this will start percona and create the database percona for you. This is all done by

  1. 在此处添加入口点脚本 https://github.com/docker-library/percona/blob/master/5.7/Dockerfile#L65
  2. 不要忘记在镜像构建期间复制脚本 https://github.com/docker-library/percona/blob/master/5.7/Dockerfile#L63
  3. 然后在图像启动期间,您的 ENV 变量将导致触发:https://github.com/docker-library/percona/blob/master/5.7/docker-entrypoint.sh#L91

当然,你可以用它做任何你喜欢的事情.例如,这配置了一个通用的 portus 图像:https://github.com/EugenMayer/docker-rancher-extra-catalogs/blob/master/templates/registry-slim/11/docker-compose.yml它有这个入口点 https://github.com/EugenMayer/docker-image-portus/blob/master/build/startup.sh

Of course, you can do whatever you like with this. E.g this configures a general portus image: https://github.com/EugenMayer/docker-rancher-extra-catalogs/blob/master/templates/registry-slim/11/docker-compose.yml which has this entrypoint https://github.com/EugenMayer/docker-image-portus/blob/master/build/startup.sh

所以你看,切入点策略很常见也很强大,我想只要有可能就走这条路.

So you see, the entry-point strategy is very common and very powerful and i would suppose to go this route whenever you can.

也许是为了完整性",图像派生策略,所以你有一个名为myapp"的基础图像,并为安装 X 创建一个新图像

Maybe for "completeness", the image-derive strategy, so you have you base image called "myapp" and for the installation X you create a new image

from myapp
COPY my.ini /etc/mysql/my.ini
COPY application.yml /var/app/config/application.yml

将此图像称为 myapp:x - 与此相比,明显的问题是,与 a) 相比,它的便携性要高得多.

And call this image myapp:x - the obvious issue with this is, you end up having a lot of images, on the other side, compared to a) its much more portable.

希望有帮助

这篇关于多个 Docker 容器,相同的镜像,不同的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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