当前位置: 首页 > news >正文

容器化 Djiango 应用程序

1.配置 Nginx 服务器

        1.在项目目录下创建 nginx 目录用于存放 Nginx 配置文件

# 1. 创建主项目目录
mkdir django-nginx-uwsgi-mysql && cd django-nginx-uwsgi-mysql# 2. 创建 Nginx 配置目录及文件
mkdir -p nginx/conf
touch nginx/conf/nginx.conf
touch nginx/conf/django-nginx.conf# 3. 创建 Django+uWSGI 相关目录及文件
mkdir -p django-uwsgi
touch django-uwsgi/Dockerfile
touch django-uwsgi/requirements.txt
touch django-uwsgi/uwsgi.ini# 4. 创建 MySQL 配置目录及文件
mkdir -p db/conf
mkdir -p db/sqls
touch db/conf/mysql_my.cnf# 5. 创建 Docker Compose 配置文件
touch compose.yaml

        2.在 nginx 目录下创建 conf 子目录

[root@host1 ~]# mkdir django-nginx-uwsgi-mysql && cd django-nginx-uwsgi-mysql
[root@host1 django-nginx-uwsgi-mysql]# mkdir -p nginx/conf
[root@host1 django-nginx-uwsgi-mysql]# touch nginx/conf/nginx.conf
[root@host1 django-nginx-uwsgi-mysql]# touch nginx/conf/django-nginx.conf
[root@host1 django-nginx-uwsgi-mysql]# mkdir -p django-uwsgi
[root@host1 django-nginx-uwsgi-mysql]# touch django-uwsgi/Dockerfile
[root@host1 django-nginx-uwsgi-mysql]# touch django-uwsgi/requirements.txt
[root@host1 django-nginx-uwsgi-mysql]# touch django-uwsgi/uwsgi.ini
[root@host1 django-nginx-uwsgi-mysql]# mkdir -p db/conf
[root@host1 django-nginx-uwsgi-mysql]# mkdir -p db/sqls
[root@host1 django-nginx-uwsgi-mysql]# touch db/conf/mysql_my.cnf
[root@host1 django-nginx-uwsgi-mysql]# touch compose.yaml

        3.在 conf 子目录下创建配置文件 nginx.conf 用于定义 Nginx 全局配置,这些配置源于 Nginx
官方镜像中的默认配置文件。nginx.conf 文件的内容如下

[root@host1 django-nginx-uwsgi-mysql]# vi nginx/conf/nginx.conf
[root@host1 django-nginx-uwsgi-mysql]# cat nginx/conf/nginx.conf
user root;
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;keepalive_timeout 65;include /etc/nginx/conf.d/*.conf;
}

        这里的主要目的是将 user 的值从 nginx 改为 root。user 是一个主模块指令,指定运行 Nginx Worker 进程的用户以及用户组

        4.在 conf 子目录下创建配置文件 django-nginx.conf 用于定义 Nginx 的扩展配置,主要定义 Nginx 与 uWsGl 交互的配置,绑定挂载到容器中的 /etc/nginx/cont.d/default.conf  文件中。django-nginx.conf 文件的主要内容如下

[root@host1 django-nginx-uwsgi-mysql]# vi nginx/conf/django-nginx.conf
[root@host1 django-nginx-uwsgi-mysql]# cat nginx/conf/django-nginx.conf
upstream uwsgi {server unix:/code/app.sock;
}
server {listen 80;server_name nginx_srv;charset utf-8;client_max_body_size 75M;location /static {alias /code/static;index index.html index.htm;}location / {uwsgi_pass uwsgi;include /etc/nginx/uwsgi_params;}
}

       其中,upstream 块主要用于配置负载均衡,以及设置一系列的后端服务器。Nginx 的 HTTP upstream 块用于配置客户端到后端服务器的负载均衡,这里用来设置 Nginx 请求转发的目的地。Nginx 将浏览器等发送过来的请求通过 proxy_pass(代理转发)或 uwsgi_pass(uwsgi 转发)指令转发给 Web 应用程序处理,然后把处理的结果返回给浏览器。Web 应用程序与 Nginx 进行交互需要使用 TCP 协议,WSGI 规范和 uwsgi 协议都在 TCP 协议之上工作。upstream 块为后端服务器指定一个名称,块中的 server 指令指定后端服务器的 IP 地址和端口,示例中的后端服务器是 uWSGI。如果 Nginx 和 uWSGI 在同一个服务器上,则可以使用 socket 文件的形式(即使用 UNIX 套接字)定义 uWSGI,这种方式的开销更小。对于负载均衡,往往要使用多个 server 指令指定多个后端服务器,而且可以设置调度算法。 server 块主要用于虚拟主机配置,如指定主机和端口。其中的 location 块设置 URL 匹配特定位置。location 块支持正则表达式匹配,也支持条件判断匹配,可以实现 Nginx 对动态内容和静态内容的过滤处理。使用 location 块的 URL 匹配配置可以实现反向代理或负载均衡。在此例中,设置匹配路径/static 的为静态内容,匹配路径/的为动态内容,并将请求转交给 uWSGI 处理,其中 uwsgi_pass 指令配置 UWSGI 服务器(示例中是由 upstream 块定义的 uwsgi),表示动态内容请求是通过由 uwsgi 指定的 uWSGI 处理。注意,这里的定义要与 uWSGI 服务器的 wsgi.ini 配置文件(后面会详细介绍)中的 socket 参数保持一致。另外,使用 include 指令嵌入的 uwsgi_params 文件包含 uwsgi 的请求参数,Nginx 官方镜像中已经提供该文件(/etc/nginx/uwsgi_params)。

2.配置 Django 与 uWSGI

        1.在项目日录下创建 django-uwsgi 目录,在该目录中创建 Dockerile

[root@host1 django-nginx-uwsgi-mysql]# vi django-uwsgi/Dockerfile
[root@host1 django-nginx-uwsgi-mysql]# cat django-uwsgi/Dockerfile
FROM python:3.11.4
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY ./requirements.txt /code
RUN pip install --upgrade pip \&& pip install -r requirements.txt

        2.基于 Python 官方镜像安表的较件具体由 requirements.txt 文件定义

[root@host1 django-nginx-uwsgi-mysql]# vi django-uwsgi/requirements.txt
[root@host1 django-nginx-uwsgi-mysql]# cat django-uwsgi/requirements.txt
django>=4.2.1,<4.3
django-tinymce4-lite
django-bootstrap4
mysqlclient==2.1.1
uwsgi

        3.在 django-uwsgi 日录下创建 uwsgi.ini 文件来配置 uWSGI 服务器

[root@host1 django-nginx-uwsgi-mysql]# vi django-uwsgi/uwsgi.ini
[root@host1 django-nginx-uwsgi-mysql]# cat django-uwsgi/uwsgi.ini
[uwsgi]
socket = /code/app.sock
master = true
processes = 4
threads = 2
chdir = /code
module = myexample.wsgi:application
chmod-socket = 666
env = LANG=en_US.UTF-8
vacuum = true

3.配置 MySQL

        1.在项目目录下创建 db 目录来存放相关配置,再在该目永中创建子目录 conf 用存放 MySQL 配置文件

#见1.1

        2.本例在 conf 子目录中提供一个简单的配置文件 mysq_my.cnt

[root@host1 django-nginx-uwsgi-mysql]# vi db/conf/mysql_my.cnf
[root@host1 django-nginx-uwsgi-mysql]# cat db/conf/mysql_my.cnf
[mysqld]
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4

        3.在 db 目录下创建 sqls 子目录用于存放数提库创建的期不,为简化实验中本例没有提供脚本

#见1.1

4.在 Compose 文件中定义所有的服务

        在项目目录下创建一个名为 compose.yaml 的文件

[root@host1 django-nginx-uwsgi-mysql]# vi compose.yaml
[root@host1 django-nginx-uwsgi-mysql]# cat compose.yaml
services:db:image: mysql/mysql-server:8.0restart: alwaysenvironment:- LANG=C.UTF-8- TZ=Asia/Shanghai- MYSQL_DATABASE=django- MYSQL_ROOT_PASSWORD=django- MYSQL_ROOT_HOST=%ports:- "3306:3306"volumes:- ./db/conf:/etc/my.cnf.d- ./db/sqls:/docker-entrypoint-initdb.d- db_data:/var/lib/mysqlnginx:image: nginxrestart: alwaysvolumes:- ./nginx/nginx.conf:/etc/nginx/nginx.conf- ./nginx/conf/django-nginx.conf:/etc/nginx/conf.d/default.conf- ./app:/code- ./log:/var/log/nginxports:- "8000:80"depends_on:- django-uwsgidjango-uwsgi:build: ./django-uwsgirestart: alwayscommand: uwsgi --ini /etc/uwsgi/uwsgi.inivolumes:- ./django-uwsgi:/etc/uwsgi/uwsgi.ini- ./app:/code- ./log/uwsgi:/var/log/uwsgidepends_on:- db
volumes:db_data:
[root@host1 django-nginx-uwsgi-mysql]# tree .
.
├── compose.yaml
├── db
│   ├── conf
│   │   └── mysql_my.cnf
│   └── sqls
├── django-uwsgi
│   ├── Dockerfile
│   ├── requirements.txt
│   └── uwsgi.ini
└── nginx└── conf├── django-nginx.conf└── nginx.conf6 directories, 7 files

5.创建并运行应用程序

        1.在项目目录下执行以下命令完成服务的构建

[root@host1 django-nginx-uwsgi-mysql]# docker compose build
[+] Building 788.7s (13/13) FINISHED                                                                 => [internal] load local bake definitions                                                      0.0s=> => reading from stdin 597B                                                                  0.0s=> [internal] load build definition from Dockerfile                                            0.0s=> => transferring dockerfile: 420B                                                            0.0s=> [internal] load metadata for docker.io/library/python:3.11.4                                0.9s=> [internal] load .dockerignore                                                               0.0s=> => transferring context: 2B                                                                 0.0s=> [1/6] FROM docker.io/library/python:3.11.4@sha256:85b3d192dddbc96588b719e86991e472b390805a  0.0s=> [internal] load build context                                                               0.0s=> => transferring context: 97B                                                                0.0s=> CACHED [2/6] RUN mkdir /code                                                                0.0s=> CACHED [3/6] WORKDIR /code                                                                  0.0s=> [4/6] RUN apt-get update && apt-get install -y     default-libmysqlclient-dev     build-  609.3s=> [5/6] COPY ./requirements.txt /code                                                         0.0s => [6/6] RUN pip install --upgrade pip     && pip install -r requirements.txt                176.2s => exporting to image                                                                          2.1s => => exporting layers                                                                         2.1s => => writing image sha256:4916b0d8f7ba211677c992a3cbe730e7ef0b6d2bd35a1b4f30c2b664414f76f8    0.0s => => naming to docker.io/library/django-nginx-uwsgi-mysql-django-uwsgi                        0.0s => resolving provenance for metadata file                                                      0.0s 
[+] Building 1/1                                                                                     ✔ django-nginx-uwsgi-mysql-django-uwsgi  Built                                                 0.0s

        2.在项目目录下执行以下命令创建一个 Django 初始项目

[root@host1 django-nginx-uwsgi-mysql]# docker compose run django-uwsgi django-admin startproject myexample .

        3.在项目目录下执行以下命令启动整个应用程序

[root@host1 django-nginx-uwsgi-mysql]# docker compose up -d
[+] Running 5/5✔ Network django-nginx-uwsgi-mysql_default           Created                                   0.1s ✔ Volume "django-nginx-uwsgi-mysql_db_data"          Created                                   0.0s ✔ Container django-nginx-uwsgi-mysql-db-1            Healthy                                  11.1s ✔ Container django-nginx-uwsgi-mysql-django-uwsgi-1  Started                                  11.5s ✔ Container django-nginx-uwsgi-mysql-nginx-1         Started                                  12.2s

        4.修改 Django 项目的设置文件

[root@host1 django-nginx-uwsgi-mysql]#  cat app/myexample/settings.py
"""
Django settings for myexample project.Generated by 'django-admin startproject' using Django 4.2.24.For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""from pathlib import Path# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-9ff_f#gj*4o2c7ofjx#14y#58b28696mc%@_j(ahzm$5b^qu-g'# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = TrueALLOWED_HOSTS = ['*']# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',
]MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',
]ROOT_URLCONF = 'myexample.urls'TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [],'APP_DIRS': True,'OPTIONS': {'context_processors': ['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},},
]WSGI_APPLICATION = 'myexample.wsgi.application'# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databaseDATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'django','USER': 'root','PASSWORD': 'django','HOST': 'db','PORT': '3307',  # 与 compose.yaml 中主机端口一致}
}# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validatorsAUTH_PASSWORD_VALIDATORS = [{'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',},{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',},{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',},{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',},
]# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/LANGUAGE_CODE = 'en-us'TIME_ZONE = 'UTC'USE_I18N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/STATIC_URL = '/static/'STATIC_ROOT = '/code/static_collected'STATICFILES_DIRS = ['/code/static',
]# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-fieldDEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

        5.打开浏览器访问  http://127.0.0.1:8000,出现下图,说明Django应用程序已经成功部署

[root@host1 django-nginx-uwsgi-mysql]# curl http://localhost:8001<!doctype html><html lang="en-us" dir="ltr"><head><meta charset="utf-8"><title>The install worked successfully! Congratulations!</title><meta name="viewport" content="width=device-width, initial-scale=1"><style>html {line-height: 1.15;}a {color: #19865C;}header {border-bottom: 1px solid #efefef;}body {max-width: 960px;color: #525252;font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", ui-system, sans-serif;margin: 0 auto;}main {text-align: center;}h1, h2, h3, h4, h5, p, ul {padding: 0;margin: 0;font-weight: 400;}header {display: grid;grid-template-columns: auto auto;align-items: self-end;justify-content: space-between;gap: 7px;padding-top: 20px;padding-bottom: 10px;}.logo {font-weight: 700;font-size: 1.375rem;text-decoration: none;}.figure {margin-top: 19vh;max-width: 265px;position: relative;z-index: -9;overflow: visible;}.exhaust__line {animation: thrust 70ms 100 ease-in-out alternate;}.smoke {animation: smoke .1s 70 ease-in-out alternate;}@keyframes smoke {0% {transform: translate3d(-5px, 0, 0);}100% {transform: translate3d(5px, 0, 0);}}.flame {animation: burnInner2 .1s 70 ease-in-out alternate;}@keyframes burnInner2 {0% {transform: translate3d(0, 0, 0);}100% {transform: translate3d(0, 3px, 0);}}@keyframes thrust {0% {opacity: 1;}100% {opacity: .5;}}@media (prefers-reduced-motion: reduce) {.exhaust__line,.smoke,.flame {animation: none;}}h1 {font-size: 1.375rem;max-width: 32rem;margin: 5px auto 0;}main p {line-height: 1.25;max-width: 26rem;margin: 15px auto 0;}footer {display: grid;grid-template-columns: 1fr 1fr 1fr;gap: 5px;padding: 25px 0;position: fixed;box-sizing: border-box;left: 50%;bottom: 0;width: 960px;transform: translateX(-50%);transform-style: preserve-3d;border-top: 1px solid #efefef;}.option {display: grid;grid-template-columns: min-content 1fr;gap: 10px;box-sizing: border-box;text-decoration: none;}.option svg {width: 1.5rem;height: 1.5rem;fill: gray;border: 1px solid #d6d6d6;padding: 5px;border-radius: 100%;}.option p {font-weight: 300;line-height: 1.25;color: #525252;display: table;}.option .option__heading {color: #19865C;font-size: 1.25rem;font-weight: 400;}@media (max-width: 996px) {body, footer {max-width: 780px;}}@media (max-width: 800px) {footer {height: 100%;grid-template-columns: 1fr;gap: 60px;position: relative;padding: 25px;}.figure {margin-top: 10px;}main {padding: 0 25px;}main h1 {font-size: 1.25rem;}header {grid-template-columns: 1fr;padding-left: 20px;padding-right: 20px;}footer {width: 100%;margin-top: 50px;}}@media (min-width: 801px) and (max-height: 730px) {.figure {margin-top: 80px;}}@media (min-width: 801px) and (max-height: 600px) {footer {position: relative;margin: 135px auto 0;}.figure {margin-top: 50px;}}.sr-only {clip: rect(1px, 1px, 1px, 1px);clip-path: inset(50%);height: 1px;overflow: hidden;position: absolute;white-space: nowrap;width: 1px;}</style></head><body><header><a class="logo" href="https://www.djangoproject.com/" target="_blank" rel="noopener">django</a><p>View <a href="https://docs.djangoproject.com/en/4.2/releases/" target="_blank" rel="noopener">release notes</a> for Django 4.2</p></header><main><svg class="figure" viewBox="0 0 508 268" aria-hidden="true"><path d="M305.2 156.6c0 4.6-.5 9-1.6 13.2-2.5-4.4-5.6-8.4-9.2-12-4.6-4.6-10-8.4-16-11.2 2.8-11.2 4.5-22.9 5-34.6 1.8 1.4 3.5 2.9 5 4.5 10.5 10.3 16.8 24.5 16.8 40.1zm-75-10c-6 2.8-11.4 6.6-16 11.2-3.5 3.6-6.6 7.6-9.1 12-1-4.3-1.6-8.7-1.6-13.2 0-15.7 6.3-29.9 16.6-40.1 1.6-1.6 3.3-3.1 5.1-4.5.6 11.8 2.2 23.4 5 34.6z" fill="#2E3B39" fill-rule="nonzero"/><path d="M282.981 152.6c16.125-48.1 6.375-104-29.25-142.6-35.625 38.5-45.25 94.5-29.25 142.6h58.5z" stroke="#FFF" stroke-width="3.396" fill="#6DDCBD"/><path d="M271 29.7c-4.4-10.6-9.9-20.6-16.6-29.7-6.7 9-12.2 19-16.6 29.7H271z" stroke="#FFF" stroke-width="3" fill="#2E3B39"/><circle fill="#FFF" cx="254.3" cy="76.8" r="15.5"/><circle stroke="#FFF" stroke-width="7" fill="#6DDCBD" cx="254.3" cy="76.8" r="12.2"/><path class="smoke" d="M507.812 234.24c0-2.16-.632-4.32-1.58-6.24-3.318-6.72-11.85-11.52-21.804-11.52-1.106 0-2.212.12-3.318.24-.474-11.52-12.956-20.76-28.282-20.76-3.318 0-6.636.48-9.638 1.32-4.74-6.72-14.062-11.28-24.806-11.28-.79 0-1.58 0-2.37.12-.79 0-1.58-.12-2.37-.12-10.744 0-20.066 4.56-24.806 11.28a35.326 35.326 0 00-9.638-1.32c-15.642 0-28.282 9.6-28.282 21.48 0 1.32.158 2.76.474 3.96a26.09 26.09 0 00-4.424-.36c-8.058 0-15.01 3.12-19.118 7.8-3.476-1.68-7.742-2.76-12.324-2.76-12.008 0-21.804 7.08-22.752 15.96h-.158c-9.322 0-17.38 4.32-20.856 10.44-4.108-3.6-10.27-6-17.222-6h-1.264c-6.794 0-12.956 2.28-17.222 6-3.476-6.12-11.534-10.44-20.856-10.44h-.158c-.948-9-10.744-15.96-22.752-15.96-4.582 0-8.69.96-12.324 2.76-4.108-4.68-11.06-7.8-19.118-7.8-1.422 0-3.002.12-4.424.36.316-1.32.474-2.64.474-3.96 0-11.88-12.64-21.48-28.282-21.48-3.318 0-6.636.48-9.638 1.32-4.74-6.72-14.062-11.28-24.806-11.28-.79 0-1.58 0-2.37.12-.79 0-1.58-.12-2.37-.12-10.744 0-20.066 4.56-24.806 11.28a35.326 35.326 0 00-9.638-1.32c-15.326 0-27.808 9.24-28.282 20.76-1.106-.12-2.212-.24-3.318-.24-9.954 0-18.486 4.8-21.804 11.52-.948 1.92-1.58 4.08-1.58 6.24 0 4.8 2.528 9.12 6.636 12.36-.79 1.44-1.264 3.12-1.264 4.8 0 7.2 7.742 13.08 17.222 13.08h462.15c9.48 0 17.222-5.88 17.222-13.08 0-1.68-.474-3.36-1.264-4.8 4.582-3.24 7.11-7.56 7.11-12.36z" fill="#E6E9EE"/><path fill="#6DDCBD" d="M239 152h30v8h-30z"/><path class="exhaust__line" fill="#E6E9EE" d="M250 172h7v90h-7z"/><path class="flame" d="M250.27 178.834l-5.32-8.93s-2.47-5.7 3.458-6.118h10.26s6.232.266 3.306 6.194l-5.244 8.93s-3.23 4.37-6.46 0v-.076z" fill="#AA2247"/></svg><h1>The install worked successfully! Congratulations!</h1><p>You are seeing this page because <a href="https://docs.djangoproject.com/en/4.2/ref/settings/#debug" target="_blank" rel="noopener">DEBUG=True</a> is in your settings file and you have not configured any URLs.</p></main><footer><a class="option" href="https://docs.djangoproject.com/en/4.2/" target="_blank" rel="noopener"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 21c0 .55.45 1 1 1h4c.55 0 1-.45 1-1v-1H9v1zm3-19C8.14 2 5 5.14 5 9c0 2.38 1.19 4.47 3 5.74V17c0 .55.45 1 1 1h6c.55 0 1-.45 1-1v-2.26c1.81-1.27 3-3.36 3-5.74 0-3.86-3.14-7-7-7zm2.85 11.1l-.85.6V16h-4v-2.3l-.85-.6A4.997 4.997 0 017 9c0-2.76 2.24-5 5-5s5 2.24 5 5c0 1.63-.8 3.16-2.15 4.1z"></path></svg><p><span class="option__heading">Django Documentation</span><span class="sr-only">.</span><br>Topics, references, &amp; how-to’s</p></a><a class="option" href="https://docs.djangoproject.com/en/4.2/intro/tutorial01/" target="_blank" rel="noopener"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9.4 16.6L4.8 12l4.6-4.6L8 6l-6 6 6 6 1.4-1.4zm5.2 0l4.6-4.6-4.6-4.6L16 6l6 6-6 6-1.4-1.4z"></path></svg><p><span class="option__heading">Tutorial: A Polling App</span><span class="sr-only">.</span><br>Get started with Django</p></a><a class="option" href="https://www.djangoproject.com/community/" target="_blank" rel="noopener"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M16.5 13c-1.2 0-3.07.34-4.5 1-1.43-.67-3.3-1-4.5-1C5.33 13 1 14.08 1 16.25V19h22v-2.75c0-2.17-4.33-3.25-6.5-3.25zm-4 4.5h-10v-1.25c0-.54 2.56-1.75 5-1.75s5 1.21 5 1.75v1.25zm9 0H14v-1.25c0-.46-.2-.86-.52-1.22.88-.3 1.96-.53 3.02-.53 2.44 0 5 1.21 5 1.75v1.25zM7.5 12c1.93 0 3.5-1.57 3.5-3.5S9.43 5 7.5 5 4 6.57 4 8.5 5.57 12 7.5 12zm0-5.5c1.1 0 2 .9 2 2s-.9 2-2 2-2-.9-2-2 .9-2 2-2zm9 5.5c1.93 0 3.5-1.57 3.5-3.5S18.43 5 16.5 5 13 6.57 13 8.5s1.57 3.5 3.5 3.5zm0-5.5c1.1 0 2 .9 2 2s-.9 2-2 2-2-.9-2-2 .9-2 2-2z"></path></svg><p><span class="option__heading">Django Community</span><span class="sr-only">.</span><br>Connect, get help, or contribute</p></a></footer></body>
</html>

        6.在 app 子目录下添加名为 static 的目录,该目录用于存放静态内容

[root@host1 django-nginx-uwsgi-mysql]# curl -H "Cache-Control: no-cache" http://localhost:8001/static/index.html
<h1>你好!这是一个静态页面!</h1>

        7.完成实验,在项目目录下执行 docker compose down --volume 命令停止并清理上述应用程序和服务

[root@host1 django-nginx-uwsgi-mysql]# docker compose down --volume 
WARN[0000] --volume is deprecated, please use --volumes 
WARN[0000] --volume is deprecated, please use --volumes 
WARN[0000] --volume is deprecated, please use --volumes 
WARN[0000] --volume is deprecated, please use --volumes 
[+] Running 6/6✔ Container django-nginx-uwsgi-mysql-nginx-1         Removed                                   0.3s ✔ Container django-nginx-uwsgi-mysql-django-uwsgi-1  Removed                                  10.4s ✔ Container django-nginx-uwsgi-mysql-db-1            Removed                                   1.4s ✔ Volume django-nginx-uwsgi-mysql_db_data            Removed                                   0.0s ✔ Volume django-nginx-uwsgi-mysql_socket_volume      Removed                                   0.0s ✔ Network django-nginx-uwsgi-mysql_default           Removed                                   0.2s

!!!错误反馈修改过程(节选):

[root@host1 django-nginx-uwsgi-mysql]# docker compose build
[+] Building 332.1s (10/10) FINISHED                                                                 => [internal] load local bake definitions                                                      0.0s=> => reading from stdin 597B                                                                  0.0s=> [internal] load build definition from Dockerfile                                            0.0s=> => transferring dockerfile: 272B                                                            0.0s=> [internal] load metadata for docker.io/library/python:3.11.4                                6.0s=> [internal] load .dockerignore                                                               0.0s=> => transferring context: 2B                                                                 0.0s=> [1/5] FROM docker.io/library/python:3.11.4@sha256:85b3d192dddbc96588b719e86991e472b390805  65.0s=> => resolve docker.io/library/python:3.11.4@sha256:85b3d192dddbc96588b719e86991e472b390805a  0.0s=> => sha256:cd9c1d09c0875beccec67491cf012e11a0935500b407563466a61b3d45efc5a0 7.53kB / 7.53kB  0.0s=> => sha256:de4cac68b6165c40cf6f8b30417948c31be03a968e233e55ee40221553a5e5 49.56MB / 49.56MB  6.1s=> => sha256:35469d11bde33a2f1cc54c04f64451275e09985bebf23a101a51e28a1774f548 2.01kB / 2.01kB  0.0s=> => sha256:d31b0195ec5f04dfc78eca9d73b5d223fc36a29f54ee888bc4e0615b5839e6 24.03MB / 24.03MB  5.9s=> => sha256:9b1fd34c30b75e7edb20c2fd09a9862697f302ef9ae357e521ef3c84d5534e 64.11MB / 64.11MB  9.5s=> => sha256:85b3d192dddbc96588b719e86991e472b390805a754681a38132de1977d8e429 2.14kB / 2.14kB  0.0s=> => sha256:c485c4ba383179db59368a8a4d2df3e783620647fe0b014331c7fd2bd85 211.03MB / 211.03MB  20.5s=> => sha256:9c94b131279a02de1f5c2eb72e9cda9830b128840470843e0761a45d7bebbefe 6.39MB / 6.39MB  7.7s=> => extracting sha256:de4cac68b6165c40cf6f8b30417948c31be03a968e233e55ee40221553a5e570       9.1s=> => sha256:620f733a13b991a9dcc9723b171916754e6835ae3142b72f3c48afeef0720 19.76MB / 19.76MB  10.6s=> => sha256:97b7f725207b98b1dc4c0ff26e533577a0a366d93a06ff6d5a2a01f41ac713da 244B / 244B     10.5s=> => sha256:e1a7d63bdd4545742abf0564007c70981d6408a87f4c33e59528adacb71312d 3.09MB / 3.09MB  11.6s=> => extracting sha256:d31b0195ec5f04dfc78eca9d73b5d223fc36a29f54ee888bc4e0615b5839e692       2.5s=> => extracting sha256:9b1fd34c30b75e7edb20c2fd09a9862697f302ef9ae357e521ef3c84d5534e3f      10.7s=> => extracting sha256:c485c4ba383179db59368a8a4d2df3e783620647fe0b014331c7fd2bd8526e5b      29.8s=> => extracting sha256:9c94b131279a02de1f5c2eb72e9cda9830b128840470843e0761a45d7bebbefe       1.2s=> => extracting sha256:620f733a13b991a9dcc9723b171916754e6835ae3142b72f3c48afeef0720e37       2.7s=> => extracting sha256:97b7f725207b98b1dc4c0ff26e533577a0a366d93a06ff6d5a2a01f41ac713da       0.0s=> => extracting sha256:e1a7d63bdd4545742abf0564007c70981d6408a87f4c33e59528adacb71312d9       1.1s=> [internal] load build context                                                               0.0s=> => transferring context: 186B                                                               0.0s=> [2/5] RUN mkdir /code                                                                      31.6s=> [3/5] WORKDIR /code                                                                         0.0s=> [4/5] COPY ./requirements.txt /code                                                         0.0s=> ERROR [5/5] RUN pip install --upgrade pip     && pip install -r requirements.txt          229.1s
------> [5/5] RUN pip install --upgrade pip     && pip install -r requirements.txt:
5.368 Requirement already satisfied: pip in /usr/local/lib/python3.11/site-packages (23.1.2)
22.34 Collecting pip
55.70   Downloading pip-25.2-py3-none-any.whl (1.8 MB)
141.0      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 49.8 kB/s eta 0:00:00
141.2 Installing collected packages: pip
141.2   Attempting uninstall: pip
141.2     Found existing installation: pip 23.1.2
141.3     Uninstalling pip-23.1.2:
142.0       Successfully uninstalled pip-23.1.2
144.4 Successfully installed pip-25.2
144.4 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
185.8 Collecting django<4.3,>=4.2.1 (from -r requirements.txt (line 1))
194.5   Downloading django-4.2.24-py3-none-any.whl.metadata (4.2 kB)
199.4 Collecting django-tinymce4-lite (from -r requirements.txt (line 2))
199.8   Downloading django_tinymce4_lite-1.8.0-py3-none-any.whl.metadata (6.4 kB)
205.4 Collecting django-bootstrap4 (from -r requirements.txt (line 3))
205.6   Downloading django_bootstrap4-25.2-py3-none-any.whl.metadata (4.2 kB)
228.9 ERROR: Ignored the following versions that require a different python version: 6.0a1 Requires-Python >=3.12
228.9 ERROR: Could not find a version that satisfies the requirement mysqlclient==2.1.1 (from versions: none)
228.9 ERROR: No matching distribution found for mysqlclient==2.1.1
------
Dockerfile:6--------------------5 |     COPY ./requirements.txt /code6 | >>> RUN pip install --upgrade pip \7 | >>>     && pip install -r requirements.txt8 |     --------------------failed to solve: process "/bin/sh -c pip install --upgrade pip     && pip install -r requirements.txt" did not complete successfully: exit code: 1[root@host1 django-nginx-uwsgi-mysql]# vi django-uwsgi/Dockerfile
[root@host1 django-nginx-uwsgi-mysql]# cat django-uwsgi/Dockerfile
FROM python:3.11.4
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /codeRUN apt-get update && apt-get install -y \default-libmysqlclient-dev \  build-essential \            python3-dev                  COPY ./requirements.txt /code
RUN pip install --upgrade pip \&& pip install -r requirements.txt
[root@host1 django-nginx-uwsgi-mysql]# vi django-uwsgi/Dockerfile
[root@host1 django-nginx-uwsgi-mysql]# cat django-uwsgi/Dockerfile
FROM python:3.11.4
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /codeRUN apt-get update && apt-get install -y \default-libmysqlclient-dev \  build-essential \            python3-dev                  COPY ./requirements.txt /code
RUN pip install --upgrade pip \&& pip install -r requirements.txt
[root@host1 django-nginx-uwsgi-mysql]# docker compose build
[+] Building 788.7s (13/13) FINISHED                                                                 => [internal] load local bake definitions                                                      0.0s=> => reading from stdin 597B                                                                  0.0s=> [internal] load build definition from Dockerfile                                            0.0s=> => transferring dockerfile: 420B                                                            0.0s=> [internal] load metadata for docker.io/library/python:3.11.4                                0.9s=> [internal] load .dockerignore                                                               0.0s=> => transferring context: 2B                                                                 0.0s=> [1/6] FROM docker.io/library/python:3.11.4@sha256:85b3d192dddbc96588b719e86991e472b390805a  0.0s=> [internal] load build context                                                               0.0s=> => transferring context: 97B                                                                0.0s=> CACHED [2/6] RUN mkdir /code                                                                0.0s=> CACHED [3/6] WORKDIR /code                                                                  0.0s=> [4/6] RUN apt-get update && apt-get install -y     default-libmysqlclient-dev     build-  609.3s=> [5/6] COPY ./requirements.txt /code                                                         0.0s => [6/6] RUN pip install --upgrade pip     && pip install -r requirements.txt                176.2s => exporting to image                                                                          2.1s => => exporting layers                                                                         2.1s => => writing image sha256:4916b0d8f7ba211677c992a3cbe730e7ef0b6d2bd35a1b4f30c2b664414f76f8    0.0s => => naming to docker.io/library/django-nginx-uwsgi-mysql-django-uwsgi                        0.0s => resolving provenance for metadata file                                                      0.0s 
[+] Building 1/1                                                                                     ✔ django-nginx-uwsgi-mysql-django-uwsgi  Built                                                 0.0s
            0% {transform: translate3d(0, 0, 0);}100% {transform: translate3d(0, 3px, 0);}}@keyframes thrust {0% {opacity: 1;}100% {opacity: .5;}}@media (prefers-reduced-motion: reduce) {.exhaust__line,.smoke,.flame {animation: none;}}h1 {font-size: 1.375rem;max-width: 32rem;margin: 5px auto 0;}main p {line-height: 1.25;max-width: 26rem;margin: 15px auto 0;}footer {display: grid;grid-template-columns: 1fr 1fr 1fr;gap: 5px;padding: 25px 0;position: fixed;box-sizing: border-box;left: 50%;bottom: 0;width: 960px;transform: translateX(-50%);transform-style: preserve-3d;border-top: 1px solid #efefef;}.option {display: grid;grid-template-columns: min-content 1fr;gap: 10px;box-sizing: border-box;text-decoration: none;}.option svg {width: 1.5rem;height: 1.5rem;fill: gray;border: 1px solid #d6d6d6;padding: 5px;border-radius: 100%;}.option p {font-weight: 300;line-height: 1.25;color: #525252;display: table;}.option .option__heading {color: #19865C;font-size: 1.25rem;font-weight: 400;}@media (max-width: 996px) {body, footer {max-width: 780px;}}@media (max-width: 800px) {footer {height: 100%;grid-template-columns: 1fr;gap: 60px;position: relative;padding: 25px;}.figure {margin-top: 10px;}main {padding: 0 25px;}main h1 {font-size: 1.25rem;}header {grid-template-columns: 1fr;padding-left: 20px;padding-right: 20px;}footer {width: 100%;margin-top: 50px;}}@media (min-width: 801px) and (max-height: 730px) {.figure {margin-top: 80px;}}@media (min-width: 801px) and (max-height: 600px) {footer {position: relative;margin: 135px auto 0;}.figure {margin-top: 50px;}}.sr-only {clip: rect(1px, 1px, 1px, 1px);clip-path: inset(50%);height: 1px;overflow: hidden;position: absolute;white-space: nowrap;width: 1px;}</style></head><body><header><a class="logo" href="https://www.djangoproject.com/" target="_blank" rel="noopener">django</a><p>View <a href="https://docs.djangoproject.com/en/4.2/releases/" target="_blank" rel="noopener">release notes</a> for Django 4.2</p></header><main><svg class="figure" viewBox="0 0 508 268" aria-hidden="true"><path d="M305.2 156.6c0 4.6-.5 9-1.6 13.2-2.5-4.4-5.6-8.4-9.2-12-4.6-4.6-10-8.4-16-11.2 2.8-11.2 4.5-22.9 5-34.6 1.8 1.4 3.5 2.9 5 4.5 10.5 10.3 16.8 24.5 16.8 40.1zm-75-10c-6 2.8-11.4 6.6-16 11.2-3.5 3.6-6.6 7.6-9.1 12-1-4.3-1.6-8.7-1.6-13.2 0-15.7 6.3-29.9 16.6-40.1 1.6-1.6 3.3-3.1 5.1-4.5.6 11.8 2.2 23.4 5 34.6z" fill="#2E3B39" fill-rule="nonzero"/><path d="M282.981 152.6c16.125-48.1 6.375-104-29.25-142.6-35.625 38.5-45.25 94.5-29.25 142.6h58.5z" stroke="#FFF" stroke-width="3.396" fill="#6DDCBD"/><path d="M271 29.7c-4.4-10.6-9.9-20.6-16.6-29.7-6.7 9-12.2 19-16.6 29.7H271z" stroke="#FFF" stroke-width="3" fill="#2E3B39"/><circle fill="#FFF" cx="254.3" cy="76.8" r="15.5"/><circle stroke="#FFF" stroke-width="7" fill="#6DDCBD" cx="254.3" cy="76.8" r="12.2"/><path class="smoke" d="M507.812 234.24c0-2.16-.632-4.32-1.58-6.24-3.318-6.72-11.85-11.52-21.804-11.52-1.106 0-2.212.12-3.318.24-.474-11.52-12.956-20.76-28.282-20.76-3.318 0-6.636.48-9.638 1.32-4.74-6.72-14.062-11.28-24.806-11.28-.79 0-1.58 0-2.37.12-.79 0-1.58-.12-2.37-.12-10.744 0-20.066 4.56-24.806 11.28a35.326 35.326 0 00-9.638-1.32c-15.642 0-28.282 9.6-28.282 21.48 0 1.32.158 2.76.474 3.96a26.09 26.09 0 00-4.424-.36c-8.058 0-15.01 3.12-19.118 7.8-3.476-1.68-7.742-2.76-12.324-2.76-12.008 0-21.804 7.08-22.752 15.96h-.158c-9.322 0-17.38 4.32-20.856 10.44-4.108-3.6-10.27-6-17.222-6h-1.264c-6.794 0-12.956 2.28-17.222 6-3.476-6.12-11.534-10.44-20.856-10.44h-.158c-.948-9-10.744-15.96-22.752-15.96-4.582 0-8.69.96-12.324 2.76-4.108-4.68-11.06-7.8-19.118-7.8-1.422 0-3.002.12-4.424.36.316-1.32.474-2.64.474-3.96 0-11.88-12.64-21.48-28.282-21.48-3.318 0-6.636.48-9.638 1.32-4.74-6.72-14.062-11.28-24.806-11.28-.79 0-1.58 0-2.37.12-.79 0-1.58-.12-2.37-.12-10.744 0-20.066 4.56-24.806 11.28a35.326 35.326 0 00-9.638-1.32c-15.326 0-27.808 9.24-28.282 20.76-1.106-.12-2.212-.24-3.318-.24-9.954 0-18.486 4.8-21.804 11.52-.948 1.92-1.58 4.08-1.58 6.24 0 4.8 2.528 9.12 6.636 12.36-.79 1.44-1.264 3.12-1.264 4.8 0 7.2 7.742 13.08 17.222 13.08h462.15c9.48 0 17.222-5.88 17.222-13.08 0-1.68-.474-3.36-1.264-4.8 4.582-3.24 7.11-7.56 7.11-12.36z" fill="#E6E9EE"/><path fill="#6DDCBD" d="M239 152h30v8h-30z"/><path class="exhaust__line" fill="#E6E9EE" d="M250 172h7v90h-7z"/><path class="flame" d="M250.27 178.834l-5.32-8.93s-2.47-5.7 3.458-6.118h10.26s6.232.266 3.306 6.194l-5.244 8.93s-3.23 4.37-6.46 0v-.076z" fill="#AA2247"/></svg><h1>The install worked successfully! Congratulations!</h1><p>You are seeing this page because <a href="https://docs.djangoproject.com/en/4.2/ref/settings/#debug" target="_blank" rel="noopener">DEBUG=True</a> is in your settings file and you have not configured any URLs.</p></main><footer><a class="option" href="https://docs.djangoproject.com/en/4.2/" target="_blank" rel="noopener"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 21c0 .55.45 1 1 1h4c.55 0 1-.45 1-1v-1H9v1zm3-19C8.14 2 5 5.14 5 9c0 2.38 1.19 4.47 3 5.74V17c0 .55.45 1 1 1h6c.55 0 1-.45 1-1v-2.26c1.81-1.27 3-3.36 3-5.74 0-3.86-3.14-7-7-7zm2.85 11.1l-.85.6V16h-4v-2.3l-.85-.6A4.997 4.997 0 017 9c0-2.76 2.24-5 5-5s5 2.24 5 5c0 1.63-.8 3.16-2.15 4.1z"></path></svg><p><span class="option__heading">Django Documentation</span><span class="sr-only">.</span><br>Topics, references, &amp; how-to’s</p></a><a class="option" href="https://docs.djangoproject.com/en/4.2/intro/tutorial01/" target="_blank" rel="noopener"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9.4 16.6L4.8 12l4.6-4.6L8 6l-6 6 6 6 1.4-1.4zm5.2 0l4.6-4.6-4.6-4.6L16 6l6 6-6 6-1.4-1.4z"></path></svg><p><span class="option__heading">Tutorial: A Polling App</span><span class="sr-only">.</span><br>Get started with Django</p></a><a class="option" href="https://www.djangoproject.com/community/" target="_blank" rel="noopener"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M16.5 13c-1.2 0-3.07.34-4.5 1-1.43-.67-3.3-1-4.5-1C5.33 13 1 14.08 1 16.25V19h22v-2.75c0-2.17-4.33-3.25-6.5-3.25zm-4 4.5h-10v-1.25c0-.54 2.56-1.75 5-1.75s5 1.21 5 1.75v1.25zm9 0H14v-1.25c0-.46-.2-.86-.52-1.22.88-.3 1.96-.53 3.02-.53 2.44 0 5 1.21 5 1.75v1.25zM7.5 12c1.93 0 3.5-1.57 3.5-3.5S9.43 5 7.5 5 4 6.57 4 8.5 5.57 12 7.5 12zm0-5.5c1.1 0 2 .9 2 2s-.9 2-2 2-2-.9-2-2 .9-2 2-2zm9 5.5c1.93 0 3.5-1.57 3.5-3.5S18.43 5 16.5 5 13 6.57 13 8.5s1.57 3.5 3.5 3.5zm0-5.5c1.1 0 2 .9 2 2s-.9 2-2 2-2-.9-2-2 .9-2 2-2z"></path></svg><p><span class="option__heading">Django Community</span><span class="sr-only">.</span><br>Connect, get help, or contribute</p></a></footer></body>
</html>
[root@host1 django-nginx-uwsgi-mysql]# docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash
root@48023910cbe5:/code# python manage.py migrate
Traceback (most recent call last):File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 288, in ensure_connectionself.connect()File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in innerreturn func(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 269, in connectself.connection = self.get_new_connection(conn_params)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in innerreturn func(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connectionconnection = Database.connect(**conn_params)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/MySQLdb/__init__.py", line 123, in Connectreturn Connection(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/MySQLdb/connections.py", line 185, in __init__super().__init__(*args, **kwargs2)
MySQLdb.OperationalError: (2002, "Can't connect to server on 'db' (115)")The above exception was the direct cause of the following exception:Traceback (most recent call last):File "/code/manage.py", line 22, in <module>main()File "/code/manage.py", line 18, in mainexecute_from_command_line(sys.argv)File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_lineutility.execute()File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in executeself.fetch_command(subcommand).run_from_argv(self.argv)File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argvself.execute(*args, **cmd_options)File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 458, in executeoutput = self.handle(*args, **options)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 106, in wrapperres = handle_func(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/core/management/commands/migrate.py", line 100, in handleself.check(databases=[database])File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 485, in checkall_issues = checks.run_checks(^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/core/checks/registry.py", line 88, in run_checksnew_errors = check(app_configs=app_configs, databases=databases)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/core/checks/database.py", line 13, in check_database_backendsissues.extend(conn.validation.check(**kwargs))^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/validation.py", line 9, in checkissues.extend(self._check_sql_mode(**kwargs))^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/validation.py", line 14, in _check_sql_modeself.connection.sql_mode & {"STRICT_TRANS_TABLES", "STRICT_ALL_TABLES"}^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__res = instance.__dict__[self.name] = self.func(instance)^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 443, in sql_modesql_mode = self.mysql_server_data["sql_mode"]^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__res = instance.__dict__[self.name] = self.func(instance)^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 399, in mysql_server_datawith self.temporary_connection() as cursor:File "/usr/local/lib/python3.11/contextlib.py", line 137, in __enter__return next(self.gen)^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 704, in temporary_connectionwith self.cursor() as cursor:^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in innerreturn func(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 329, in cursorreturn self._cursor()^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 305, in _cursorself.ensure_connection()File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in innerreturn func(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 287, in ensure_connectionwith self.wrap_database_errors:File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__raise dj_exc_value.with_traceback(traceback) from exc_valueFile "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 288, in ensure_connectionself.connect()File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in innerreturn func(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 269, in connectself.connection = self.get_new_connection(conn_params)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in innerreturn func(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connectionconnection = Database.connect(**conn_params)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/MySQLdb/__init__.py", line 123, in Connectreturn Connection(*args, **kwargs)^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/usr/local/lib/python3.11/site-packages/MySQLdb/connections.py", line 185, in __init__super().__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2002, "Can't connect to server on 'db' (115)")
root@48023910cbe5:/code# exit
exit
[root@host1 django-nginx-uwsgi-mysql]# mkdir app/static
[root@host1 django-nginx-uwsgi-mysql]# echo '<h1>你好!这是一个静态页面!</h1>' > app/static/index.html
[root@host1 django-nginx-uwsgi-mysql]# ^C
[root@host1 django-nginx-uwsgi-mysql]# vi app/myexample/settings.py
[root@host1 django-nginx-uwsgi-mysql]# cat app/myexample/settings.py
"""
Django settings for myexample project.Generated by 'django-admin startproject' using Django 4.2.24.For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""from pathlib import Path# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-9ff_f#gj*4o2c7ofjx#14y#58b28696mc%@_j(ahzm$5b^qu-g'# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = TrueALLOWED_HOSTS = ['*']# Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',
]MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',
]ROOT_URLCONF = 'myexample.urls'TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [],'APP_DIRS': True,'OPTIONS': {'context_processors': ['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',],},},
]WSGI_APPLICATION = 'myexample.wsgi.application'# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databaseDATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'django','USER': 'root','PASSWORD': 'django','HOST': 'db','PORT': '3307',  # 与 compose.yaml 中主机端口一致}
}# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validatorsAUTH_PASSWORD_VALIDATORS = [{'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',},{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',},{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',},{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',},
]# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/LANGUAGE_CODE = 'en-us'TIME_ZONE = 'UTC'USE_I18N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/STATIC_URL = '/static/'STATIC_ROOT = '/code/static_collected'
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-fieldDEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
[root@host1 django-nginx-uwsgi-mysql]# docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash
root@48023910cbe5:/code# python manage.py collectstatic --noinput125 static files copied to '/code/static_collected'.
root@48023910cbe5:/code# ls /code/static_collected/index.html
ls: cannot access '/code/static_collected/index.html': No such file or directory
root@48023910cbe5:/code# exit
exit
[root@host1 django-nginx-uwsgi-mysql]# docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash
root@48023910cbe5:/code# vi /code/myexample/settings.py
bash: vi: command not found
root@48023910cbe5:/code# apt update
Get:1 http://mirrors.aliyun.com/debian bookworm InRelease [151 kB]            
Get:2 http://mirrors.aliyun.com/debian-security bookworm-security InRelease [48.0 kB]         
Get:3 http://mirrors.aliyun.com/debian bookworm-updates InRelease [55.4 kB]        
Get:4 http://deb.debian.org/debian bookworm InRelease [151 kB]                   
Get:5 http://mirrors.aliyun.com/debian bookworm-backports InRelease [59.4 kB]
Get:6 http://mirrors.aliyun.com/debian bookworm/non-free amd64 Packages [102 kB]
Get:7 http://mirrors.aliyun.com/debian bookworm/main amd64 Packages [8791 kB]   
Get:8 http://mirrors.aliyun.com/debian bookworm/contrib amd64 Packages [53.5 kB]
Get:9 http://mirrors.aliyun.com/debian-security bookworm-security/main amd64 Packages [279 kB]
Get:10 http://mirrors.aliyun.com/debian bookworm-updates/main amd64 Packages [6924 B]               
Get:11 http://mirrors.aliyun.com/debian bookworm-backports/contrib amd64 Packages [5856 B]          
Get:12 http://mirrors.aliyun.com/debian bookworm-backports/non-free amd64 Packages [13.3 kB]        
Get:13 http://mirrors.aliyun.com/debian bookworm-backports/main amd64 Packages [296 kB]             
Get:14 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]                            
Get:15 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]                  
Get:16 http://deb.debian.org/debian bookworm/main amd64 Packages [8791 kB]                          
Get:17 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [6924 B]                   
Get:18 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [279 kB]         
Fetched 19.2 MB in 32s (595 kB/s)                                                                   
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
178 packages can be upgraded. Run 'apt list --upgradable' to see them.
N: Repository 'Debian bookworm' changed its 'non-free component' value from 'non-free' to 'non-free non-free-firmware'
N: More information about this can be found online in the Release notes at: https://www.debian.org/releases/bookworm/amd64/release-notes/ch-information.html#non-free-split
root@48023910cbe5:/code# apt install vim -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:libgpm2 libsodium23 vim-common vim-runtime xxd
Suggested packages:gpm ctags vim-doc vim-scripts
The following NEW packages will be installed:libgpm2 libsodium23 vim vim-common vim-runtime xxd
0 upgraded, 6 newly installed, 0 to remove and 178 not upgraded.
Need to get 8979 kB of archives.
After this operation, 41.9 MB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/debian bookworm/main amd64 vim-common all 2:9.0.1378-2+deb12u2 [125 kB]
Get:2 http://mirrors.aliyun.com/debian bookworm/main amd64 libgpm2 amd64 1.20.7-10+b1 [14.2 kB]
Get:3 http://mirrors.aliyun.com/debian bookworm/main amd64 libsodium23 amd64 1.0.18-1 [161 kB]
Get:4 http://mirrors.aliyun.com/debian bookworm/main amd64 vim-runtime all 2:9.0.1378-2+deb12u2 [7027 kB]
Get:5 http://mirrors.aliyun.com/debian bookworm/main amd64 vim amd64 2:9.0.1378-2+deb12u2 [1568 kB]
Get:6 http://mirrors.aliyun.com/debian bookworm/main amd64 xxd amd64 2:9.0.1378-2+deb12u2 [84.1 kB]
Fetched 8979 kB in 5s (1947 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package vim-common.
(Reading database ... 24465 files and directories currently installed.)
Preparing to unpack .../0-vim-common_2%3a9.0.1378-2+deb12u2_all.deb ...
Unpacking vim-common (2:9.0.1378-2+deb12u2) ...
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack .../1-libgpm2_1.20.7-10+b1_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.7-10+b1) ...
Selecting previously unselected package libsodium23:amd64.
Preparing to unpack .../2-libsodium23_1.0.18-1_amd64.deb ...
Unpacking libsodium23:amd64 (1.0.18-1) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../3-vim-runtime_2%3a9.0.1378-2+deb12u2_all.deb ...
Adding 'diversion of /usr/share/vim/vim90/doc/help.txt to /usr/share/vim/vim90/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim90/doc/tags to /usr/share/vim/vim90/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (2:9.0.1378-2+deb12u2) ...
Selecting previously unselected package vim.
Preparing to unpack .../4-vim_2%3a9.0.1378-2+deb12u2_amd64.deb ...
Unpacking vim (2:9.0.1378-2+deb12u2) ...
Selecting previously unselected package xxd.
Preparing to unpack .../5-xxd_2%3a9.0.1378-2+deb12u2_amd64.deb ...
Unpacking xxd (2:9.0.1378-2+deb12u2) ...
Setting up libsodium23:amd64 (1.0.18-1) ...
Setting up libgpm2:amd64 (1.20.7-10+b1) ...
Setting up xxd (2:9.0.1378-2+deb12u2) ...
Setting up vim-common (2:9.0.1378-2+deb12u2) ...
Setting up vim-runtime (2:9.0.1378-2+deb12u2) ...
Setting up vim (2:9.0.1378-2+deb12u2) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
Processing triggers for libc-bin (2.36-9+deb12u1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
root@48023910cbe5:/code# vi /code/myexample/settings.py
root@48023910cbe5:/code# python manage.py collectstatic --noinput
System check identified some issues:WARNINGS:
?: (staticfiles.W004) The directory '/code/app/ststic' in the STATICFILES_DIRS setting does not exist.0 static files copied to '/code/static_collected', 125 unmodified.
root@48023910cbe5:/code# vim /code/myexample/settings.py
root@48023910cbe5:/code# ls -ld /code/app/static
ls: cannot access '/code/app/static': No such file or directory
root@48023910cbe5:/code# exit
exit
[root@host1 django-nginx-uwsgi-mysql]# ls -ld app/static
drwxr-xr-x. 2 root root 24  9月 24 16:43 app/static
[root@host1 django-nginx-uwsgi-mysql]# grep -A 5 "django-uwsgi:" compose.yaml | grep "volumes:" -A 3
[root@host1 django-nginx-uwsgi-mysql]# vi compose.yaml
[root@host1 django-nginx-uwsgi-mysql]# docker compose restart django-uwsgi
[+] Restarting 1/1✔ Container django-nginx-uwsgi-mysql-django-uwsgi-1  Started                                  10.6s 
[root@host1 django-nginx-uwsgi-mysql]# docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash
root@48023910cbe5:/code# ls -ld /code/app/static
ls: cannot access '/code/app/static': No such file or directory
root@48023910cbe5:/code# exit
exit
[root@host1 django-nginx-uwsgi-mysql]# cat compose.yaml
services:db:image: mysql/mysql-server:8.0restart: alwaysenvironment:- LANG=C.UTF-8- TZ=Asia/Shanghai- MYSQL_DATABASE=django- MYSQL_ROOT_PASSWORD=django- MYSQL_ROOT_HOST=%ports:- "3307:3306"volumes:- ./db/conf:/etc/my.cnf.d- ./db/sqls:/docker-entrypoint-initdb.d- db_data:/var/lib/mysqlhealthcheck:test: ["CMD", "mysqladmin", "ping", "-u", "root", "-p$$MYSQL_ROOT_PASSWORD", "--socket=/var/lib/mysql/mysql.sock"]interval: 10stimeout: 5sretries: 5start_period: 30sdjango-uwsgi:build: ./django-uwsgirestart: alwayscommand: >sh -c "mkdir -p /var/run/uwsgi && chmod 777 /var/run/uwsgi && uwsgi --ini /etc/uwsgi/uwsgi.ini"volumes:- ./django-uwsgi/uwsgi.ini:/etc/uwsgi/uwsgi.ini- ./app:/code- socket_volume:/var/run/uwsgi- ./log/uwsgi:/var/log/uwsgidepends_on:db:condition: service_healthynginx:image: nginxrestart: alwaysvolumes:- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf- ./nginx/conf/django-nginx.conf:/etc/nginx/conf.d/default.conf- ./app:/code- socket_volume:/var/run/uwsgi- ./log:/var/log/nginxports:- "8001:80"depends_on:- django-uwsgivolumes:db_data:socket_volume:
[root@host1 django-nginx-uwsgi-mysql]# docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash
root@48023910cbe5:/code# vim /code/myexample/settings.py
root@48023910cbe5:/code# ls -ld /code/static
drwxr-xr-x. 2 root root 24 Sep 24 08:43 /code/static
root@48023910cbe5:/code# python manage.py collectstatic --noinput1 static file copied to '/code/static_collected', 125 unmodified.
root@48023910cbe5:/code# ls /code/static_collected/index.html
/code/static_collected/index.html
root@48023910cbe5:/code# exit
exit
[root@host1 django-nginx-uwsgi-mysql]# curl http://localhost:8001/static/index.html
<!DOCTYPE html>
<html lang="en">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>Page not found at /static/index.html</title><meta name="robots" content="NONE,NOARCHIVE"><style type="text/css">html * { padding:0; margin:0; }body * { padding:10px 20px; }body * * { padding:0; }body { font:small sans-serif; background:#eee; color:#000; }body>div { border-bottom:1px solid #ddd; }h1 { font-weight:normal; margin-bottom:.4em; }h1 span { font-size:60%; color:#666; font-weight:normal; }table { border:none; border-collapse: collapse; width:100%; }td, th { vertical-align:top; padding:2px 3px; }th { width:12em; text-align:right; color:#666; padding-right:.5em; }#info { background:#f6f6f6; }#info ol { margin: 0.5em 4em; }#info ol li { font-family: monospace; }#summary { background: #ffc; }#explanation { background:#eee; border-bottom: 0px none; }pre.exception_value { font-family: sans-serif; color: #575757; font-size: 1.5em; margin: 10px 0 10px 0; }</style>
</head>
<body><div id="summary"><h1>Page not found <span>(404)</span></h1><table class="meta"><tr><th>Request Method:</th><td>GET</td></tr><tr><th>Request URL:</th><td>http://localhost:8001/static/index.html</td></tr></table></div><div id="info"><p>Using the URLconf defined in <code>myexample.urls</code>,Django tried these URL patterns, in this order:</p><ol><li>admin/</li></ol><p>The current path, <code>static/index.html</code>,didn’t match any of these.</p></div><div id="explanation"><p>You’re seeing this error because you have <code>DEBUG = True</code> inyour Django settings file. Change that to <code>False</code>, and Djangowill display a standard 404 page.</p></div>
</body>
</html>
[root@host1 django-nginx-uwsgi-mysql]# vi nginx/conf/django-nginx.conf
[root@host1 django-nginx-uwsgi-mysql]# cat nginx/conf/django-nginx.conf
upstream django {server unix:///var/run/uwsgi/app.sock;
}server {listen 80;server_name localhost;location /static/ {alias /code/static_collected/;expires 30d;add_header Cache-Control "public, max-age=2592000";}location / {include uwsgi_params;uwsgi_pass django;uwsgi_read_timeout 300;}
}
[root@host1 django-nginx-uwsgi-mysql]# grep -A 5 "nginx:" compose.yaml | grep "volumes:" -A 4volumes:- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf- ./nginx/conf/django-nginx.conf:/etc/nginx/conf.d/default.conf
[root@host1 django-nginx-uwsgi-mysql]# vi compose.yaml
[root@host1 django-nginx-uwsgi-mysql]# cat compose.yaml
services:db:image: mysql/mysql-server:8.0restart: alwaysenvironment:- LANG=C.UTF-8- TZ=Asia/Shanghai- MYSQL_DATABASE=django- MYSQL_ROOT_PASSWORD=django- MYSQL_ROOT_HOST=%ports:- "3307:3306"volumes:- ./db/conf:/etc/my.cnf.d- ./db/sqls:/docker-entrypoint-initdb.d- db_data:/var/lib/mysqlhealthcheck:test: ["CMD", "mysqladmin", "ping", "-u", "root", "-p$$MYSQL_ROOT_PASSWORD", "--socket=/var/lib/mysql/mysql.sock"]interval: 10stimeout: 5sretries: 5start_period: 30sdjango-uwsgi:build: ./django-uwsgirestart: alwayscommand: >sh -c "mkdir -p /var/run/uwsgi && chmod 777 /var/run/uwsgi && uwsgi --ini /etc/uwsgi/uwsgi.ini"volumes:- ./django-uwsgi/uwsgi.ini:/etc/uwsgi/uwsgi.ini- ./app:/code- socket_volume:/var/run/uwsgi- ./log/uwsgi:/var/log/uwsgidepends_on:db:condition: service_healthynginx:image: nginxrestart: alwaysvolumes:- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf- ./nginx/conf/django-nginx.conf:/etc/nginx/conf.d/default.conf- ./app:/code- socket_volume:/var/run/uwsgi- ./log:/var/log/nginx- ./app/static_collected:/code/static_collectedports:- "8001:80"depends_on:- django-uwsgivolumes:db_data:socket_volume:
[root@host1 django-nginx-uwsgi-mysql]# grep -A 5 "nginx:" compose.yaml | grep "volumes:" -A 4volumes:- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf- ./nginx/conf/django-nginx.conf:/etc/nginx/conf.d/default.conf
[root@host1 django-nginx-uwsgi-mysql]# cat nginx/conf/django-nginx.conf
upstream django {server unix:///var/run/uwsgi/app.sock;
}server {listen 80;server_name localhost;location /static/ {alias /code/static_collected/;expires 30d;add_header Cache-Control "public, max-age=2592000";}location / {include uwsgi_params;uwsgi_pass django;uwsgi_read_timeout 300;}
}
[root@host1 django-nginx-uwsgi-mysql]# vi nginx/conf/django-nginx.conf
[root@host1 django-nginx-uwsgi-mysql]# cat nginx/conf/django-nginx.conf
upstream django {server unix:///var/run/uwsgi/app.sock;
}server {listen 80;server_name localhost;location /static/ {alias /code/static_collected/;expires 30d;}location / {include uwsgi_params;uwsgi_pass django;}
}
[root@host1 django-nginx-uwsgi-mysql]# docker exec -it django-nginx-uwsgi-mysql-nginx-1 bash
root@4320d7298419:/# cat /etc/nginx/conf.d/default.conf
upstream django {server unix:///var/run/uwsgi/app.sock;
}server {listen 80;server_name localhost;location / {include uwsgi_params;uwsgi_pass django;uwsgi_read_timeout 300;}
}
root@4320d7298419:/# ls -l /code/static_collected/index.html
-rw-r--r--. 1 root root 46 Sep 24 09:02 /code/static_collected/index.html
root@4320d7298419:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@4320d7298419:/# exit
exit
[root@host1 django-nginx-uwsgi-mysql]# docker compose restart nginx
[+] Restarting 1/1✔ Container django-nginx-uwsgi-mysql-nginx-1  Started                                          0.7s 
[root@host1 django-nginx-uwsgi-mysql]# curl -X GET http://localhost:8001/static/index.html --no-cachecurl: option --no-cache: is unknown
curl: try 'curl --help' or 'curl --manual' for more information
[root@host1 django-nginx-uwsgi-mysql]# curl -H "Cache-Control: no-cache" http://localhost:8001/static/index.html
<h1>你好!这是一个静态页面!</h1>
[root@host1 django-nginx-uwsgi-mysql]# docker logs django-nginx-uwsgi-mysql-nginx-1 --tail 20 | grep "/static/index.html"
[root@host1 django-nginx-uwsgi-mysql]# docker exec -it django-nginx-uwsgi-mysql-nginx-1 bash
root@4320d7298419:/# cat /etc/nginx/conf.d/default.conf | grep -A 10 "location /static/"location /static/ {alias /code/static_collected/;expires 30d;}location / {include uwsgi_params;uwsgi_pass django;}
}
root@4320d7298419:/# ls -l /code/static_collected/index.html
-rw-r--r--. 1 root root 46 Sep 24 09:02 /code/static_collected/index.html
root@4320d7298419:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@4320d7298419:/# nginx -s reload
root@4320d7298419:/# exit
exit
[root@host1 django-nginx-uwsgi-mysql]# vi nginx/conf/django-nginx.conf
[root@host1 django-nginx-uwsgi-mysql]# cat nginx/conf/django-nginx.conf
upstream django {server unix:///var/run/uwsgi/app.sock;
}server {listen 80;server_name localhost;location /static/ {alias /code/static_collected/;expires 30d;
add_header Content-Type "text/html; charset=utf-8";}location / {include uwsgi_params;uwsgi_pass django;}
}
[root@host1 django-nginx-uwsgi-mysql]# docker compose restart nginx
[+] Restarting 1/1✔ Container django-nginx-uwsgi-mysql-nginx-1  Started                                          0.7s 
[root@host1 django-nginx-uwsgi-mysql]# vi app/static/index.html
[root@host1 django-nginx-uwsgi-mysql]# cat app/static/index.html
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>静态页面测试</title>
</head>
<body>
<h1>你好!这是一个静态页面!</h1>
</body>
</html>
[root@host1 django-nginx-uwsgi-mysql]# docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash
root@48023910cbe5:/code# python manage.py collectstatic --noinput1 static file copied to '/code/static_collected', 125 unmodified.
root@48023910cbe5:/code# cat /code/static_collected/index.html
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>静态页面测试</title>
</head>
<body>
<h1>你好!这是一个静态页面!</h1>
</body>
</html>
root@48023910cbe5:/code# exit
exit
[root@host1 django-nginx-uwsgi-mysql]# docker compose restart nginx
[+] Restarting 1/1✔ Container django-nginx-uwsgi-mysql-nginx-1  Started                                          0.7s 
[root@host1 django-nginx-uwsgi-mysql]# 
 1001  mkdir django-nginx-uwsgi-mysql && cd django-nginx-uwsgi-mysql1002  mkdir -p nginx/conf1003  touch nginx/conf/nginx.conf1004  touch nginx/conf/django-nginx.conf1005  mkdir -p django-uwsgi1006  touch django-uwsgi/Dockerfile1007  touch django-uwsgi/requirements.txt1008  touch django-uwsgi/uwsgi.ini1009  mkdir -p db/conf1010  mkdir -p db/sqls1011  touch db/conf/mysql_my.cnf1012  touch compose.yaml1013  vi nginx/conf/nginx.conf1014  cat nginx/conf/nginx.conf1015  vi nginx/conf/django-nginx.conf1016  cat nginx/conf/django-nginx.conf1017  vi django-uwsgi/requirements.txt1018  cat django-uwsgi/requirements.txt1019  vi django-uwsgi/Dockerfile1020  cat django-uwsgi/Dockerfile1021  vi django-uwsgi/uwsgi.ini1022  cat django-uwsgi/uwsgi.ini1023  vi db/conf/mysql_my.cnf1024  cat db/conf/mysql_my.cnf1025  vi compose.yaml1026  cat compose.yaml1027  tree .1028  docker compose build1029  vi django-uwsgi/Dockerfile1030  cat django-uwsgi/Dockerfile1031  docker compose build1032  docker compose run django-uwsgi django-admin startproject myexample .1033  [root@host1 django-nginx-uwsgi-mysql]# ss -tuln | grep 33061034  lsof -i :33061035  systemctl stop mysqld1036  vi compose.yaml1037  cat compose.yaml1038  vi app/myexample/settings.py1039  cat app/myexample/settings.py1040  docker compose down1041  docker compose up -d1042  docker compose run django-uwsgi django-admin startproject myexample .1043  ls app/myexample/settings.py1044  vi ls app/myexample/settings.py1045  vi app/myexample/settings.py1046  cat app/myexample/settings.py1047  docker compose down && docker compose up -d1048  vi compose.yaml1049  cat compose.yaml1050  docker compose down --rmi local1051  docker compose up -d1052  lsof -i :80001053  vi compose.yaml1054  cat compose.yaml1055  docker compose down --remove-orphans1056  docker compose up -d1057  docker compose ps1058  docker logs django-nginx-uwsgi-mysql-django-uwsgi-1 --tail 501059  vi django-uwsgi/uwsgi.ini1060  cat django-uwsgi/uwsgi.ini1061  vi compose.yaml1062  cat compose.yaml1063  cat django-uwsgi/Dockerfile1064  vi django-uwsgi/Dockerfile1065  cat django-uwsgi/Dockerfile1066  docker compose down --rmi local --remove-orphans1067  vi vi compose.yaml1068  vi compose.yaml1069  cat compose.yaml1070  docker compose down --remove-orphans1071  docker compose build1072  docker compose up -d1073  docker logs django-nginx-uwsgi-mysql-db-1 --tail 1001074  vi compose.yaml1075  cat compose.yaml1076  docker compose down && docker compose up -d1077  docker compose ps1078  docker logs django-nginx-uwsgi-mysql-django-uwsgi-1 --tail 1001079  cat -A django-uwsgi/uwsgi.ini1080  yum install dos2unix && dos2unix django-uwsgi/uwsgi.ini1081  cat -A django-uwsgi/uwsgi.ini1082  rm -f django-uwsgi/uwsgi.ini1083  vi django-uwsgi/uwsgi.ini1084  cat django-uwsgi/uwsgi.ini1085  cat -A django-uwsgi/uwsgi.ini1086  docker compose down --rmi local --volumes --remove-orphans1087  docker compose build1088  docker compose up -d1089  docker compose ps1090  docker logs django-nginx-uwsgi-mysql-django-uwsgi-1 --tail 501091  vi django-uwsgi/Dockerfile1092  cat django-uwsgi/Dockerfile1093  docker compose down && docker compose up --build -d1094  ps aux | grep "docker build"1095  kill -9 <PID>1096  kill -9 <495233>1097  ps aux | grep -E "docker-compose up --build|docker build|docker builder" | grep -v grep1098  ps aux | grep -E "docker-compose up --build|docker build" | grep -v grep1099  docker builder prune -f1100  docker system prune -f1101  docker compose down && docker compose up --build -d1102  vi django-uwsgi/Dockerfile1103  cat django-uwsgi/Dockerfile1104  docker compose down && docker compose up --build -d1105  docker compose ps1106  docker logs django-nginx-uwsgi-mysql-django-uwsgi-1 --tail 201107  vi django-uwsgi/uwsgi.ini1108  cat django-uwsgi/uwsgi.ini1109  vi nginx/conf/django-nginx.conf1110  cat nginx/conf/django-nginx.conf1111  docker compose down && docker compose up -d1112  docker compose ps1113  curl http://localhost:80011114  vi compose.yaml1115  cat compose.yaml1116  vi django-uwsgi/uwsgi.ini1117  cat django-uwsgi/uwsgi.ini1118  vi nginx/conf/django-nginx.conf1119  cat nginx/conf/django-nginx.conf1120  docker compose down -v && docker compose up -d1121  docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash1122  docker logs django-nginx-uwsgi-mysql-django-uwsgi-1 --tail 301123  vi compose.yaml1124  cat compose.yaml1125  docker compose down -v && docker compose up -d1126  docker compose ps1127  docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash1128  docker exec -it django-nginx-uwsgi-mysql-nginx-1 bash1129  curl http://localhost:80011130  docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash1131  mkdir app/static1132  echo '<h1>你好!这是一个静态页面!</h1>' > app/static/index.html1133  vi app/myexample/settings.py1134  cat app/myexample/settings.py1135  docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash1136  ls -ld app/static1137  grep -A 5 "django-uwsgi:" compose.yaml | grep "volumes:" -A 31138  vi compose.yaml1139  docker compose restart django-uwsgi1140  docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash1141  cat compose.yaml1142  docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash1143  curl http://localhost:8001/static/index.html1144  vi nginx/conf/django-nginx.conf1145  cat nginx/conf/django-nginx.conf1146  grep -A 5 "nginx:" compose.yaml | grep "volumes:" -A 41147  vi compose.yaml1148  cat compose.yaml1149  grep -A 5 "nginx:" compose.yaml | grep "volumes:" -A 41150  cat nginx/conf/django-nginx.conf1151  vi nginx/conf/django-nginx.conf1152  cat nginx/conf/django-nginx.conf1153  docker exec -it django-nginx-uwsgi-mysql-nginx-1 bash1154  docker compose restart nginx1155  curl -X GET http://localhost:8001/static/index.html --no-cache1156  curl -H "Cache-Control: no-cache" http://localhost:8001/static/index.html1157  docker logs django-nginx-uwsgi-mysql-nginx-1 --tail 20 | grep "/static/index.html"1158  docker exec -it django-nginx-uwsgi-mysql-nginx-1 bash1159  vi nginx/conf/django-nginx.conf1160  cat nginx/conf/django-nginx.conf1161  docker compose restart nginx1162  vi app/static/index.html1163  cat app/static/index.html1164  docker exec -it django-nginx-uwsgi-mysql-django-uwsgi-1 bash1165  docker compose restart nginx1166  grep "Oct 10" /var/log/syslog1167  history

http://www.dtcms.com/a/403919.html

相关文章:

  • 营销网站建设企划案例网站建设业务越做越累
  • Java EE、Java SE 和 Spring Boot
  • 两学一做专题网站wordpress 用户密码的加密算法
  • 手写数据结构-- avl树
  • MySQL-事务日志
  • SpringBoot旅游管理系统
  • 永州市城乡建设规划局网站湖南大型网站建设公司
  • 买东西网站有哪些汽车设计公司排名前十强
  • IT 疑难杂症诊疗室:破解常见故障的实战指南​
  • 集团网站建设详细策划广告设计与制作模板
  • OSError: [WinError 182] 操作系统无法运行 %1。 解决办法
  • 部门网站建设的工作领导小组局域网建设简单的影视网站
  • 嵌入式学习(45)-基于STM32F407Hal库的Modbus Slave从机程序
  • 【字符串算法集合】KMP EXKMP Manacher Trie 树 AC 自动机
  • 网站是哪家公司开发的中山网站建设文化价位
  • 织梦网站如何备份教程企业网站建设公司网络
  • 杭州的网站建设公司4s店网站建设方案
  • 如果在自己电脑上运行,没有问题。但是移植到工控机,有问题
  • 网站建设计划方案中国著名的个人网站
  • 漫谈<爬虫与反爬的斗争>之反爬技术全景综述
  • @WebFilter 过滤器的执行顺序
  • 唐山建站方案七台河新闻综合频道直播
  • webpack library
  • 网站如何做背景音乐苏州集团网站建设
  • 建设工程招聘信息网站微信pc版
  • windows系统怎么做ppt下载网站永康外贸网站建设
  • 人工设计图像特征
  • 网站抓取qqwordpress 菜单 导航
  • centos网卡设置问题
  • springboot 自定义注解记录接口访问日志表