compose.yaml.j2.final 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. services:
  2. {{ service_name }}:
  3. image: docker.io/library/postgres:17.6
  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 PostgreSQL configuration
  18. - POSTGRES_INITDB_ARGS: Database initialization arguments (e.g., --data-checksums)
  19. - POSTGRES_HOST_AUTH_METHOD: Authentication method (optional)
  20. - POSTGRES_USER: Database superuser name
  21. - POSTGRES_PASSWORD: Database password (from env or secret file)
  22. - POSTGRES_DB: Default database name
  23. - TZ: Timezone
  24. #}
  25. environment:
  26. - POSTGRES_INITDB_ARGS={{ postgres_initdb_args }}
  27. {% if postgres_host_auth_method %}
  28. - POSTGRES_HOST_AUTH_METHOD={{ postgres_host_auth_method }}
  29. {% endif %}
  30. - POSTGRES_USER={{ database_user }}
  31. {% if postgres_secrets_enabled %}
  32. - POSTGRES_PASSWORD_FILE=/run/secrets/postgres_password
  33. {% else %}
  34. - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
  35. {% endif %}
  36. - POSTGRES_DB={{ database_name }}
  37. - TZ={{ container_timezone }}
  38. {#
  39. Network configuration:
  40. - Databases typically use bridge networking for internal communication
  41. - Port exposure controlled separately for security
  42. #}
  43. {% if network_mode == 'bridge' or network_mode == '' %}
  44. networks:
  45. {{ network_name }}:
  46. {% endif %}
  47. {#
  48. Port mappings (only expose if needed):
  49. - PostgreSQL default port 5432
  50. Note: Swarm mode uses 'host' mode for port publishing
  51. #}
  52. {% if network_mode == 'bridge' or network_mode == '' %}
  53. ports:
  54. {% if swarm_enabled %}
  55. - target: 5432
  56. published: {{ database_port }}
  57. protocol: tcp
  58. mode: host
  59. {% else %}
  60. - "{{ database_port }}:5432"
  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 }}:/var/lib/postgresql/data:rw
  71. {% else %}
  72. - {{ service_name }}-data:/var/lib/postgresql/data
  73. {% endif %}
  74. {#
  75. Use Docker secrets for password management (Swarm or Compose with secrets enabled)
  76. #}
  77. {% if postgres_secrets_enabled %}
  78. secrets:
  79. - postgres_password
  80. {% endif %}
  81. {#
  82. Health check to monitor PostgreSQL availability
  83. #}
  84. healthcheck:
  85. test: ["CMD-SHELL", "pg_isready -U {{ database_user }}"]
  86. start_period: 30s
  87. interval: 10s
  88. timeout: 10s
  89. retries: 5
  90. {#
  91. Deploy configuration for Swarm mode:
  92. - Single replica (PostgreSQL doesn't support multi-replica without replication setup)
  93. - For HA, use external replication tools or PostgreSQL streaming replication
  94. #}
  95. {% if swarm_enabled %}
  96. deploy:
  97. mode: replicated
  98. replicas: 1
  99. restart_policy:
  100. condition: on-failure
  101. {% endif %}
  102. {#
  103. Docker secrets definition (when secrets are enabled)
  104. #}
  105. {% if postgres_secrets_enabled %}
  106. secrets:
  107. postgres_password:
  108. file: secret.postgres_password.txt
  109. {% endif %}
  110. {#
  111. Volume definitions:
  112. - When volume_mode is 'local' (default): use docker-managed local volumes
  113. - When volume_mode is 'nfs': configure NFS-backed volumes
  114. - When volume_mode is 'mount': no volume definition needed (bind mounts used directly)
  115. #}
  116. {% if volume_mode == 'local' %}
  117. volumes:
  118. {{ service_name }}-data:
  119. driver: local
  120. {% elif volume_mode == 'nfs' %}
  121. volumes:
  122. {{ service_name }}-data:
  123. driver: local
  124. driver_opts:
  125. type: nfs
  126. o: addr={{ volume_nfs_server }},{{ volume_nfs_options }}
  127. device: ":{{ volume_nfs_path }}"
  128. {% endif %}
  129. {#
  130. Network definitions:
  131. - Bridge network for service communication
  132. - Use overlay network in Swarm mode for multi-host communication
  133. #}
  134. {% if network_mode == 'bridge' or network_mode == '' %}
  135. networks:
  136. {{ network_name }}:
  137. {% if network_external %}
  138. external: true
  139. {% else %}
  140. {% if swarm_enabled %}
  141. driver: overlay
  142. attachable: true
  143. {% else %}
  144. driver: bridge
  145. {% endif %}
  146. {% endif %}
  147. {% endif %}