Skip to content

Commit

Permalink
merge feature/docker compose into main (#49)
Browse files Browse the repository at this point in the history
* removed version, added project name, removed container names

* docker compose temlate updates to support ansible docker deployment refactoring

- added project name
- removed container names
- volumes will only be defined in ansible host vars, so using a lot of jinja2 templating for volume definitions
  • Loading branch information
qtrinh2 authored Sep 16, 2024
1 parent 6c02f8d commit ad38234
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
5 changes: 2 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: "3.9"
name: lasfera

services:
app:
build: .
image: "rrchnm/lasfera"
container_name: "lasfera-app"
ports:
- "8000:8000"
environment:
Expand All @@ -28,7 +28,6 @@ services:
- .:/app
db:
image: postgres:16
container_name: "lasfera-db"
volumes:
- pg-data:/var/lib/postgresql/data/
environment:
Expand Down
54 changes: 40 additions & 14 deletions docker-compose.yml.j2
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# this is a Jinja2 template file used during the Ansible deployment
# environment specific configuration can be found in our Ansble scripts
---
name: {{ compose_stack_name }}

services:

{% set service = 'app' %}

app:
image: ghcr.io/{{ template.git.package.image_name }}:{{ template.git.package.tag }}
container_name: {{ template.name }}_app
ports:
- "{{ template.env.host_app_port }}:8000"
environment:
Expand All @@ -17,26 +21,48 @@ services:
- DB_NAME={{ template.env.db_name }}
- DB_USER={{ template.env.db_user }}
- DB_PASSWORD={{ template.env.db_pass }}
volumes:
- dj-data:/app/media
depends_on:
db:
condition: service_healthy
command: >
sh -c "
poetry run python manage.py migrate &&
poetry run python manage.py runserver 0.0.0.0:8000
"
{% if template.volumes is defined %}
{% set vols = (template.volumes | selectattr('service', 'eq', service)) %}
{% if vols is iterable and vols | length > 0 %}

volumes:
{% for vol in vols %}

- {{ vol.name }}:{{ vol.container_path }}
{% endfor %}
{% endif %}
{% endif %}

depends_on:
db:
condition: service_healthy

{% set service = 'db' %}

db:
image: postgres:16
container_name: {{ template.name }}_db
volumes:
- pg-data:/var/lib/postgresql/data/
environment:
- POSTGRES_DB={{ template.env.db_name }}
- POSTGRES_USER={{ template.env.db_user }}
- POSTGRES_PASSWORD={{ template.env.db_pass }}
- POSTGRES_HOST=db
{% if template.volumes is defined %}
{% set vols = (template.volumes | selectattr('service', 'eq', service)) %}
{% if vols is iterable and vols | length > 0 %}

volumes:
{% for vol in vols %}

- {{ vol.name }}:{{ vol.container_path }}
{% endfor %}
{% endif %}
{% endif %}

healthcheck:
test: ["CMD-SHELL", "pg_isready -U lasfera"]
interval: 5s
Expand All @@ -45,9 +71,9 @@ services:

# external volumes managed and defined by ansible
volumes:
dj-data:
name: "{{ template.name }}_app-vol"
external: true
pg-data:
name: "{{ template.name }}_db-vol"
{% for vol in template.volumes %}

{{ vol.name }}:
name: "{{ compose_stack_name }}--{{ vol.name }}"
external: true
{% endfor %}

0 comments on commit ad38234

Please sign in to comment.