Skip to content

Commit cb1b0b7

Browse files
Merge pull request #463 from DefangLabs/jordan/one-off-jobs
demonstrate one-off jobs with django and rails samples
2 parents 7041ab3 + 1b85521 commit cb1b0b7

File tree

7 files changed

+64
-7
lines changed

7 files changed

+64
-7
lines changed

samples/django-celery/app/command.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#!/bin/bash
22

3-
# Apply database migrations
4-
python manage.py migrate
5-
63
# Create superuser if not exists
74
python manage.py createsuperauto
85

samples/django-celery/compose.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ services:
1010
- POSTGRES_URL=postgres://postgres:${POSTGRES_PASSWORD}@database:5432/postgres
1111
depends_on:
1212
- database
13+
- migrate
1314
- broker
1415
ports:
1516
- mode: ingress
@@ -22,6 +23,19 @@ services:
2223
retries: 3
2324
start_period: 20s
2425

26+
migrate:
27+
restart: no
28+
build:
29+
context: ./app
30+
dockerfile: Dockerfile
31+
environment:
32+
- SECRET_KEY
33+
- REDIS_URL=redis://broker:6379/0
34+
- POSTGRES_URL=postgres://postgres:${POSTGRES_PASSWORD}@database:5432/postgres
35+
depends_on:
36+
- database
37+
command: python manage.py migrate
38+
2539
worker:
2640
restart: unless-stopped
2741
build:
@@ -37,6 +51,7 @@ services:
3751
depends_on:
3852
- database
3953
- broker
54+
- migrate
4055
command: celery -A django_celery worker --loglevel=info
4156

4257
database:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(docker compose:*)"
5+
],
6+
"deny": []
7+
}
8+
}

samples/django-channels-redis-postgres/app/Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Use a smaller base image
2-
FROM python:3.10-slim-buster
2+
FROM python:3.10-slim-bullseye
33

44
# Set environment variables
55
ENV PYTHONDONTWRITEBYTECODE=1
@@ -12,7 +12,8 @@ RUN apt-get update \
1212
&& apt-get install -y \
1313
gcc \
1414
python3-dev \
15-
libpq-dev
15+
libpq-dev \
16+
netcat-openbsd
1617

1718
# Install dependencies
1819
COPY requirements.txt /app/
@@ -32,4 +33,4 @@ RUN adduser --disabled-password --gecos '' django
3233
USER django
3334

3435
# Start server
35-
CMD ["sh", "-c", "python manage.py migrate && daphne -b 0.0.0.0 -p 8000 django_defang.asgi:application"]
36+
CMD ["sh", "-c", "daphne -b 0.0.0.0 -p 8000 django_defang.asgi:application"]

samples/django-channels-redis-postgres/compose.dev.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ services:
1313
depends_on:
1414
- redis-service
1515
- postgres-service
16-
command: sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
16+
- migrate
17+
command: sh -c "python manage.py runserver 0.0.0.0:8000"
1718

19+
migrate:
20+
extends:
21+
file: compose.yaml
22+
service: migrate
23+
environment:
24+
- POSTGRES_URL=postgres://djangouser:djangopassword@postgres-service:5432/djangodatabase
25+
1826
redis-service:
1927
extends:
2028
file: compose.yaml

samples/django-channels-redis-postgres/compose.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ services:
2222
depends_on:
2323
- redis-service
2424
- postgres-service
25+
- migrate
2526

2627
redis-service:
2728
image: redis:6.2
@@ -31,6 +32,18 @@ services:
3132
- mode: host
3233
target: 6379
3334

35+
migrate:
36+
restart: "no"
37+
build:
38+
context: ./app
39+
dockerfile: Dockerfile
40+
command: python manage.py migrate
41+
depends_on:
42+
- postgres-service
43+
environment:
44+
- POSTGRES_URL=postgres://djangouser:${POSTGRES_PASSWORD}@postgres-service:5432/djangodatabase?
45+
- SECRET_KEY
46+
3447
postgres-service:
3548
image: postgres:16.4
3649
# uncomment the following line in BYOC

samples/rails/compose.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ services:
2525
retries: 5
2626
depends_on:
2727
- db
28+
- migrate
29+
migrate:
30+
restart: "no"
31+
build:
32+
context: ./app
33+
dockerfile: Dockerfile
34+
env_file:
35+
- .env
36+
environment:
37+
POSTGRES_PASSWORD:
38+
command: ["bin/rails", "db:prepare"]
39+
depends_on:
40+
db:
41+
condition: service_healthy
42+
2843
db:
2944
restart: unless-stopped
3045
image: postgres:16

0 commit comments

Comments
 (0)