Dockerfile#
1
2
3
4
5
6
7
8
9
10
|
FROM python:3.7.9
RUN sed -i "s@http://deb.debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list && rm -Rf /var/lib/apt/lists/* && apt-get update && apt-get install -y supervisor --no-install-recommends
ADD . /var/web_backend
WORKDIR /var/web_backend
RUN pip install -i https://mirrors.aliyun.com/pypi/simple -r requirements.txt
COPY daphne.conf /etc/supervisor/conf.d/
RUN chmod u+x ./start.sh
EXPOSE 80
ENTRYPOINT ["sh"]
CMD ["./start.sh"]
|
Supervisor#
⚠️ 注意,这里的 socket=tcp://0.0.0.0:80
不能使用 localhost,否则只能在容器内访问服务。
下面是我的 supervisor 配置文件 daphne.conf
的内容。配置文件的内容参考自文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[fcgi-program:daphne]
# TCP socket used by Nginx backend upstream
socket=tcp://0.0.0.0:80
# Directory where your site's project files are located
directory=/var/web_backend
user=root
# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=daphne --fd 0 --access-log - --proxy-headers web_backend.asgi:application
# Number of processes to startup, roughly the number of CPUs you have
numprocs=4
# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d
# Automatically start and recover processes
autostart=true
autorestart=true
# Choose where you want your log to go
redirect_stderr=true
stdout_logfile=/var/log/web_backend/asgi.log
stderr_logfile=/var/log/web_backend/asgierror.log
|
启动脚本 start.sh
#
⚠️ 注意,这里的 -n 参数一定要加上,让服务以非后台服务的方式启动,否则 docker 服务会一直不停重启。
启动脚本 start.sh
内容如下:
1
2
|
#!bin/bash
supervisord -n -c /etc/supervisor/supervisord.conf
|
Use docker-compose#
docker-compose 配置文件#
使用 docker-compose 启动 docker 服务。docker-compose.yml 文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
version: '3'
services:
asgi_backend:
build: .
container_name: web_backend
ports:
- "8080:80"
volumes:
- /var/log/web_backend:/var/log/web_backend
environment:
- DJANGO_SETTINGS_MODULE=web_backend.settings.dev
restart: always
|
启动服务#
1
|
docker-compose -f docker-compose.yml up -d
|