Skip to content

Commit 2ba9ab9

Browse files
author
yangxg
committed
Step19: dockerize
1 parent 680a241 commit 2ba9ab9

19 files changed

+231
-30
lines changed

.dockerignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.*
2+
_credentials.py
3+
fabfile.py
4+
*.sqlite3

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ venv.bak/
106106
*.sqlite3
107107
media/
108108
_credentials.py
109+
.production
109110

110111
# Pycharm .idea folder, see following link to know which files should be ignored:
111112
# https://www.jetbrains.com/help/pycharm/synchronizing-and-sharing-settings.html#7e81d3cb

.idea/django-blog-tutorial-v2.iml .idea/HelloDjango-blog-tutorial.iml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Pipfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[source]]
22
name = "pypi"
3-
url = "https://pypi.org/simple"
3+
url = "https://pypi.douban.com/simple"
44
verify_ssl = true
55

66
[dev-packages]

Pipfile.lock

+18-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,36 @@ tutorial 分支为项目的主分支,每一篇教程的代码都和历史提
8686

8787
### Docker
8888

89-
即将奉上!敬请期待~
89+
1. **安装 Docker 和 Docker Compose**
90+
91+
2. **克隆项目到本地**
92+
93+
```
94+
git clone https://github.com/HelloGitHub-Team/HelloDjango-blog-tutorial.git
95+
```
96+
97+
3. **构建镜像和启动容器**
98+
99+
```
100+
docker-compose -f local.yml build
101+
docker-compose -f local.yml up
102+
```
103+
104+
4. **创建后台管理员账户**
105+
106+
```
107+
docker exec -it hellodjango_blog_tutorial_local python manage.py createsuperuser
108+
```
109+
110+
其中 hellodjango_blog_tutorial_local 为项目预定义容器名
111+
112+
5. 进入后台发布文章
113+
114+
在浏览器访问:http://127.0.0.1:8000/admin
115+
116+
使用第 3 步创建的后台管理员账户登录
117+
118+
具体请参阅 [创作后台开启,请开始你的表演](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/65/)
90119

91120
## 教程目录索引
92121

@@ -110,6 +139,7 @@ tutorial 分支为项目的主分支,每一篇教程的代码都和历史提
110139
16. [Nginx+Gunicorn+Supervisor 部署 Django 博客应用](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/74/)
111140
17. [使用 Fabric 自动化部署](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/75/)
112141
18. [使用 Certbot 向 Let's Encrypt 免费申请 HTTPS 证书](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/76/)
142+
19. [使用 Docker 让部署 Django 项目更加轻松](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/77/)
113143

114144
## 公众号
115145
<p align="center">

blogproject/settings/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
DATABASES = {
7171
'default': {
7272
'ENGINE': 'django.db.backends.sqlite3',
73-
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
73+
'NAME': os.path.join(BASE_DIR, 'database', 'db.sqlite3'),
7474
}
7575
}
7676

compose/local/Dockerfile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM python:3.6-alpine
2+
3+
ENV PYTHONUNBUFFERED 1
4+
5+
# 替换为国内源
6+
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
7+
8+
RUN apk update \
9+
# Pillow dependencies
10+
&& apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev
11+
12+
WORKDIR /app
13+
14+
RUN pip install pipenv -i https://pypi.douban.com/simple
15+
16+
COPY Pipfile /app/Pipfile
17+
COPY Pipfile.lock /app/Pipfile.lock
18+
RUN pipenv install --system --deploy --ignore-pipfile
19+
20+
COPY ./compose/local/start.sh /start.sh
21+
RUN sed -i 's/\r//' /start.sh
22+
RUN chmod +x /start.sh

compose/local/start.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
python manage.py migrate
4+
python manage.py runserver 0.0.0.0:8000

compose/production/django/Dockerfile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM python:3.6-alpine
2+
3+
ENV PYTHONUNBUFFERED 1
4+
5+
# 替换为国内源
6+
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
7+
8+
RUN apk update \
9+
# Pillow dependencies
10+
&& apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev
11+
12+
WORKDIR /app
13+
14+
RUN pip install pipenv -i https://pypi.douban.com/simple
15+
16+
COPY Pipfile /app/Pipfile
17+
COPY Pipfile.lock /app/Pipfile.lock
18+
RUN pipenv install --system --deploy --ignore-pipfile
19+
20+
COPY . /app
21+
22+
COPY ./compose/production/django/start.sh /start.sh
23+
RUN sed -i 's/\r//' /start.sh
24+
RUN chmod +x /start.sh

compose/production/django/start.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
python manage.py migrate
4+
python manage.py collectstatic --noinput
5+
gunicorn blogproject.wsgi:application -w 4 -k gthread -b 0.0.0.0:8000 --chdir=/app

compose/production/nginx/Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM nginx:1.17.1
2+
3+
# 替换为国内源
4+
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak
5+
COPY ./compose/production/nginx/sources.list /etc/apt/
6+
RUN apt-get update && apt-get install -y --allow-unauthenticated certbot python-certbot-nginx
7+
8+
RUN rm /etc/nginx/conf.d/default.conf
9+
COPY ./compose/production/nginx/hellodjango-blog-tutorial.conf /etc/nginx/conf.d/hellodjango-blog-tutorial.conf
10+
11+
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
upstream hellodjango_blog_tutorial {
2+
server hellodjango_blog_tutorial:8000;
3+
}
4+
5+
server {
6+
server_name hellodjango-blog-tutorial-demo.zmrenwu.com;
7+
8+
location /static {
9+
alias /apps/hellodjango_blog_tutorial/static;
10+
}
11+
12+
location / {
13+
proxy_set_header Host $host;
14+
proxy_set_header X-Real-IP $remote_addr;
15+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
16+
proxy_set_header X-Forwarded-Proto $scheme;
17+
18+
proxy_pass http://hellodjango_blog_tutorial;
19+
}
20+
21+
listen 80;
22+
}

compose/production/nginx/sources.list

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
2+
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
3+
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
4+
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
5+
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
6+
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
7+
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
8+
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
9+
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
10+
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
11+
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
12+
deb http://archive.canonical.com/ubuntu xenial partner
13+
deb-src http://archive.canonical.com/ubuntu xenial partner
14+
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
15+
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
16+
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
17+
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
18+
19+
deb http://mirrors.tencentyun.com/debian/ jessie main non-free contrib
20+
deb http://mirrors.tencentyun.com/debian/ jessie-updates main non-free contrib
21+
deb http://mirrors.tencentyun.com/debian/ jessie-backports main non-free contrib
22+
deb-src http://mirrors.tencentyun.com/debian/ jessie main non-free contrib
23+
deb-src http://mirrors.tencentyun.com/debian/ jessie-updates main non-free contrib
24+
deb-src http://mirrors.tencentyun.com/debian/ jessie-backports main non-free contrib
25+
deb http://mirrors.tencentyun.com/debian-security/ jessie/updates main non-free contrib
26+
deb-src http://mirrors.tencentyun.com/debian-security/ jessie/updates main non-free contrib
27+
28+
deb http://deb.debian.org/debian stretch main
29+
deb http://security.debian.org/debian-security stretch/updates main
30+
deb http://deb.debian.org/debian stretch-updates main

fabfile.py

-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ def deploy(c):
3737
responders = _get_github_auth_responders()
3838
c.run(cmd, watchers=responders)
3939

40-
# 安装依赖,迁移数据库,收集静态文件
41-
with c.cd(project_root_path):
42-
c.run('~/.local/bin/pipenv install --deploy --ignore-pipfile')
43-
c.run('~/.local/bin/pipenv run python manage.py migrate')
44-
c.run('~/.local/bin/pipenv run python manage.py collectstatic --noinput')
45-
4640
# 重新启动应用
4741
with c.cd(supervisor_conf_path):
4842
cmd = 'supervisorctl start {}'.format(supervisor_program_name)

local.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: '3'
2+
3+
volumes:
4+
database_local:
5+
6+
services:
7+
hellodjango_blog_tutorial_local:
8+
build:
9+
context: .
10+
dockerfile: ./compose/local/Dockerfile
11+
image: hellodjango_blog_tutorial_local
12+
container_name: hellodjango_blog_tutorial_local
13+
working_dir: /app
14+
volumes:
15+
- database_local:/app/database
16+
- .:/app
17+
ports:
18+
- "8000:8000"
19+
command: /start.sh

production.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: '3'
2+
3+
volumes:
4+
static:
5+
database:
6+
7+
services:
8+
hellodjango_blog_tutorial:
9+
build:
10+
context: .
11+
dockerfile: compose/production/django/Dockerfile
12+
image: hellodjango_blog_tutorial
13+
container_name: hellodjango_blog_tutorial
14+
working_dir: /app
15+
volumes:
16+
- database:/app/database
17+
- static:/app/static
18+
env_file:
19+
- .envs/.production
20+
ports:
21+
- "8000:8000"
22+
command: /start.sh
23+
24+
nginx:
25+
build:
26+
context: .
27+
dockerfile: compose/production/nginx/Dockerfile
28+
image: hellodjango_blog_tutorial_nginx
29+
container_name: hellodjango_blog_tutorial_nginx
30+
volumes:
31+
- static:/apps/hellodjango_blog_tutorial/static
32+
ports:
33+
- "80:80"
34+
- "443:443"

0 commit comments

Comments
 (0)