Docker-compose推荐使用数据容器的方法 [英] Docker-compose recommended way to use data containers

查看:166
本文介绍了Docker-compose推荐使用数据容器的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个我想用来提供静态webapp文件的nginx容器。这些webapp文件存在于名为webapp的单独容器中。

In my application I have an nginx container that I want to use to serve static webapp files. These webapp files exist in a separate container called "webapp".

将webapp容器装入nginx容器的推荐方法是什么?我目前的方法看起来像这样:

What's the recommended way of mounting the webapp container into the nginx container? My current approach looks like this:

version: '2'
services:
  nginx:
    volumes_from:
      - webapp


  webapp:
      image: someimageurl                                  

在webapp的Docker文件中,我公开了与nginx容器正在运行的路径相同的卷。

And in the Dockerfile for webapp, I expose a volume at the same path that the nginx container is serving.

,但问题是在docker-compose v3中,volumes_from正在被删除,并用命名卷替换。所以我的新配置将如下所示:

This works great, but the problem is that in docker-compose v3, "volumes_from" is being removed and replaced with named volumes. So my new config would look like:

version: '3'

volumes: 
   static-webapp:

services:
  nginx:
    volumes:
      - static-webapp:/opt/webapp


  webapp:
      image: someimageurl
      volumes:
        - static-webapp:/opt/webapp                          

这个问题是,webapp容器中的数据只会被复制到static-webapp卷中。在更改内容并重新运行docker-compose之后,卷中的内容不变。我明白这是设计,以至不覆盖持久性数据。

The problem with this is that the data from the webapp container will only be copied into the 'static-webapp' volume once. After changing the content and re-running docker-compose up, the content in the volume is unchanged. I understand that this is by design, as to not overwrite persistent data.

我的问题是:我应该如何使用v3的docker-compose做这个?我本来想要一个未命名的卷,我可以挂载到多个容器。

My question is: How should I be doing this with v3 of docker-compose? I essentially want an unnamed volume that I can mount into multiple containers.

我想到的解决方案是:

1。)捆绑nginx容器中的静态内容。我不喜欢这样,因为我不能保持要求分开。

1.) Bundle the static content in the nginx container. I don't like this because then I can't keep the requirements separate.

2。)我可以在每个docker-compose运行后手动删除static-webapp卷。这也似乎是非理想的。

2.) I could manually remove the 'static-webapp' volume after each docker-compose run. This also seems non-ideal.

推荐答案

推荐的方法是删除容器之间的依赖关系,因为这些依赖关系在逻辑上不适用于集群可能在不能共享文件的不同主机上运行的集群(这就是为什么卷从版本3格式中删除)。数据容器在引用命名卷之后已经被弃用,尽管有一些过时的文档表示不同。

The recommended way is to remove dependencies between containers since these dependencies don't logically apply to swarm where containers may be running on different hosts where they can't share files (this is why volumes-from was removed from the version 3 format). Data containers had already been deprecated after the introduction of of named volumes, despite some out of date documentation suggesting otherwise.

我可以想到的选项在我的顶部头包括:

The options that I can think of off the top of my head include:


  1. 使用版本2撰写文件,不使用群组模式。如果您不使用群集模式,则无需升级到版本3格式。

  1. Using a version 2 compose file and not using swarm mode. There's no need to upgrade to the version 3 format if you aren't using this with swarm mode.

直接将数据添加到每个容器。

Directly add your data to each container.

搜索应用程序以分开不同的组件。我不清楚nginx和另一个容器中的应用程序代码的用例,似乎应该只是一个或另一个。

Rearchitecture your app to split apart components differently. I'm not clear on the use case of application code in both nginx and another container, seems like it should only be one or the other.

创建一个具有所有应用数据的数据同步实用程序容器在整个群集中全局运行,并在启动时将更改推送到每个群集节点上的卷。这也可以调查像git repo这样的东西,并且随时更新本地卷的任何更改。然后,您的其他容器指向此本地命名卷。

Create a data sync utility container that has all of the app data, runs globally across the swarm, and pushes out changes to volumes on each of the swarm nodes on startup. This could also poll something like a git repo and constantly update the local volume on any changes. Your other containers then point to this local named volume.

将容器指向远程卷(如NFS),因此只需要将数据更新一次任何主机,并消除任何主机不同步,但也会在任何读取中增加额外的延迟。

Point the containers to a remote volume, like NFS, so you only need to update the data once from any host and eliminate any hosts being out of sync, but also adding extra latency in any reads.

这篇关于Docker-compose推荐使用数据容器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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