配置Django的Dockerfile:
FROM python:3.6
RUN mkdir /code
COPY ./ProjectName /code
COPY ./uwsgi_config /code
COPY ./start.sh /code
WORKDIR /code
RUN mkdir ~/.pip
RUN chmod 666 ~/.pip
RUN touch ~/.pip/pip.conf
RUN chmod 666 ~/.pip/pip.conf
RUN echo "[global]" >> ~/.pip/pip.conf
RUN echo "timeout = 6000" >> ~/.pip/pip.conf
RUN echo "index-url = http://pypi.douban.com/simple/" >> ~/.pip/pip.conf
RUN echo "[install]" >> ~/.pip/pip.conf
RUN echo "use-mirrors = true" >> ~/.pip/pip.conf
RUN echo "mirrors = http://pypi.douban.com/simple/" >> ~/.pip/pip.conf
RUN echo "trusted-host = pypi.douban.com" >> ~/.pip/pip.conf
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
RUN pip3 install uwsgi
EXPOSE 8000
# CMD ["sh", "start.sh"]
# CMD ["uwsgi", "--ini", "/code/uwsgi.ini"]
配置uwsgi:
[uwsgi]
# the base directory (full path)
chdir = /code
# Django's wsgi file
module = ProjectName.wsgi
# max request count
max-requests = 5000
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 5
# the socket (use the full path to be safe
http = :8000
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
start.sh 配置(做一些预设值之类的东西,可以不要):
#!/bin/bash
python manage.py makemigrations
python manage.py migrate
Dockerfile:
FROM nginx:latest
WORKDIR /usr/share/nginx/html/
# self nginx conf
ADD default.conf /etc/nginx/conf.d/
RUN mkdir /usr/share/nginx/html/static
# django static file dir
ADD ./static /usr/share/nginx/html/static
EXPOSE 80
default.conf配置:
server {
listen 80;
server_name server_ip;
charset utf-8;
client_max_body_size 75M;
location /static {
alias /usr/share/nginx/html/static;
}
location / {
proxy_pass http://server_ip:8000;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
docker-compose.yml配置:
version: '3'
services:
mysql:
image: 'mysql:5.7'
container_name: mysql
restart: always
ports:
- '3306:3306'
volumes:
- './mysql/data:/var/lib/mysql'
- './mysql/conf:/etc/mysql/conf.d'
environment:
MYSQL_ROOT_PASSWORD: 123123
MYSQL_DATABASE: db
TZ: Asia/Shanghai
web:
build: ./projectname
container_name: web_name
restart: always
depends_on:
- mysql
- redis
ports:
- '8000:8000'
command: bash -c "python manage.py makemigrations && python manage.py migrate && uwsgi --ini /code/uwsgi.ini"
redis:
image: 'redis:5.0'
container_name: redis
restart: always
ports:
- '6379:6379'
nginx:
build: ~/worker/nginx
container_name: nginx
restart: always
ports:
- 80:80
volumes:
- ~/worker/nginx/log:/var/log/nginx/
depends_on:
- web
执行命令:
docker-compose build
docker-compose up -d
docker run -it 镜像名 bash
docker exec -it 容器id bash
docker run -p 80:80 -d 镜像名
注:当 web 设置depends_on 为mysql 后,需要在Django连接mysql的 host 把 IP 的值改成 db,否则报错:
django.db.utils.OperationalError: (2003, 'Can\'t connect to MySQL server on \'mariadb55\' (111 "Connection refused")')
参考:https://segmentfault.com/a/1190000017034025
version: '3'
services:
mysql:
image: 'mysql:5.7'
container_name: mysql
restart: always
ports:
- '3306:3306'
volumes:
- './mysql/data:/var/lib/mysql'
- './mysql/conf:/etc/mysql/conf.d'
environment:
MYSQL_ROOT_PASSWORD: 123123
MYSQL_DATABASE: mydb
TZ: Asia/Shanghai
注:如果执行后发现mydb数据库没有初始化,需要手动清空 ./mysql/data 文件夹下所有内容
参考:http://www.fecshop.com/topic/1186
mysql/conf:
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
skip-name-resolve
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
连接docker mysql 容器慢的问题:
skip-name-resolve
innodb_flush_log_at_trx_commit=0
sync_binlog=0
方法解析:
skip-name-resolve:跳过域名解析,对有些场景很管用,但对我遇到的不怎么管用。
设置这个以后,速度只有一点点提升(2秒 -> 1秒9)。
innodb_flush_log_at_trx_commit=0: 这个选项决定着什么时候把日志信息写入日志文件以及什么时候把这些文件物理地写(术语称为”同步”)到硬盘上。设置值0的意思是每隔一秒写一次日志并进行 同步,这可以减少硬盘写操作次数,但可能造成数据丢失; 设置值1(设置设置)的意思是在每执行完一条COMMIT命令就写一次日志并进行同步,这可以防止数据丢失,但硬盘写操作可能会很频繁; 设置值2是一般折衷的办法,即每执行完一条COMMIT命令写一次日志,每隔一秒进行一次同步。
设置这个以后,速度有很大提升(1秒9 -> 1秒)。
sync_binlog=0:每经过n次日志写操作就把日志文件写入硬盘一次(对日志信息进行一次同步)。设为1是最安全的做法,但效率最低。docker中默认设置是1,意思是由操作系统来负责二进制日志文件的同步工作。
设置这个以后,速度有显著提升(1秒 -> 30毫秒)
参考:https://blog.csdn.net/chunzhenzyd/article/details/82887269