compose.yaml.j2 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. services:
  2. {{ service_name }}:
  3. image: docker.io/library/mariadb:{{ mariadb_version }}
  4. {#
  5. If not in swarm mode, apply restart policy and container_name,
  6. else swarm mode handles restarts via deploy.restart_policy
  7. #}
  8. {% if not swarm_enabled %}
  9. restart: {{ restart_policy }}
  10. container_name: {{ container_name }}
  11. {% endif %}
  12. {#
  13. Set container hostname
  14. #}
  15. hostname: {{ container_hostname }}
  16. {#
  17. Environment variables for MariaDB configuration
  18. - TZ: Timezone
  19. - MARIADB_ROOT_PASSWORD: Root password (set, random, or empty)
  20. - MARIADB_DATABASE: Default database name
  21. - MARIADB_USER: Default user name
  22. - MARIADB_PASSWORD: Default user password (from env or secret)
  23. #}
  24. environment:
  25. - TZ={{ container_timezone }}
  26. {% if mariadb_root_password_mode == 'set' %}
  27. - MARIADB_ROOT_PASSWORD={{ mariadb_root_password }}
  28. {% elif mariadb_root_password_mode == 'random' %}
  29. - MARIADB_RANDOM_ROOT_PASSWORD=true
  30. {% else %}
  31. - MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=true
  32. {% endif %}
  33. - MARIADB_DATABASE={{ mariadb_database }}
  34. - MARIADB_USER={{ mariadb_user }}
  35. {% if swarm_enabled %}
  36. - MARIADB_PASSWORD=/run/secrets/mariadb_password
  37. {% else %}
  38. - MARIADB_PASSWORD={{ mariadb_password }}
  39. {% endif %}
  40. {#
  41. Network configuration:
  42. - Databases typically use bridge networking for internal communication
  43. - Default to bridge network if not specified
  44. #}
  45. networks:
  46. {{ network_name }}:
  47. {#
  48. Port mappings (only expose if enabled):
  49. - MariaDB default port 3306
  50. Note: Swarm mode uses 'host' mode for port publishing
  51. #}
  52. {% if mariadb_port_enabled %}
  53. ports:
  54. {% if swarm_enabled %}
  55. - target: 3306
  56. published: {{ mariadb_port }}
  57. protocol: tcp
  58. mode: host
  59. {% else %}
  60. - "{{ mariadb_port }}:3306"
  61. {% endif %}
  62. {% endif %}
  63. {#
  64. Volume configuration for persistent data
  65. - When volume_mode is 'mount': bind mount from host path
  66. - When volume_mode is 'local', 'nfs', or empty: use docker-managed volumes
  67. #}
  68. volumes:
  69. {% if volume_mode == 'mount' %}
  70. - {{ volume_mount_path }}/{{ service_name }}:/var/lib/mysql:rw
  71. {% else %}
  72. - {{ service_name }}-data:/var/lib/mysql
  73. {% endif %}
  74. {#
  75. Deploy configuration for Swarm mode:
  76. - Single replica (MariaDB doesn't support multi-replica without replication setup)
  77. - For HA, use Galera Cluster or MariaDB replication
  78. - Uses Docker secrets for password management
  79. - Optional resource limits and reservations
  80. #}
  81. {% if swarm_enabled %}
  82. secrets:
  83. - mariadb_password
  84. deploy:
  85. {% if swarm_placement_mode == 'replicated' %}
  86. mode: replicated
  87. replicas: {{ swarm_replicas }}
  88. placement:
  89. constraints:
  90. - node.hostname == {{ swarm_placement_host }}
  91. {% else %}
  92. mode: global
  93. {% endif %}
  94. restart_policy:
  95. condition: on-failure
  96. {% if resources_enabled %}
  97. resources:
  98. limits:
  99. cpus: '{{ resources_cpu_limit }}'
  100. memory: {{ resources_memory_limit }}
  101. reservations:
  102. cpus: '{{ resources_cpu_reservation }}'
  103. memory: {{ resources_memory_reservation }}
  104. {% endif %}
  105. {% endif %}
  106. {#
  107. Volume definitions:
  108. - When volume_mode is 'local' (default): use docker-managed local volumes
  109. - When volume_mode is 'nfs': configure NFS-backed volumes
  110. - When volume_mode is 'mount': no volume definition needed (bind mounts used directly)
  111. #}
  112. {% if volume_mode == 'local' %}
  113. volumes:
  114. {{ service_name }}-data:
  115. driver: local
  116. {% elif volume_mode == 'nfs' %}
  117. volumes:
  118. {{ service_name }}-data:
  119. driver: local
  120. driver_opts:
  121. type: nfs
  122. o: addr={{ volume_nfs_server }},{{ volume_nfs_options }}
  123. device: ":{{ volume_nfs_path }}/{{ service_name }}"
  124. {% endif %}
  125. {#
  126. Network definitions:
  127. - Bridge network for service communication
  128. - Use overlay network in Swarm mode for multi-host communication
  129. #}
  130. networks:
  131. {{ network_name }}:
  132. {% if network_external %}
  133. external: true
  134. {% else %}
  135. {% if swarm_enabled %}
  136. driver: overlay
  137. attachable: true
  138. {% else %}
  139. driver: bridge
  140. {% endif %}
  141. {% endif %}
  142. {#
  143. Docker Swarm secrets (external secrets managed via docker secret create)
  144. #}
  145. {% if swarm_enabled %}
  146. secrets:
  147. mariadb_password:
  148. external: true
  149. {% endif %}