Skip to content

Commit df32138

Browse files
committed
Make it fully docker-compose-ized
To make it easier for other users to try hackergame platform. Ref: https://github.com/taoky/container-slides
1 parent 9b38edb commit df32138

File tree

8 files changed

+168
-7
lines changed

8 files changed

+168
-7
lines changed

Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:3.11
2+
3+
RUN apt-get update && \
4+
apt-get clean && \
5+
rm -rf /var/lib/apt/lists/*
6+
7+
ENV PYTHONUNBUFFERED=1
8+
WORKDIR /opt/hackergame
9+
COPY requirements.txt /opt/hackergame/
10+
RUN pip3 install --upgrade -r requirements.txt
11+
# Bind project inside instead of copying it
12+
# to avoid copying credentials inside container
13+
# COPY ./ /opt/hackergame/
14+
15+
EXPOSE 2018
16+
CMD ["docker/start.sh"]

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@
3434
1. 其他配置文件:`cp conf/pgbouncer.ini /etc/pgbouncer/`, `systemctl restart pgbouncer`
3535
1. 配置反向代理的客户端 IP 透传:前置反向代理需使用 `X-Real-IP` 请求头传递客户端 IP,`/etc/nginx/sites-enabled/hackergame` 中需添加一行 `set_real_ip_from <reverse-proxy-ip>` 以信任来自 `reverse-proxy-ip` 的指示客户端 IP 的请求头,否则平台不能正确获取用户 IP。
3636

37-
另外我们提供 [docker compose 样例](https://github.com/ustclug/hackergame/pull/175),但是实际部署不使用该容器版本。
37+
### Docker compose
38+
39+
另外我们提供包括数据库与 nginx 等在内的 [docker compose 样例](./docker-compose.yml),但是**实际部署不使用该容器版本**
40+
41+
与部署版本不同,该样例**默认开启了调试模式(环境变量 `DEBUG`),并且允许 hostname 为 localhost 等本地地址**
42+
43+
1. 复制 `docker/.env.example``.env``cp docker/.env.example .env`。并修改其中的环境变量(为数据库设置密码)。
44+
1. 执行 `docker compose up` 启动环境。
45+
1. 执行 `docker exec -it hackergame ./manage.py migrate` 初始化数据库。
46+
1. 执行 `docker exec -it hackergame ./manage.py collectstatic` 初始化 Static 目录。
47+
1. 执行 `docker exec -it hackergame ./manage.py setup` 写入 Google 与 Microsoft app secret。
3848

3949
### uWSGI 运行模型
4050

conf/nginx-sites/hackergame-docker

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
server {
2+
listen 80;
3+
#server_name hack.lug.ustc.edu.cn;
4+
server_name _;
5+
6+
charset utf-8;
7+
sendfile on;
8+
tcp_nopush on;
9+
tcp_nodelay on;
10+
server_tokens off;
11+
log_not_found off;
12+
13+
include /etc/nginx/mime.types;
14+
default_type application/octet-stream;
15+
16+
access_log /var/log/nginx/hackergame.log;
17+
error_log /var/log/nginx/hackergame.error.log;
18+
19+
gzip on;
20+
gzip_vary on;
21+
gzip_proxied any;
22+
gzip_comp_level 6;
23+
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
24+
25+
location /media {
26+
root /var/opt/hackergame;
27+
add_header Content-Type application/octet-stream;
28+
expires -1;
29+
}
30+
location /static {
31+
root /var/opt/hackergame;
32+
expires 1h;
33+
}
34+
location / {
35+
uwsgi_pass hackergame:2018;
36+
client_max_body_size 500M;
37+
include /etc/nginx/uwsgi_params;
38+
}
39+
}

conf/settings/docker.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
from .hackergame import *
2+
import os
23

4+
# Official domain and localhost for local test
5+
ALLOWED_HOSTS = ["hack.lug.ustc.edu.cn", '.localhost', '127.0.0.1', '[::1]']
6+
# For local test
7+
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
8+
DATABASES = {
9+
'default': {
10+
'ENGINE': 'django.db.backends.postgresql',
11+
'NAME': 'hackergame',
12+
'USER': 'hackergame',
13+
'CONN_MAX_AGE': 0,
14+
'ATOMIC_REQUESTS': True,
15+
'HOST': 'hackergame-pgbouncer',
16+
'PORT': 5432,
17+
'PASSWORD': os.environ.get('DB_PASSWORD', ''),
18+
},
19+
}
320
CACHES = {
421
'default': {
522
'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
6-
'LOCATION': 'memcached:11211',
23+
'LOCATION': 'hackergame-memcached:11211',
724
'TIMEOUT': 3600,
825
'KEY_PREFIX': 'hackergame',
926
},
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
[uwsgi]
2-
socket=unix:///run/uwsgi/app/hackergame-docker/socket
2+
socket=:2018
33
chdir=/opt/hackergame
44
#plugin=python3,gevent_python3
55
module=frontend.wsgi:application
6-
env=DJANGO_SETTINGS_MODULE=conf.settings.hackergame
76
env=PSYCOPG_WAIT_FUNC=wait_selector
87
master=true
98
#processes=16
109
gevent=1024
1110
gevent-monkey-patch=true
1211
vacuum=true
13-
home=/usr/local
14-
uid=www-data
15-
gid=www-data
12+
#home=/usr/local
13+
#uid=www-data
14+
#gid=www-data
1615
stats=/run/uwsgi/app/hackergame-docker/stats.socket
1716
harakiri=60

docker-compose.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
version: '2.3'
2+
3+
services:
4+
hackergame:
5+
container_name: &name hackergame
6+
hostname: *name
7+
build: .
8+
restart: always
9+
environment:
10+
- DJANGO_SETTINGS_MODULE=conf.settings.docker
11+
- DB_PASSWORD=${DB_PASSWORD}
12+
# 调试用
13+
- DEBUG=True
14+
volumes:
15+
- .:/opt/hackergame/:ro
16+
# 存储静态网页与题目文件
17+
- hackergame-static:/var/opt/hackergame/
18+
# 很不幸,你可能还需要 bind 完整的题目目录进来(不然不方便导入)
19+
depends_on:
20+
- memcached
21+
- pgbouncer
22+
memcached:
23+
container_name: hackergame-memcached
24+
image: memcached
25+
restart: always
26+
postgresql:
27+
container_name: hackergame-postgresql
28+
image: postgres:15
29+
restart: always
30+
environment:
31+
- POSTGRES_USER=hackergame
32+
- POSTGRES_PASSWORD=${DB_PASSWORD}
33+
- POSTGRES_DB=hackergame
34+
pgbouncer:
35+
container_name: hackergame-pgbouncer
36+
image: edoburu/pgbouncer:latest
37+
restart: always
38+
environment:
39+
- DB_USER=hackergame
40+
- DB_PASSWORD=${DB_PASSWORD}
41+
- DB_HOST=postgresql
42+
- POOL_MODE=transaction
43+
# 坑: pg14+ 默认使用 scram-sha-256, 而 pgbouncer 默认是 md5
44+
- AUTH_TYPE=scram-sha-256
45+
depends_on:
46+
- postgresql
47+
nginx:
48+
container_name: hackergame-nginx
49+
image: nginx:1.25
50+
restart: always
51+
volumes:
52+
- ./conf/nginx-sites/hackergame-docker:/etc/nginx/conf.d/default.conf:ro
53+
- hackergame-static:/var/opt/hackergame/:ro
54+
- nginx-log:/var/log/nginx/:rw
55+
ports:
56+
- 12345:80
57+
depends_on:
58+
- hackergame
59+
60+
volumes:
61+
hackergame-static:
62+
nginx-log:

docker/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DB_PASSWORD=youshouldmodifythis

docker/start.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh -e
2+
3+
# Set permission for /run/uwsgi/app/hackergame-docker/
4+
mkdir -p /run/uwsgi/app/hackergame-docker/
5+
chown www-data:www-data /run/uwsgi/app/hackergame-docker/
6+
7+
echo "Note that /opt/hackergame/ shall be readable by uwsgi (www-data in container)."
8+
echo "You could set it to be readable by everyone: chmod -R a+rX hackergame/"
9+
10+
echo "If this is your first time to run this container, you should run:"
11+
echo " docker exec -it hackergame ./manage.py migrate"
12+
echo " docker exec -it hackergame ./manage.py collectstatic"
13+
14+
# Start uwsgi
15+
exec /usr/local/bin/uwsgi --master --ini conf/uwsgi.ini \
16+
--ini conf/uwsgi-apps/hackergame-docker.ini \
17+
--set-placeholder appname=hackergame-docker

0 commit comments

Comments
 (0)