Which is better to create docker-compose.yml file

随声附和 提交于 2020-01-16 19:33:05

问题


I have two web applications, all of them are visited through their domain names, nginx as their proxy, there are two methods to create docker-compose.yml file

  1. create only one docker-compose.yml file with all applications's config on it, like following
webapp1:
  image:nodejs
  command: node app.js
webapp2:
  image:nodejs
  command: node app.js
nginx:
  image:nginx
  ports:
    - "80:80"
  depends_on:
    - webapp1
    - webapp2
  1. create three docker-compose.yml files, like following

webapp1

webapp1:
  image:nodejs
  command: node app.js

webapp2

webapp2:
  image:nodejs
  command: node app.js

nginx

nginx:
  image:nginx
  ports:
    - "80:80"

Which method is better? I prefer the second method, because web applications will be changed often, if I use the first method, other applications might be affected, the second method will not have this problem.

If I use the second method, how to link nginx with webapp1, webapp2, depends_on can't be used here. Any ideas?

updated: There's no relationship between webapp1 and webapp2, I just want to run them on one server.


回答1:


You should prefer to write one docker-compose.yml file for an application and all of its dependencies.

As you note, the most straightforward reason to prefer this is that you can run docker-compose up once and get the entire application stack. With separate Compose files, not only do you need to run that command once per service but the wiring to get them to talk to each other is tricky.

I would not especially worry about parts in a larger Compose file affecting other container stacks. Standard microservices style is that different services shouldn't share storage, and in Docker it's straightforward to run a database per service, so every Compose file should be able to be completely self-contained, and restarting (some) services in one file shouldn't affect anything anywhere else.

Finally, as far as redeploying containers on change goes, you have the advantages that (a) re-running docker build is very quick if nothing has changed and still pretty fast if your upstream dependencies haven't changed (e.g., your package.json is unchanged), and (b) re-running docker-compose up -d will only affect containers that change in some way. If you for example

docker-compose up --build -d

that won't affect things like your database containers that are unchanged, and if you haven't changed some particular application component, it will still get rebuilt but you'll wind up with the same image out.



来源:https://stackoverflow.com/questions/59495219/which-is-better-to-create-docker-compose-yml-file

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