本文介绍了我想使用docker-compose volume指令跨多个容器共享代码内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要容器化的PHP应用程序。我正在设置以下内容:

I have a PHP application that I need to containerize. I am setting up the following:


  1. 用于清漆的容器

  2. 用于nginx的容器

  3. 用于php-fpm的容器

  4. 用于cron的容器

  5. 用于工具的容器

  6. 带有PHP代码的容器

  1. container for varnish
  2. container for nginx
  3. container for php-fpm
  4. container for cron
  5. container for tooling
  6. container with PHP code baked into

容器2,3,4,5都需要访问相同的PHP应用程序代码库将其放入容器6中。

Container 2,3,4,5 all need to have access to the same PHP application codebase that is baked into container 6.

我希望将其设置为仅更改代码库容器的version标记即可恢复到该应用程序的先前版本。

I would like to set this up to be able to revert to previous releases for the application by just changing the version tag of the codebase container.

我当前的作曲家文件类似于->

My current composer file is something like ->

version "3"
services:
  web:
   image:nginx
   links:
     - phpfpm
   volumes:
     - code:/var/www/html

 phpfpm:
  image:php-fpm
  links:
    - db
  volumes:
    - code:/var/www/html

 code:
   build:
   context: ./
   dockerfile: Dockerfile
   volumes:
     - code:/var/www/html

volumes:
  code:
    driver: local

此时已创建代码量。来自容器代码的php代码副本已复制到该卷中。

At this point the code volume was created. Copy of the php code from container code was copied to the volume.

这很好,因为所有新更改都将保留在该卷中,尽管当我拉出一个新版本的我的卷将无法更新。

This is good as all new changes will be persisted to the volume although when I pull a new version of the codebase my volume will not get updated.

我想要实现的是,我的nginx和cron以及工具开发者都看到了最新版本的代码库容器的内容,并且我也希望能够使用容器6中的php代码来运行多个容器调用。

What I would like to achieve is that my nginx and cron and tooling continer all see the latest version of the codebase container's content and as well I want to be able to run several one of containers calls using that php code that is in container 6.

我该怎么做才能使用v3语法?

How do I need to do to go about that using v3 syntax?

谢谢

推荐答案

有几种方法可以处理以下问题:

There are a few ways to handle this:


  1. 最有效的方法是将代码移入每个图像中,这可能是更好的设计,可能会将您的体系结构更改为特定代码段仅在一个图像中显示,而不是在每个图像中包含所有代码。共享代码会产生紧密的依赖关系,这在很大程度上与微服务设计背道而驰。

  1. The most work but the better design is to move the code into each image, possibly changing your architecture to have specific pieces of the code in only one image, rather than having all the pieces in every image. Having the code shared creates a tight dependency that is very much against the micro-services design.

继续使用命名的卷,但是在启动时启动它一个密钥容器。不太理想(请参见上文),但变化最小。要对其进行初始化,您可以将代码添加到图片的一个目录中,例如/ var / www / html-cache,将卷安装在/ var / www / html中,入口点的第一步将是 cp -a / var / www / html-cache /。 / var / www / html/。

Continue to use the named volume, but initialize it on startup of one key container. Less ideal (see above) but would work with the least change. To initialize it, you'd add the code to your image in one directory, e.g. /var/www/html-cache, mount your volume in /var/www/html, and the first step of the entrypoint would be to cp -a /var/www/html-cache/. /var/www/html/..

创建代码同步映像,以根据版本控件的需要更新音量。这只是卷位置上的 git pull

Create a code sync image that updates the volume on demand from your version control. This would just be a git pull on the volume location.

使用指向卷的卷Docker外部的代码,例如一个主机目录甚至是nfs挂载,用于管理Docker外部的代码同步。

Use a volume that points to the code outside of Docker, e.g. a host directory or even an nfs mount, that manages the code synchronization outside of Docker. This is commonly done for development, but I wouldn't recommend it for production.

docker-Version 3的版本通常用于开发,但我不建议将其用于生产。对我来说compose.yml现在是swarm模式的同义词。如果尝试以群集模式执行此操作,则需要在可能运行容器的每个主机上运行卷同步,或者指向共享目录中的外部卷(例如nfs)。如果不升级群集模式,则无需立即切换到版本3。

Version 3 of the docker-compose.yml to me is synonymous with swarm mode right now. If you try to do this in swarm mode, then you either need to run the volume synchronization on every host where a container may run, or point to an external volume in a shared directory (e.g. nfs). Without upgrading the swarm mode, there's no immediate need to switch to version 3.

这篇关于我想使用docker-compose volume指令跨多个容器共享代码内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:04