时间:2021-07-01 10:21:17 帮助过:7人阅读
1、初始化一个django项目
1)项目文件
from django.contrib import admin from django.urls import path, include urlpatterns = [ path(‘‘, include(‘demoapp.urls‘)), path(‘admin/‘, admin.site.urls), ]urls.py
from django.urls import path from . import views app_name = ‘demoapp‘ urlpatterns = [ path(‘‘, views.index, name=‘index‘), path(‘celery/‘, views.celery_index, name=‘celery_index‘), path(‘celery/random_add/‘, views.random_add, name=‘celery_random_add‘), path(‘celery/random_mul/‘, views.random_mul, name=‘celery_random_mul‘), path(‘celery/random_xsum/‘, views.random_xsum, name=‘celery_random_xsum‘), ]demoapp/urls.py
import random from django.shortcuts import render from . import tasks def index(request): context = {} return render(request, ‘demoapp/index.html‘, context) def celery_index(request): context = {} return render(request, ‘demoapp/celery_index.html‘, context) def random_add(request): a, b = random.choices(range(100), k=2) tasks.add.delay(a, b) context = {‘function_detail‘: ‘add({}, {})‘.format(a, b)} return render(request, ‘demoapp/celery_detail.html‘, context) def random_mul(request): a, b = random.choices(range(100), k=2) tasks.mul.delay(a, b) context = {‘function_detail‘: ‘mul({}, {})‘.format(a, b)} return render(request, ‘demoapp/celery_detail.html‘, context) def random_xsum(request): array = random.choices(range(100), k=random.randint(1, 10)) tasks.xsum.delay(array) context = {‘function_detail‘: ‘xsum({})‘.format(array)} return render(request, ‘demoapp/celery_detail.html‘, context)demoapp/views.py
2)celery配置文件
# This will make sure the app is always imported when # Django starts so that shared_task will use this app. from .celery import app as celery_app __all__ = [‘celery_app‘]web/__init__.py
import os from celery import Celery # 只要是想在自己的脚本中访问Django的数据库等文件就必须配置Django的环境变量 os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘web.settings‘) # app名字 app = Celery(‘web‘) # 配置celery class Config: BROKER_URL = ‘redis://redis:6379‘ # redis://127.0.0.1:6379 CELERY_RESULT_BACKEND = ‘redis://redis:6379‘ app.config_from_object(Config) # 到各个APP里自动发现tasks.py文件 app.autodiscover_tasks()web/celery.py
# Create your tasks here from celery import shared_task @shared_task def add(x, y): return x + y @shared_task def mul(x, y): return x * y @shared_task def xsum(numbers): return sum(numbers)demoapp/tasks.py
2、nginx容器相关配置文件
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }django-docker\nginx\nginx.conf
server { listen 80; server_name localhost; charset utf-8; client_max_body_size 10M; location /static/ { alias /django_static/; } location / { include uwsgi_params; uwsgi_pass web:8000; } }django-docker\nginx\conf\default.conf
3、web向配置文件
FROM python:3 ENV PYTHONUNBUFFERED=1 RUN mkdir /code WORKDIR /code ADD requirements.txt /code/ RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple # ADD . /code/django-docker\Dockerfile
[uwsgi] socket=:8000 chdir=/code/web module=web.wsgi:application pidfile=/tmp/web-master.pid master=True vacuum=True processes=1 max-requests=5000django-docker\web\uwsgi.ini
4、docker-compose.yml文件
version: ‘3‘ services: mysql: image: mysql:5.7 volumes: - ./mysql:/var/lib/mysql expose: - "3306" restart: always environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=djangodocker - MYSQL_USER=django - MYSQL_PASSWORD=django nginx: image: nginx:alpine volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf - ./nginx/conf:/etc/nginx/conf.d - ./web/staticfiles:/django_static ports: - "80:80" depends_on: - web redis: image: redis:alpine expose: - "6379" restart: always web: build: . # command: python manage.py runserver 0:8000 # ports: # - "8000:8000" command: uwsgi --ini uwsgi.ini working_dir: /code/web volumes: - .:/code expose: - "8000" depends_on: - mysql - redis celery: build: . command: celery -A web worker -l info working_dir: /code/web volumes: - .:/code depends_on: - mysql - redisdocker-compose.yml
1、docker-compose.yml详解
version: ‘3‘ # cocker compose版本号 services: # 顶级配置文件 mysql: # 服务名: 容器建通信、管理容器 image: mysql:5.7 # 引入官方mysql镜像 volumes: - ./mysql:/var/lib/mysql # 把当前文件夹下的 ./mysql文件夹挂载到docker容器 /var/lib/mysql 路径下 expose: - "3306" # 将当前容器的端口3306端口暴露给link到本容器的容器 restart: always # 宿主机重启自动拉起这个docker容器 environment: - MYSQL_ROOT_PASSWORD=root # mysql服务器root密码root - MYSQL_DATABASE=djangodocker # 创建数据库 djangodocker - MYSQL_USER=django # 创建一个用户 django - MYSQL_PASSWORD=django # 用户密码为django nginx: image: nginx:alpine volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf - ./nginx/conf:/etc/nginx/conf.d - ./web/staticfiles:/django_static ports: - "80:80" # 绑定容器的80端口到主机的80端口 depends_on: - web # 必须先启动web容器然才能启动nginx容器 redis: image: redis:alpine expose: - "6379" restart: always web: build: . # command: python manage.py runserver 0:8000 # ports: # - "8000:8000" command: uwsgi --ini uwsgi.ini # 启动uwsgi命令 working_dir: /code/web # 项目工作路径 volumes: - .:/code # 将当前文件夹下所有文件挂载到容器的 /code 文件夹 expose: - "8000" depends_on: # 必须mysql和reids容器启动后才能启动web容器 - mysql - redis celery: build: . command: celery -A web worker -l info working_dir: /code/web volumes: - .:/code depends_on: - mysql - redis
2、相似指令比较
‘‘‘1. expose 与 ports 比较‘‘‘ # ports: 绑定容器的端口到主机的端口,这样就可以在外网访问docker容器的服务 # expose: 将当前容器的端口3暴露给link到本容器的容器,expose不会将端口暴露给主机 ‘‘‘2. depends_on 与 links区别‘‘‘ # depends_on: 指定本容器启动依赖的容器必须先启动 # links: 保证容器如果ip变化也能访问(基本已经弃用,因为不使用link仍然可以通过容器名称访问)
docker-compose部署django+nginx+uwsgi+celery+redis+mysql
标签:agent tab ati dir web 创建数据库 art socket process