Skip to content

Commit

Permalink
feat: 增加 Docker 相关支持
Browse files Browse the repository at this point in the history
1. 使用 docker-compose 快速搭建开发环境
2. 构建成 Docker 镜像
  • Loading branch information
stackia committed Mar 16, 2020
1 parent ecfca08 commit 4ac6dd8
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 0 deletions.
81 changes: 81 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/


# PyCharm
# http://www.jetbrains.com/pycharm/webhelp/project.html
.idea
.iml

# virtualenv
venv/

migrations/
!migrations/__init__.py
collectedstatic/
DjangoBlog/whoosh_index/
google93fd32dbd906620a.html
baidu_verify_FlHL7cUyC9.html
BingSiteAuth.xml
cb9339dbe2ff86a5aa169d28dba5f615.txt
werobot_session
django.jpg
uploads/
settings_production.py
werobot_session.db
65 changes: 65 additions & 0 deletions docker-support/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
FROM alpine:3.11
WORKDIR /app

COPY ./requirements.txt .

RUN apk --update --no-cache add python3 py3-pip py-gunicorn nginx mariadb-connector-c \
&& apk --update --no-cache add --virtual .build-deps \
tzdata \
# mysqlclient dependencies
python3-dev \
mariadb-connector-c-dev \
gcc \
musl-dev \
# Pillow dependencies
jpeg-dev \
zlib-dev \
freetype-dev \
lcms2-dev \
openjpeg-dev \
tiff-dev \
tk-dev \
tcl-dev \
harfbuzz-dev \
fribidi-dev \
&& pip3 install --upgrade pip setuptools gevent \
&& mkdir -p /run/nginx \
&& pip3 install -r requirements.txt \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del .build-deps

COPY . .
RUN python3 manage.py collectstatic --noinput \
&& python3 manage.py compress --force

COPY ./docker-support/nginx.conf /etc/nginx/conf.d/default.conf

ENV DJANGO_DEBUG False
ENV DJANGO_SECRET_KEY DJANGO_BLOG_CHANGE_ME

ENV DJANGO_MYSQL_DATABASE djangoblog
ENV DJANGO_MYSQL_USER root
ENV DJANGO_MYSQL_PASSWORD djangoblog_123
ENV DJANGO_MYSQL_HOST 127.0.0.1
ENV DJANGO_MYSQL_PORT 3306

ENV DJANGO_MEMCACHED_ENABLE True
ENV DJANGO_MEMCACHED_LOCATION 127.0.0.1:11211

ENV DJANGO_BAIDU_NOTIFY_URL http://data.zz.baidu.com/urls?site=https://www.example.org&token=CHANGE_ME

ENV DJANGO_EMAIL_TLS False
ENV DJANGO_EMAIL_SSL True
ENV DJANGO_EMAIL_HOST smtp.example.org
ENV DJANGO_EMAIL_PORT 465
ENV DJANGO_EMAIL_USER SMTP_USER_CHANGE_ME
ENV DJANGO_EMAIL_PASSWORD SMTP_PASSWORD_CHANGE_ME

ENV DJANGO_ADMIN_EMAIL admin@example.org

ENV DJANGO_WXADMIN_PASSWORD DJANGO_BLOG_CHANGE_ME
ENV DJANGO_WEROBOT_TOKEN DJANGO_BLOG_CHANGE_ME

EXPOSE 80
ENTRYPOINT ["./docker-support/entrypoint.sh"]
57 changes: 57 additions & 0 deletions docker-support/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Docker 支持

## 使用 docker-compose 快速搭建开发环境(MySQL / Memcached)

我们提供了 `dev-environment-setup.yml` 用于快速搭建开发环境。

```shell script
docker-compose -f ./docker-support/dev-environment-setup.yml up
```

运行这条命令后,可以快速搭建起以下环境:

- MySQL 5.7 - 端口 `3306`,用户名 `root`,密码 `djangoblog_123`,自动以 UTF8MB4 编码创建 `djangoblog` 数据库
- Memcached - 端口 `11211`

## 构建镜像

```shell script
docker build -f .\docker-support\Dockerfile -t <你的 Docker Hub 用户名>/django_blog:latest .
```

## 运行自定义指令(例如数据库迁移)

```shell script
docker run -it --rm <你的 Docker Hub 用户名>/django_blog:latest <指令>
```

例如:

```shell script
docker run -it --rm -e DJANGO_MYSQL_HOST=192.168.231.50 django_blog/django_blog:latest makemigrations
docker run -it --rm -e DJANGO_MYSQL_HOST=192.168.231.50 django_blog/django_blog:latest migrate
docker run -it --rm -e DJANGO_MYSQL_HOST=192.168.231.50 django_blog/django_blog:latest createsuperuser
```

## 环境变量清单

| 环境变量名称 | 默认值 | 备注 |
|---------------------------|----------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| DJANGO_DEBUG | False | |
| DJANGO_SECRET_KEY | DJANGO_BLOG_CHANGE_ME | 请务必修改,建议[随机生成](https://www.random.org/passwords/?num=5&len=24&format=html&rnd=new) |
| DJANGO_MYSQL_DATABASE | djangoblog | |
| DJANGO_MYSQL_USER | root | |
| DJANGO_MYSQL_PASSWORD | djangoblog_123 | |
| DJANGO_MYSQL_HOST | 127.0.0.1 | |
| DJANGO_MYSQL_PORT | 3306 | |
| DJANGO_MEMCACHED_ENABLE | True | |
| DJANGO_MEMCACHED_LOCATION | 127.0.0.1:11211 | |
| DJANGO_BAIDU_NOTIFY_URL | http://data.zz.baidu.com/urls?site=https://www.example.org&token=CHANGE_ME | 请在[百度站长平台](https://ziyuan.baidu.com/linksubmit/index)获取接口地址 |
| DJANGO_EMAIL_TLS | False | |
| DJANGO_EMAIL_SSL | True | |
| DJANGO_EMAIL_HOST | smtp.example.org | |
| DJANGO_EMAIL_PORT | 465 | |
| DJANGO_EMAIL_USER | SMTP_USER_CHANGE_ME | |
| DJANGO_EMAIL_PASSWORD | SMTP_PASSWORD_CHANGE_ME | |
| DJANGO_ADMIN_EMAIL | admin@example.org | |
| DJANGO_WEROBOT_TOKEN | DJANGO_BLOG_CHANGE_ME | 请使用自己的微信公众号通信令牌(Token) |
17 changes: 17 additions & 0 deletions docker-support/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
cd /app

if [ $# -eq 0 ]; then
nginx
gunicorn DjangoBlog.wsgi:application \
--name djangoblog \
--user root \
--group root \
--bind 127.0.0.1:8000 \
--log-level=debug \
--log-file=- \
--workers $(grep -c ^processor /proc/cpuinfo) \
--worker-class gevent
else
python3 ./manage.py $1
fi
22 changes: 22 additions & 0 deletions docker-support/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
server {
root /app/;
listen 80;
keepalive_timeout 70;

location /static/ {
expires max;
alias /app/collectedstatic/;
}

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8000;
break;
}
}
}

0 comments on commit 4ac6dd8

Please sign in to comment.