I want to share code content across several containers using docker-compose volume directive

眉间皱痕 提交于 2019-12-11 07:30:48

问题


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

  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

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

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

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.

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.

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

Thanks


回答1:


There are a few ways to handle this:

  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.

  2. 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/..

  3. 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.

  4. 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.

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.



来源:https://stackoverflow.com/questions/44251094/i-want-to-share-code-content-across-several-containers-using-docker-compose-volu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!