• docker部署django(uwsgi+nginx)


    1、目录结构

    在这里插入图片描述

    2、uwsgi.ini

    [uwsgi]
    #使用nginx连接时使用,真实ip,端口不能占用已使用端口
    socket=0.0.0.0:8001
    #直接做web服务器使用
    #http=172.xx.xx.xx:8001
    #项目目录
    chdir=/DjangoProTest
    #项目中wsgi.py文件的目录,相对于项目目录
    wsgi-file=DjangoProTest/wsgi.py
    processes=4
    threads=2
    master=True
    pidfile=uwsgi.pid
    ;daemonize=uwsgi.log
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    注: 最后的daemonize应该注释掉,否则docker容器启动后便会立即停止,docker容器需要一个持续运行的前台进程,否则会终止容器运行

    3、Dockerfile

    # syntax=docker/dockerfile:1
    
    FROM python:3.8-slim-buster
    
    WORKDIR /DjangoProTest
    
    COPY requirement.txt requirement.txt
    
    RUN apt-get update
    
    RUN apt-get install -y gcc
    
    RUN pip3 install -r requirement.txt
    
    RUN pip3 install uwsgi
    
    COPY . .
    
    CMD ["uwsgi", "--ini", "uwsgi.ini"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4、Docker-compose.dev.yml

    version: '3.8'
    
    services:
      django_server:
        build:
          context: .
        image: django_server_image:v1.0.0
    #    command: uwsgi --ini uwsgi.ini
        volumes:
        - ./:/DjangoProTest
        container_name: django_server_project
        restart: always
        ports:
        - 8001:8001
        networks: #定义该容器的网桥名称和IP地址
          net-django:
            ipv4_address: 10.127.2.5
    
      nginx:
        restart: always
        image: nginx
        volumes:
          - ./deploy_conf/nginx/nginx.conf:/etc/nginx/nginx.conf
        ports:
        - 80:80
        - 8000:8000
        networks: #定义该容器的网桥名称和IP地址
          net-django:  # 全局网桥名称
            ipv4_address: 10.127.2.4
    
    networks:   #定义整个docker-compose编排的容器使用的网桥名称和IP地址网段,注意 x.x.x.1不能使用
      net-django:
        ipam:
          config:
            - subnet: 10.127.2.0/24  # ip地址网段
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    5、nginx.conf

    user root;
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    
    events {
    	worker_connections 768;
    	# multi_accept on;
    }
    
    http {
    
    	##
    	# Basic Settings
    	##
    	## 后端
    	server {
    	       listen       8000;
    	       server_name  localhost;  # localhost就行,当访问8000端口会自动映射过来
    	       location / {
    	       	# 配置路径
    	       	    include uwsgi_params;
    				uwsgi_pass 10.127.2.5:8001;  # 项目容器的ip和uwsgi开放的端口
                }
            }
    
    
    	sendfile on;
    	tcp_nopush on;
    	tcp_nodelay on;
    	keepalive_timeout 65;
    	types_hash_max_size 2048;
    	# server_tokens off;
    
    	# server_names_hash_bucket_size 64;
    	# server_name_in_redirect off;
    
    	include /etc/nginx/mime.types;
    	default_type application/octet-stream;
    
    	##
    	# SSL Settings
    	##
    
    	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    	ssl_prefer_server_ciphers on;
    
    	##
    	# Logging Settings
    	##
    
    	access_log /var/log/nginx/access.log;
    	error_log /var/log/nginx/error.log;
    
    	##
    	# Gzip Settings
    	##
    
    	gzip on;
    
    	# gzip_vary on;
    	# gzip_proxied any;
    	# gzip_comp_level 6;
    	# gzip_buffers 16 8k;
    	# gzip_http_version 1.1;
    	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
    	##
    	# Virtual Host Configs
    	##
    
    	include /etc/nginx/conf.d/*.conf;
    	include /etc/nginx/sites-enabled/*;
    }
    
    
    #mail {
    #	# See sample authentication script at:
    #	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    #
    #	# auth_http localhost/auth.php;
    #	# pop3_capabilities "TOP" "USER";
    #	# imap_capabilities "IMAP4rev1" "UIDPLUS";
    #
    #	server {
    #		listen     localhost:110;
    #		protocol   pop3;
    #		proxy      on;
    #	}
    #
    #	server {
    #		listen     localhost:143;
    #		protocol   imap;
    #		proxy      on;
    #	}
    #}
    
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    6、运行docker-compose

    docker-compose -f Docker-compose.dev.yml up --build -d
    
    • 1
  • 相关阅读:
    windows server 2019 、win11安装docker desktop
    ByteX-shrink_r源码解析
    [SQL Server]在应使用条件的上下文(在 ‘)‘ 附近)中指定了非布尔类型的表达式,查询时间大于某个数值时
    利用fiddler抓包工具测试APP及高级应用
    进程基本概念
    汇川AM400高速计数器应用编程(配置+CODESYS源代码)
    一线架构师开发总结:剖析并发编程+JVM性能,深入Tomcat与MySQL,离架构师更近一步!
    【图像处理GIU】图像分割(Matlab代码实现)
    深度强化学习应用实践技巧
    【每天学习一点新知识】CC攻击和DDoS的区别
  • 原文地址:https://blog.csdn.net/weixin_47513022/article/details/126469748