Common docker compose items as yaml fragments

huangapple go评论107阅读模式
英文:

Common docker compose items as yaml fragments

问题

I want to clean up my docker-compose.yml with yaml fragments.

  1. x-common: &common
  2. restart: unless-stopped
  3. security_opt:
  4. - no-new-privileges:true
  5. volumes:
  6. - /etc/localtime:/etc/localtime:ro
  7. services:
  8. myapp1:
  9. <<: *common
  10. image: app1
  11. volumes:
  12. - ./foo:/foo
  13. myapp2:
  14. <<: *common
  15. image: app2
  16. volumes:
  17. - ./bar:/bar

When I run docker compose config to show the final result, the common restart and security_opt items are applied. But the common volumes item is not - I assume this is because the services are overwriting it.

Is there a way to do this?

英文:

I want to clean up my docker-compose.yml with yaml fragments.

  1. x-common: &amp;common
  2. restart: unless-stopped
  3. security_opt:
  4. - no-new-privileges:true
  5. volumes:
  6. - /etc/localtime:/etc/localtime:ro
  7. services:
  8. myapp1:
  9. &lt;&lt;: *common
  10. image: app1
  11. volumes:
  12. - ./foo:/foo
  13. myapp2:
  14. &lt;&lt;: *common
  15. image: app2
  16. volumes:
  17. - ./bar:/bar

When I run docker compose config to show the final result, the common restart and security_opt items are applied. But the common volumes item is not - I assume this is because the services are overwriting it.

Is there a way to do this?

答案1

得分: 1

这是您提供的代码示例的中文翻译:

  1. 这个问题没有一个很好的解决方式。复杂的因素在于`volumes`是一个*列表*,而不是一个字典,而YAML没有合并列表的语法。
  2. 你可以尝试像这样做:
  3. ```yaml
  4. x-common: &common
  5. restart: unless-stopped
  6. security_opt:
  7. - no-new-privileges:true
  8. x-common-volume: &volume /etc/localtime:/etc/localtime:ro
  9. services:
  10. myapp1:
  11. <<: *common
  12. image: app1
  13. volumes:
  14. - *volume
  15. - ./foo:/foo
  16. myapp2:
  17. <<: *common
  18. image: app2
  19. volumes:
  20. - *volume
  21. - ./bar:/bar

这可以让你完成一半的工作 - 你需要记得将共同的卷添加到每个服务中,但你不必每次都重新输入完整的条目。


根据这个问题的严重程度,你可以考虑使用某种模板机制生成你的docker-compose.yml文件 - 例如,使用jsonnet

  1. local Common = {
  2. restart: 'unless-stopped',
  3. security_opt: ['no-new-privileges:true'],
  4. volumes: [
  5. '/etc/localtime:/etc/localtime:ro',
  6. ]
  7. };
  8. {
  9. services: {
  10. myapp1: Common {
  11. image: 'app1',
  12. volumes+: [
  13. './foo:/foo',
  14. ],
  15. },
  16. myapp2: Common {
  17. image: 'app2',
  18. volumes+: [
  19. './bar:/bar',
  20. ],
  21. },
  22. },
  23. }

通过运行jsonnet docker-compose.jsonnet > docker-compose.yaml生成你的docker-compose.yml文件,你就可以得到你想要的内容。

英文:

There's not really a great way to do this. The complicating factor is that volumes is a list, rather than a dictionary, and YAML doesn't have a syntax for merging lists.

You could do something like this:

  1. x-common: &amp;common
  2. restart: unless-stopped
  3. security_opt:
  4. - no-new-privileges:true
  5. x-common-volume: &amp;volume /etc/localtime:/etc/localtime:ro
  6. services:
  7. myapp1:
  8. &lt;&lt;: *common
  9. image: app1
  10. volumes:
  11. - *volume
  12. - ./foo:/foo
  13. myapp2:
  14. &lt;&lt;: *common
  15. image: app2
  16. volumes:
  17. - *volume
  18. - ./bar:/bar

Which gets you halfway there -- you need to remember to add the common volume to each service, but you don't have to retype the full entry each time.


Depending on how much this bothers you, you could look into generating your docker-compose.yml files with some sort of templating mechanism -- e.g., using jsonnet:

  1. local Common = {
  2. restart: &#39;unless-stopped&#39;,
  3. security_opt: [&#39;no-new-privileges:true&#39;],
  4. volumes: [
  5. &#39;/etc/localtime:/etc/localtime:ro&#39;,
  6. ]
  7. };
  8. {
  9. services: {
  10. myapp1: Common {
  11. image: &#39;app1&#39;,
  12. volumes+: [
  13. &#39;./foo:/foo&#39;,
  14. ],
  15. },
  16. myapp2: Common {
  17. image: &#39;app2&#39;,
  18. volumes+: [
  19. &#39;./bar:/bar&#39;,
  20. ],
  21. },
  22. },
  23. }

Generating your docker-compose.yml by running jsonnet docker-compose.jsonnet &gt; docker-compose.yaml and you've got what you want.

huangapple
  • 本文由 发表于 2023年6月12日 21:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定