FROM python:3.7 RUN mkdir /code COPY requirements.txt /code/ WORKDIR /code RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
#指定容器内安装的Django版本 Django==2.2.6 #容器内安装uwsgi uwsgi
docker build -t Django:1.0 .
version: '3' services: django: image: django:1.0 container_name: django restart: always ports: - 8200:8200 - 8222:8888 command: ping 127.0.0.1 #是容器一直存在不退出 volumes: #在容器内部生成mydjango应用时所生成的文件中的配置文件 - ./settings.py:/code/mydjango/mydjango/settings.py #uwsgi的配置文件,与Django应用的manage.py处于同级目录 启动uwsgi时用 - ./uwsgi.ini:/code/mydjango/uwsgi.ini
#进入Django容器 docker exec -it django /bin/bash #生成Django应用 django-admin startproject mydjango #进入项目文件夹 cd /code/mydjango #启动应用 python manage.py runserver 0.0.0.0:8200
[uwsgi] #服务端口此处端口写容器内端口 http = 0.0.0.0:8888 #指定与Nginx通信的方式,不影响uwsgi本身运行。如果配置了需要到nginx中进行相关配置-才能通过nginx访问Django # socket = 127.0.0.1:8001 # 启动一个master进程,来管理其余的子进程 master = True processes = 4 threads = 2 #python虚拟环境目录绝对路径。如果有的话,home是虚拟环境根目录,PYTHNONHOME是虚拟环境下的bin目录(放置了Python执行文件) #home = /env #PYTHONHOME = /env/bin #django项目目录,与manager.py同级 chdir = /code/mydjango #主应用中的wsgi,下面这种配法是在Django根目录下运行uwsgi有效,主APP名为有settings.py的那个目录名。如果是其他目录运行,下面建议写成绝对路径。 wsgi-file = /code/mydjango/mydjango/wsgi.py #服务停止时自动移除unix Socket和pid文件 vacuum = true #设置每个工作进程处理请求的上限,达到上限时,将回收(重启)进程,可以预防内存泄漏 max-requests=5000 #设置后台运行保存日志。只要配置了daemonize就会让uwsgi后台运行,同时将日志输出到指定目录 daemonize=/code/mydjango/uwsgi.log #保存主进程的pid,用来控制uwsgi服务 pidfile=/code/mydjango/uwsgi.pid #uwsgi --stop/reload xxx.pid 停止/重启uwsgi #静态文件映射 #static-map = /static=Django下static目录的绝对路径
进入容器内,启动uwsgi
#进入容器内 docker exec -it django /bin/bash #进入项目文件夹 cd /code/mydjango #启动uwsgi uwsgi --ini uwsgi.ini
version: '3' services: nginx-uwsgi: image: nginx container_name: nginx-uwsgi restart: always ports: - 8333:80 volumes: - ./nginx:/etc/nginx - ./html:/usr/share/nginx/html #将nginx文件夹整个挂载出来 - ./logs:/var/log/nginx - /etc/localtime:/etc/localtime:ro
docker-compose up -d
#配置负载均衡 upstream uwsgi { #配置主机名和端口 server ip:8222; } server { #配置监听 listen 80; listen [::]:8333; #配置服务名 server_name ; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { #引入配置文件 uwsgi_params; #规定访问协议 uwsgi_pass uwsgi; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; # location /static { # alias /code/mydjango/; #} # location = /50x.html { # root /usr/share/nginx/html; # } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
[uwsgi] #服务端口 #http = 0.0.0.0:8888 #指定与Nginx通信的方式,不影响uwsgi本身运行。如果配置了需要到nginx中进行相关配置-才能通过nginx访问Django socket = 0.0.0.0:8888 # 启动一个master进程,来管理其余的子进程 master = True processes = 4 threads = 2 #python虚拟环境目录绝对路径。如果有的话,home是虚拟环境根目录,PYTHNONHOME是虚拟环境下的bin目录(放置了Python执行文件) #home = /env #PYTHONHOME = /env/bin #django项目目录,与manager.py同级 chdir = /code/mydjango #主应用中的wsgi,下面这种配法是在Django根目录下运行uwsgi有效,主APP名为有settings.py的那个目录名。如果是其他目录运行,下面建议写成绝对路径。 wsgi-file = /code/mydjango/mydjango/wsgi.py #服务停止时自动移除unix Socket和pid文件 vacuum = true #设置每个工作进程处理请求的上限,达到上限时,将回收(重启)进程,可以预防内存泄漏 max-requests=5000 #设置后台运行保存日志。只要配置了daemonize就会让uwsgi后台运行,同时将日志输出到指定目录 daemonize=/code/mydjango/uwsgi.log #保存主进程的pid,用来控制uwsgi服务 pidfile=/code/mydjango/uwsgi.pid #uwsgi --stop/reload xxx.pid 停止/重启uwsgi #静态文件映射 #static-map = /static=Django下static目录的绝对路径 buffer-size = 65536
upstream uwsgi { server ip:8222; } server { listen 80; listen [::]:8333; server_name ip; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { include uwsgi_params; uwsgi_pass uwsgi; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; # location /static { # alias /code/mydjango/; #} # location = /50x.html { # root /usr/share/nginx/html; # } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }