Skip to content

Commit 5e0b2c6

Browse files
diegocastrumauvipy
andauthored
Add Docker, Docker Compose and use dockerized PostgreSQL for local testing (#476)
* fix: correct heading formatting in injecting_metadata.rst * feat(settings): update database configuration to use environment variables. Relates #475 * feat(tox): add passenv for DB_POSTGRES_* environment variables Relates #475 * feat(docker): Integrate Docker and Docker Compose for testing. - Introduced a `Dockerfile` to enable containerized testing environment for Django Celery Results. - Added a `compose.yml` file to orchestrate services, including a PostgreSQL container for database testing. - Enhanced `.gitignore` to exclude .env and temporary files. - Included a small update of the `README.rst` with instructions for running tests using Docker and Docker Compose. - Added `.dockerignore` to optimize Docker builds. Closes #475 * Update compose.yml * Update compose.yml * Update docker/Dockerfile * chore(Dockerfile): fix label formatting and update documentation link in Dockerfile * Update compose.yml --------- Co-authored-by: Asif Saif Uddin <[email protected]>
1 parent 9571584 commit 5e0b2c6

File tree

9 files changed

+225
-14
lines changed

9 files changed

+225
-14
lines changed

.dockerignore

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Version control
2+
.git
3+
.gitignore
4+
.gitattributes
5+
6+
# Python
7+
__pycache__/
8+
*.py[cod]
9+
*$py.class
10+
*.so
11+
.Python
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
*.egg-info/
25+
*.egg
26+
27+
# Unit test / coverage reports
28+
htmlcov/
29+
.tox/
30+
.coverage
31+
.coverage.*
32+
.cache
33+
coverage.xml
34+
*.cover
35+
.pytest_cache/
36+
.hypothesis/
37+
38+
# Django
39+
*.log
40+
local_settings.py
41+
db.sqlite3
42+
db.sqlite3-journal
43+
44+
# Docker
45+
docker-compose*.yml
46+
Dockerfile
47+
.docker
48+
docker/
49+
50+
# IDE
51+
.idea/
52+
.vscode/
53+
*.swp
54+
*.swo
55+
*~
56+
57+
# Environment
58+
.env
59+
.venv
60+
env/
61+
venv/
62+
ENV/
63+
64+
# Documentation
65+
docs/
66+
*.rst
67+
*.md
68+
LICENSE
69+
AUTHORS
70+
71+
# OS specific
72+
.DS_Store
73+
Thumbs.db

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ cover/
2929
.cache/
3030
htmlcov/
3131
coverage.xml
32+
.env
33+
*.ignore

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Darjus Loktevic <[email protected]>
2525
David Fischer <[email protected]>
2626
David Ziegler <[email protected]>
2727
Diego Andres Sanabria Martin <[email protected]>
28+
Diego Castro <[email protected]>
2829
Dmitriy Krasilnikov <[email protected]>
2930
Donald Stufft <[email protected]>
3031
Eldon Stegall

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ pip command::
7373
$ pip install https://github.com/celery/django-celery-results/zipball/master#egg=django-celery-results
7474

7575

76+
Running with Docker Compose
77+
===========================
78+
79+
To run the project using Docker Compose, ensure you have Docker and Docker Compose installed. Then, execute the following command in the project root directory::
80+
81+
$ docker-compose up --build
82+
83+
This will start a containerized PostgreSQL database and run the tests using `tox`.
84+
85+
86+
7687
Issues with mysql
7788
-----------------
7889

compose.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: django-celeryresult
2+
3+
volumes:
4+
postgres_data:
5+
6+
networks:
7+
celery:
8+
9+
services:
10+
postgres:
11+
image: postgres:${POSTGRES_VERSION:-15}
12+
container_name: djangoceleryresults-postgres
13+
cpus: 0.25
14+
mem_limit: 256m
15+
mem_reservation: 128m
16+
environment:
17+
POSTGRES_USER: ${DB_POSTGRES_USER:-postgres}
18+
POSTGRES_PASSWORD: ${DB_POSTGRES_PASSWORD:-postgres}
19+
POSTGRES_DB: ${DB_POSTGRES_DATABASE:-postgres}
20+
volumes:
21+
- postgres_data:/var/lib/postgresql/data
22+
networks:
23+
jazzband:
24+
aliases:
25+
- dcr-postgres
26+
27+
app:
28+
build:
29+
context: .
30+
dockerfile: ./docker/Dockerfile
31+
container_name: djangoceleryresults-app
32+
cpus: 0.25
33+
mem_limit: 512m
34+
mem_reservation: 256m
35+
environment:
36+
DB_POSTGRES_USER: ${DB_POSTGRES_USER:-postgres}
37+
DB_POSTGRES_PASSWORD: ${DB_POSTGRES_PASSWORD:-postgres}
38+
DB_POSTGRES_DATABASE: ${DB_POSTGRES_DATABASE:-postgres}
39+
DB_POSTGRES_HOST: dcr-postgres
40+
depends_on:
41+
- postgres
42+
networks:
43+
celery:
44+
volumes:
45+
- ./:/usr/src/celery/django-celery-results

docker/Dockerfile

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
FROM python:3.13.3-slim AS base
2+
3+
LABEL authors='Diego Castro <[email protected]>' \
4+
org.opencontainers.image.description='A Docker image to run tests for Django Celery Results using Tox' \
5+
org.opencontainers.image.documentation='https://django-celery-results.readthedocs.io/' \
6+
org.opencontainers.image.license='BSD-3-Clause' \
7+
org.opencontainers.image.source='https://github.com/celery/django-celery-results' \
8+
org.opencontainers.image.title='Dockerized Tox suite for Django Celery Results' \
9+
org.opencontainers.image.vendor='Celery'
10+
11+
RUN <<EOF
12+
apt-get update && apt-get install -y --no-install-recommends \
13+
build-essential \
14+
curl \
15+
wget \
16+
gnupg \
17+
zlib1g-dev \
18+
libbz2-dev \
19+
libreadline-dev \
20+
libsqlite3-dev \
21+
libssl-dev \
22+
libffi-dev \
23+
libpq-dev \
24+
postgresql-client \
25+
xz-utils \
26+
tk-dev \
27+
libncurses5-dev \
28+
git \
29+
bash && \
30+
apt-get clean && rm -rf /var/lib/apt/lists/*
31+
EOF
32+
33+
ENV PYTHON_VERSIONS="3.12.3 3.11.9 3.10.14 3.9.19 3.8.20" \
34+
PYPY_VERSION="pypy3.10-v7.3.19-linux64"
35+
36+
RUN <<EOF
37+
for version in $PYTHON_VERSIONS; do
38+
VERSION_MAJOR_MINOR=$(echo $version | cut -d. -f1-2) &&
39+
40+
wget https://www.python.org/ftp/python/${version}/Python-${version}.tgz &&
41+
tar -xzf Python-${version}.tgz && cd Python-${version} &&
42+
43+
./configure \
44+
--prefix=/usr \
45+
--enable-optimizations \
46+
--with-ensurepip=install \
47+
--enable-shared &&
48+
49+
make -j$(nproc) && make altinstall &&
50+
cd .. && rm -rf Python-${version} Python-${version}.tgz &&
51+
52+
ln -sf /usr/bin/python${VERSION_MAJOR_MINOR} /usr/bin/python${VERSION_MAJOR_MINOR%.*}
53+
done
54+
EOF
55+
56+
RUN <<EOF
57+
wget https://downloads.python.org/pypy/${PYPY_VERSION}.tar.bz2 &&
58+
mkdir -p /opt/pypy310 &&
59+
tar -xjC /opt/pypy310 --strip-components=1 -f ${PYPY_VERSION}.tar.bz2 &&
60+
find /opt/pypy310/lib* -depth -type d -a \( -name test -o -name tests \) -exec rm -rf '{}' +;
61+
62+
rm ${PYPY_VERSION}.tar.bz2 &&
63+
64+
ln -sf /opt/pypy310/bin/pypy3 /usr/bin/
65+
ln -sf /opt/pypy310/bin/pypy3 /usr/bin/pypy3.10
66+
EOF
67+
68+
RUN pip install --upgrade pip && pip install --no-cache-dir tox
69+
70+
WORKDIR /usr/src/celery/django-celery-results
71+
72+
COPY . .
73+
74+
ENTRYPOINT ["tox"]

docs/injecting_metadata.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Injecting metadata
2-
===============
2+
==================
33

44

55
To save arbitrary data on the field TaskResult.meta, the Celery Task Request must be manipulated as such:

t/proj/settings.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,22 @@
2828
DATABASES = {
2929
'default': {
3030
'ENGINE': 'django.db.backends.postgresql',
31-
'HOST': 'localhost',
32-
'NAME': 'postgres',
33-
'USER': 'postgres',
34-
'PASSWORD': 'postgres',
31+
'HOST': os.getenv('DB_POSTGRES_HOST', 'localhost'),
32+
'PORT': os.getenv('DB_POSTGRES_PORT', '5432'),
33+
'NAME': os.getenv('DB_POSTGRES_DATABASE', 'postgres'),
34+
'USER': os.getenv('DB_POSTGRES_USER', 'postgres'),
35+
'PASSWORD': os.getenv('DB_POSTGRES_PASSWORD', 'postgres'),
3536
'OPTIONS': {
3637
'connect_timeout': 1000,
37-
}
38+
},
3839
},
3940
'secondary': {
4041
'ENGINE': 'django.db.backends.postgresql',
41-
'HOST': 'localhost',
42-
'NAME': 'postgres',
43-
'USER': 'postgres',
44-
'PASSWORD': 'postgres',
42+
'HOST': os.getenv('DB_POSTGRES_HOST', 'localhost'),
43+
'PORT': os.getenv('DB_POSTGRES_PORT', '5432'),
44+
'NAME': os.getenv('DB_POSTGRES_DATABASE', 'postgres'),
45+
'USER': os.getenv('DB_POSTGRES_USER', 'postgres'),
46+
'PASSWORD': os.getenv('DB_POSTGRES_PASSWORD', 'postgres'),
4547
'OPTIONS': {
4648
'connect_timeout': 1000,
4749
},
@@ -51,18 +53,19 @@
5153
},
5254
'read-only': {
5355
'ENGINE': 'django.db.backends.postgresql',
54-
'HOST': 'localhost',
56+
'HOST': os.getenv('DB_POSTGRES_HOST', 'localhost'),
57+
'PORT': os.getenv('DB_POSTGRES_PORT', '5432'),
5558
'NAME': 'read-only-database',
56-
'USER': 'postgres',
57-
'PASSWORD': 'postgres',
59+
'USER': os.getenv('DB_POSTGRES_USER', 'postgres'),
60+
'PASSWORD': os.getenv('DB_POSTGRES_PASSWORD', 'postgres'),
5861
'OPTIONS': {
5962
'connect_timeout': 1000,
6063
'options': '-c default_transaction_read_only=on',
6164
},
6265
'TEST': {
6366
'MIRROR': 'default',
6467
},
65-
}
68+
},
6669
}
6770
except ImportError:
6871
DATABASES = {

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ DJANGO =
2323
5.2: django52
2424

2525
[testenv]
26+
passenv =
27+
DB_POSTGRES_*
2628
deps=
2729
-r{toxinidir}/requirements/default.txt
2830
-r{toxinidir}/requirements/test.txt

0 commit comments

Comments
 (0)