Skip to content

Commit 1c3103a

Browse files
author
Aleksander Ambrozkiewicz
committed
Add default Docker backend image with docker compose and postgresql
1 parent b61a656 commit 1c3103a

File tree

8 files changed

+348
-101
lines changed

8 files changed

+348
-101
lines changed

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DJANGO_SETTINGS_MODULE=restauth.settings
2+
3+
DEBUG=on
4+
ALLOWED_HOSTS=*
5+
SECRET_KEY=t6xm-!%va(qgw@)*4+sbnzyhk_&gc#s1r6!qy*=a9%u06rumy&
6+
7+
DATABASE_URL=psql://postgres:postgres@db/postgres

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ __pycache__/
22
*.py[cod]
33
.idea/
44
db.sqlite3
5-
.pytest_cache/
5+
.pytest_cache/
6+
.env

Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM python:3.7-alpine
2+
3+
ENV PYTHONUNBUFFERED 1
4+
5+
RUN apk update \
6+
&& apk add bash \
7+
# psycopg2 dependencies
8+
&& apk add --virtual build-deps gcc python3-dev musl-dev \
9+
&& apk add postgresql-dev \
10+
&& apk add build-base linux-headers pcre-dev
11+
12+
EXPOSE 3031
13+
WORKDIR /code
14+
COPY Pipfile Pipfile.lock ./
15+
RUN pip install --upgrade pip
16+
RUN pip install pipenv
17+
RUN pipenv install --system --dev
18+
COPY . .
19+
20+
CMD ["./run-backend.sh"]

Pipfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ verify_ssl = true
44
name = "pypi"
55

66
[packages]
7-
Django = "==2.0.2"
8-
djangorestframework = "==3.7.7"
9-
djangorestframework-jwt = "==1.11.0"
10-
django-hashid-field = "==2.1.0"
7+
Django = "~=2.2"
8+
djangorestframework = "~=3.10"
9+
djangorestframework-jwt = "~=1.11"
10+
django-hashid-field = "~=2.1"
1111
dj-database-url = "*"
1212

1313
[dev-packages]
@@ -18,4 +18,4 @@ pytest-django = "==3.1.2"
1818
django-rest-swagger = "*"
1919

2020
[requires]
21-
python_version = "3.6"
21+
python_version = "3.7"

Pipfile.lock

Lines changed: 108 additions & 88 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: '3'
2+
3+
services:
4+
db:
5+
image: postgres:9.6
6+
ports:
7+
- "5432:5432"
8+
volumes:
9+
- restauth_backend:/var/lib/postgresql/data
10+
11+
dev:
12+
build: .
13+
image: restauth_backend
14+
command: "./wait-for-it.sh db:5432 -- python manage.py runserver 0:8000"
15+
volumes:
16+
- .:/code
17+
ports:
18+
- "8000:8000"
19+
depends_on:
20+
- "db"
21+
restart: on-failure
22+
env_file: .env
23+
24+
volumes:
25+
restauth_backend:
26+
external: true

restauth/settings.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
# Quick-start development settings - unsuitable for production
99
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
1010

11-
# SECURITY WARNING: keep the secret key used in production secret!
12-
SECRET_KEY = '4y7c-7+bja2@e1$%d@6v+#dir%70!dc7!7_04e2r(d%4$g7+id'
13-
14-
# SECURITY WARNING: don't run with debug turned on in production!
15-
DEBUG = True
16-
17-
ALLOWED_HOSTS = []
11+
SECRET_KEY = os.environ.get('SECRET_KEY')
12+
DEBUG = os.environ.get('DEBUG')
13+
ALLOWED_HOSTS = list(filter(lambda s: len(s) > 0, map(str.strip, os.environ.get('ALLOWED_HOSTS').split(','))))
1814

1915

2016
# Application definition

wait-for-it.sh

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#!/usr/bin/env bash
2+
# Use this script to test if a given TCP host/port are available
3+
4+
cmdname=$(basename $0)
5+
6+
echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
7+
8+
usage()
9+
{
10+
cat << USAGE >&2
11+
Usage:
12+
$cmdname host:port [-s] [-t timeout] [-- command args]
13+
-h HOST | --host=HOST Host or IP under test
14+
-p PORT | --port=PORT TCP port under test
15+
Alternatively, you specify the host and port as host:port
16+
-s | --strict Only execute subcommand if the test succeeds
17+
-q | --quiet Don't output any status messages
18+
-t TIMEOUT | --timeout=TIMEOUT
19+
Timeout in seconds, zero for no timeout
20+
-- COMMAND ARGS Execute command with args after the test finishes
21+
USAGE
22+
exit 1
23+
}
24+
25+
wait_for()
26+
{
27+
if [[ $TIMEOUT -gt 0 ]]; then
28+
echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
29+
else
30+
echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
31+
fi
32+
start_ts=$(date +%s)
33+
while :
34+
do
35+
if [[ $ISBUSY -eq 1 ]]; then
36+
nc -z $HOST $PORT
37+
result=$?
38+
else
39+
(echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
40+
result=$?
41+
fi
42+
if [[ $result -eq 0 ]]; then
43+
end_ts=$(date +%s)
44+
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
45+
break
46+
fi
47+
sleep 1
48+
done
49+
return $result
50+
}
51+
52+
wait_for_wrapper()
53+
{
54+
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
55+
if [[ $QUIET -eq 1 ]]; then
56+
timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
57+
else
58+
timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
59+
fi
60+
PID=$!
61+
trap "kill -INT -$PID" INT
62+
wait $PID
63+
RESULT=$?
64+
if [[ $RESULT -ne 0 ]]; then
65+
echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
66+
fi
67+
return $RESULT
68+
}
69+
70+
# process arguments
71+
while [[ $# -gt 0 ]]
72+
do
73+
case "$1" in
74+
*:* )
75+
hostport=(${1//:/ })
76+
HOST=${hostport[0]}
77+
PORT=${hostport[1]}
78+
shift 1
79+
;;
80+
--child)
81+
CHILD=1
82+
shift 1
83+
;;
84+
-q | --quiet)
85+
QUIET=1
86+
shift 1
87+
;;
88+
-s | --strict)
89+
STRICT=1
90+
shift 1
91+
;;
92+
-h)
93+
HOST="$2"
94+
if [[ $HOST == "" ]]; then break; fi
95+
shift 2
96+
;;
97+
--host=*)
98+
HOST="${1#*=}"
99+
shift 1
100+
;;
101+
-p)
102+
PORT="$2"
103+
if [[ $PORT == "" ]]; then break; fi
104+
shift 2
105+
;;
106+
--port=*)
107+
PORT="${1#*=}"
108+
shift 1
109+
;;
110+
-t)
111+
TIMEOUT="$2"
112+
if [[ $TIMEOUT == "" ]]; then break; fi
113+
shift 2
114+
;;
115+
--timeout=*)
116+
TIMEOUT="${1#*=}"
117+
shift 1
118+
;;
119+
--)
120+
shift
121+
CLI=("$@")
122+
break
123+
;;
124+
--help)
125+
usage
126+
;;
127+
*)
128+
echoerr "Unknown argument: $1"
129+
usage
130+
;;
131+
esac
132+
done
133+
134+
if [[ "$HOST" == "" || "$PORT" == "" ]]; then
135+
echoerr "Error: you need to provide a host and port to test."
136+
usage
137+
fi
138+
139+
TIMEOUT=${TIMEOUT:-15}
140+
STRICT=${STRICT:-0}
141+
CHILD=${CHILD:-0}
142+
QUIET=${QUIET:-0}
143+
144+
# check to see if timeout is from busybox?
145+
# check to see if timeout is from busybox?
146+
TIMEOUT_PATH=$(realpath $(which timeout))
147+
if [[ $TIMEOUT_PATH =~ "busybox" ]]; then
148+
ISBUSY=1
149+
BUSYTIMEFLAG="-t"
150+
else
151+
ISBUSY=0
152+
BUSYTIMEFLAG=""
153+
fi
154+
155+
if [[ $CHILD -gt 0 ]]; then
156+
wait_for
157+
RESULT=$?
158+
exit $RESULT
159+
else
160+
if [[ $TIMEOUT -gt 0 ]]; then
161+
wait_for_wrapper
162+
RESULT=$?
163+
else
164+
wait_for
165+
RESULT=$?
166+
fi
167+
fi
168+
169+
if [[ $CLI != "" ]]; then
170+
if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
171+
echoerr "$cmdname: strict mode, refusing to execute subprocess"
172+
exit $RESULT
173+
fi
174+
exec "${CLI[@]}"
175+
else
176+
exit $RESULT
177+
fi

0 commit comments

Comments
 (0)