Skip to content

Commit 19a7a14

Browse files
Built and pushed Docker image to Github Packages (django#121)
1 parent bff4486 commit 19a7a14

File tree

10 files changed

+221
-4
lines changed

10 files changed

+221
-4
lines changed

.dockerignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.direnv/
2+
.dockerignore
3+
.envrc
4+
.git/
5+
.github/
6+
.gitignore
7+
docker-compose.yml
8+
*.pyc

.github/workflows/docker-publish.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Create and publish a Docker image
2+
3+
on:
4+
push:
5+
branches: ['main']
6+
7+
env:
8+
REGISTRY: ghcr.io
9+
IMAGE_NAME: ${{ github.repository }}
10+
11+
jobs:
12+
build-and-push-image:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
packages: write
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v2
21+
22+
- name: Log in to the Container registry
23+
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
24+
with:
25+
registry: ${{ env.REGISTRY }}
26+
username: ${{ github.actor }}
27+
password: ${{ secrets.GITHUB_TOKEN }}
28+
29+
- name: Extract metadata (tags, labels) for Docker
30+
id: meta
31+
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
32+
with:
33+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
34+
flavor: |
35+
latest=true
36+
tags: |
37+
type=ref,event=branch
38+
type=sha
39+
40+
- name: Build and push Docker image
41+
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
42+
with:
43+
context: .
44+
push: true
45+
tags: ${{ steps.meta.outputs.tags }}
46+
labels: ${{ steps.meta.outputs.labels }}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
*.bak
12
*.pyc
3+
.direnv/
4+
.envrc
25
.sass-cache/
36
DjangoPlugin/Django_Plugin.egg-info/
47
trac-env/htdocs/css/trachacks.css*

Dockerfile

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# The official python:2.7 image no longer receives automatic rebuilds (it's
2+
# a year old as of October, 2021), so use the latest LTS release of Ubuntu
3+
# that includes Python 2.7 instead.
4+
FROM ubuntu:20.04
5+
6+
# Install packages needed to run your application (not build deps).
7+
RUN set -x \
8+
&& RUN_DEPS=" \
9+
ca-certificates \
10+
git \
11+
libpq5 \
12+
make \
13+
postgresql-client \
14+
python2.7 \
15+
" \
16+
&& apt-get update && apt-get install -y --no-install-recommends $RUN_DEPS \
17+
&& rm -rf /var/lib/apt/lists/*
18+
19+
# Copy requirements
20+
ADD requirements.txt /requirements.txt
21+
ADD DjangoPlugin /DjangoPlugin
22+
23+
# Install build deps, then run `pip install`, then remove unneeded build deps all in a single step.
24+
# Correct the path to your production requirements file, if needed.
25+
# For installing a python2.7-compatible pip: https://stackoverflow.com/a/54335642/166053
26+
# Since we are using the system Python, also isolate the code in its own virtualenv.
27+
RUN set -x \
28+
&& BUILD_DEPS=" \
29+
build-essential \
30+
libpq-dev \
31+
python2.7-dev \
32+
wget \
33+
" \
34+
&& apt-get update && apt-get install -y --no-install-recommends $BUILD_DEPS \
35+
&& wget -q -O /tmp/get-pip.py https://bootstrap.pypa.io/pip/2.7/get-pip.py \
36+
&& python2.7 /tmp/get-pip.py \
37+
&& rm /tmp/get-pip.py \
38+
&& python2.7 -m pip install virtualenv \
39+
&& virtualenv /venv \
40+
&& /venv/bin/python -m pip install --no-cache-dir -r /requirements.txt \
41+
\
42+
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $BUILD_DEPS \
43+
&& rm -rf /var/lib/apt/lists/*
44+
45+
# Copy application code
46+
RUN mkdir /code/
47+
WORKDIR /code/
48+
ADD . /code/
49+
50+
RUN PATH=/venv/bin:${PATH} make compile-scss
51+
52+
VOLUME /code/trac-env/files/
53+
54+
# gunicorn or tracd will listen on this port
55+
EXPOSE 9000
56+
57+
ENV DJANGO_SETTINGS_MODULE=tracdjangoplugin.settings TRAC_ENV=/code/trac-env/
58+
59+
ENTRYPOINT ["/code/docker-entrypoint.sh"]
60+
61+
# Start gunicorn
62+
CMD ["/venv/bin/gunicorn", "tracdjangoplugin.wsgi:application", "--bind", "0.0.0.0:9000", "--workers", "4", "--max-requests", "1000"]

README.rst

+50-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,38 @@ Local install
77
Getting a local Trac install running is a bit tricky. Here are a few tricks
88
that can help:
99

10-
* Install the requirements in the same virtualenv as djangoproject.com.
1110
* Follow the installation instructions in djangoproject/README.rst (especially
1211
the database creation).
13-
* Use ``psql -U code.djangoproject -d code.djangoproject -c "INSERT INTO permission (username, action) VALUES ('anonymous', 'TRAC_ADMIN')"``
12+
* Use ``trac-admin ./trac-env/ permission add anonymous TRAC_ADMIN``
1413
to give all permissions to the anonymous user.
1514
* Use the command ``tracd --port 9000 -s trac-env`` to serve Trac locally.
1615
* If you've modified the ``trackhack.scss`` file, use
1716
``sassc scss/trachacks.scss trac-env/htdocs/css/trachacks.css -s compressed``
1817
to compile it to CSS.
1918

19+
Using Docker
20+
------------
21+
22+
* Install Docker
23+
* ``pip install docker-compose``
24+
* ``docker-compose up --build``
25+
* Follow instructions above to create/load the DB, grant permissions, create the
26+
config, etc. For example::
27+
28+
docker-compose up --build
29+
export DATABASE_URL=postgres://code.djangoproject:secret@db/code.djangoproject
30+
docker-compose exec -T db psql $DATABASE_URL < ../djangoproject.com/tracdb/trac.sql
31+
docker-compose exec trac /venv/bin/trac-admin /code/trac-env/ permission add anonymous TRAC_ADMIN
32+
33+
Using Podman
34+
------------
35+
36+
It may be possible to use Podman for local development to more closely simulate
37+
production. The above Docker instructions should work for Podman as well,
38+
however, be aware that ``podman-compose`` is not as well battle-tested as
39+
``docker-compose`` (e.g., it may require pruning or forcefully stopping a
40+
container before it will rebuild properly).
41+
2042
How to port the CSS from djangoproject.com
2143
------------------------------------------
2244

@@ -31,3 +53,29 @@ same directory (adjust paths if needed).
3153
``cp -rT ../djangoproject.com/static/js trac-env/htdocs/js``
3254
4. Compile trackhacks.scss:
3355
``make compile-scss``
56+
57+
How to recreate `trac.sql` after upgrading Trac
58+
-----------------------------------------------
59+
60+
61+
Start with a clean slate::
62+
63+
docker-compose down
64+
sh -c 'cd ../djangoproject.com && git checkout tracdb/trac.sql'
65+
66+
Bring up database and Trac via docker-compose::
67+
68+
docker-compose up --build -d
69+
export DATABASE_URL=postgres://code.djangoproject:secret@db/code.djangoproject
70+
docker-compose exec -T db psql $DATABASE_URL < ../djangoproject.com/tracdb/trac.sql
71+
docker-compose exec trac /venv/bin/trac-admin /code/trac-env/ upgrade
72+
docker-compose exec db pg_dump --column-inserts -d $DATABASE_URL > ../djangoproject.com/tracdb/trac.sql
73+
74+
75+
Note:
76+
77+
* There's no need to run the ``trac-admin ... wiki upgrade`` command
78+
* Be careful with changes that ``trac-admin ... upgrade`` makes to
79+
``trac.ini``. We usually don't want them.
80+
* Review the diff carefully to make sure no unexpected database changes
81+
are inadvertently included.

docker-compose.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: '3.7'
2+
3+
services:
4+
trac:
5+
image: ghcr.io/django/code.djangoproject.com:latest
6+
# Comment out to use the ghcr.io image:
7+
build:
8+
context: ./
9+
dockerfile: Dockerfile
10+
command: ["/venv/bin/tracd", "--port", "9000", "-s", "trac-env"]
11+
environment:
12+
- TRAC_INI_database=postgres://code.djangoproject:secret@db/code.djangoproject
13+
volumes:
14+
- ./:/code/
15+
ports:
16+
- 9000:9000
17+
depends_on:
18+
- db
19+
db:
20+
image: postgres:10-alpine
21+
ports:
22+
# Port 5435 might be handy for loading trac.sql from the djangoproject.com repo
23+
- 5435:5432
24+
environment:
25+
- POSTGRES_USER=code.djangoproject
26+
- POSTGRES_PASSWORD=secret
27+
- POSTGRES_DB=code.djangoproject

docker-entrypoint.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
# Fail loudly, don't echo variables to logs
4+
set -e
5+
6+
# Trac doesn't read environment variables, so allow setting arbitrary lines in
7+
# trac.ini from the environment before Trac starts. E.g.,
8+
# export TRAC_INI_database = postgres://...
9+
# will become
10+
# database = postgres://...
11+
12+
for var in "${!TRAC_INI_@}"; do
13+
sed -i "s;^${var:9} = .*;${var:9} = ${!var};" trac-env/conf/trac.ini
14+
done
15+
16+
if [ "x$TRAC_COLLECT_STATIC" = 'xon' ]; then
17+
# Collect trac static files to be served by nginx
18+
/venv/bin/trac-admin ./trac-env/ deploy ./static/
19+
fi
20+
21+
exec "$@"

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Trac==1.2.6
22
Pygments==2.5.2
33
dnspython==1.15
44
spambayes == 1.1b1
5-
psycopg2-binary==2.7.6.1
5+
psycopg2==2.7.6.1 --no-binary=psycopg2
66
docutils==0.14
77
Django==1.11.29
88
libsass==0.17.0
@@ -16,4 +16,6 @@ requests==2.20.1
1616
requests-oauthlib==1.0.0
1717
trac-github==2.3
1818

19+
gunicorn==19.10.0
20+
1921
-e ./DjangoPlugin

trac-env/conf/trac.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ default_anonymous_query = status!=closed&desc=1&order=id
139139
default_query = status!=closed&desc=1&order=id
140140

141141
[repositories]
142-
.dir = /home/www/trac/django-mirror
142+
.dir = /django-mirror
143143
.type = git
144144

145145
[search]
File renamed without changes.

0 commit comments

Comments
 (0)