此處略,詳見html
此環境使用docker-compose來編排各個容器的的配置,包括構建django鏡像,項目目錄結構以下圖:python
version: '3' services: ### Django Project-1 container ######################################### book: build: #構建django項目book容器的路徑 context: ./build/django volumes: #項目數據持久化配置---包括項目、uwsgi配置文件 - ${PROJECT_FOLDER}/book:/usr/src/app/book:rw - ./work/components/uwsgi/book/uwsgi.ini:/etc/uwsgi/book/uwsgi.ini:rw restart: always #系統重啓後容器自動啓動 privileged: true #容器啓動後是否有系統權限 networks: #定義該容器的網橋名稱和IP地址 net-django: ipv4_address: 10.127.2.5 command: uwsgi --ini /etc/uwsgi/book/uwsgi.ini #容器啓動後執行的命令,必需要此命令,以項目uwsgi配置文件啓動uwsgi ### Django Project-2 container ######################################### hos: build: #構建django項目hos容器的路徑 context: ./build/django volumes: #項目數據持久化配置---包括項目、uwsgi配置文件 - ${PROJECT_FOLDER}/hos:/usr/src/app/hos:rw - ./work/components/uwsgi/hos/uwsgi.ini:/etc/uwsgi/hos/uwsgi.ini:rw restart: always #系統重啓後容器自動啓動 privileged: true #容器啓動後是否有系統權限 networks: #定義該容器的網橋名稱和IP地址 net-django: ipv4_address: 10.127.2.4 command: uwsgi --ini /etc/uwsgi/hos/uwsgi.ini #容器啓動後執行的命令,必需要此命令,以項目uwsgi配置文件啓動uwsgi ### Nginx container ######################################### nginx: image: nginx:1.17-alpine #定義nginx使用的鏡像,直接從docker hub上獲取 ports: #定義nginx開放的web端口 80和8080 - "80:80" - "8080:8080" volumes: #nginx數據持久化配置---包括項目、項目nginx配置文件和nginx日誌文件 - ${PROJECT_FOLDER}/:/usr/src/app:rw - ./work/components/nginx/config/nginx.conf:/etc/nginx/nginx.conf:rw #windows須要rw權限才能開啓nginx服務 - ./work/components/nginx/config/conf.d:/etc/nginx/conf.d:rw - ./work/components/nginx/log:/var/log/nginx:rw restart: always #系統重啓後容器自動啓動 privileged: true #容器啓動後是否有系統權限 networks: #定義nginx容器的網橋名稱和IP地址 net-django: ipv4_address: 10.127.2.3 ### Mariadb container ######################################### mariadb: image: mariadb:10.4.7 #定義mariadb使用的鏡像,直接從docker hub上獲取 ports: #定義mariadb開放的數據庫端口 根據.env配置變量獲取 ${MYSQL_PORT}端口 - "${MYSQL_PORT}:3306" # ${MYSQL_PORT}表示地址映射後的端口,3306表示mariadb容器內的端口 volumes: #mariadb數據持久化配置---包括數據庫存儲位置、數據庫配置文件和數據庫日誌文件路徑 - ./work/components/mariadb/data:/var/lib/mysql:rw - ./work/components/mariadb/config/mysql.cnf:/etc/mysql/conf.d/mysql.cnf:rw - ./work/components/mariadb/log:/var/log/mysql:rw restart: always #系統重啓後容器自動啓動 privileged: true #容器啓動後是否有系統權限 environment: MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD} #經過系統變量${MYSQL_PASSWORD} 設置mariadb鏡像定義的變量MYSQL_ROOT_PASSWORD networks: #定義mariadb容器的網橋名稱和IP地址 net-django: ipv4_address: 10.127.2.2 ### Add Network Bridge info ######################################### networks: #定義整個docker-compose編排的容器使用的網橋名稱和IP地址網段,注意 x.x.x.1不能使用 net-django: ipam: config: - subnet: 10.127.2.0/24
#定義該鏡像使用的基礎鏡像爲:python:3.7.4-stretch FROM python:3.7.4-stretch #定義容器的工做目錄 WORKDIR /usr/src/app #拷貝pip安裝項目所須要的包名稱和第三方django xadmin管理後臺 COPY requirements.txt ./ COPY xadmin-django2.zip ./ #安裝 RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple && \ pip install ./xadmin-django2.zip #開放的端口 EXPOSE 8000
# The MySQL Client configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysql] [mysqld] sql-mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" innodb_force_recovery = 1 character-set-server=utf8 explicit_defaults_for_timestamp=true
說明:uwsgi是經過pip install uwsgi方式安裝的mysql
[uwsgi] uid = www-data gid = www-data master = true ;django項目目錄路徑 chdir = /usr/src/app/book/ ;項目的wsgi測試服務器配置文件位置 wsgi-file = /usr/src/app/book/book/wsgi.py module = hos.wsgi ;定義socket的地址和端口 socket = 10.127.2.5:8000 processes = 8 threads = 1 enable-threads = true chmod-socket = 664 chown-socket = book:www-data vacuum = true ;定義靜態文件 static-map=/static=/usr/src/app/book/static buffer-size = 65535
[uwsgi] uid = www-data gid = www-data master = true ;django項目目錄路徑 chdir = /usr/src/app/hos/ ;項目的wsgi測試服務器配置文件位置 wsgi-file = /usr/src/app/book/hos/wsgi.py module = hos.wsgi ;定義socket的地址和端口 socket = 10.127.2.4:8000 processes = 8 threads = 1 enable-threads = true chmod-socket = 664 chown-socket = hos:www-data vacuum = true ;定義靜態文件 static-map=/static=/usr/src/app/hos/static buffer-size = 65535
user nginx; worker_processes 1; pid /var/run/nginx.pid; error_log /var/log/nginx/error.log warn; 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 /dev/null; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
server { client_max_body_size 1024m; listen 80; server_name localhost; charset utf-8; location / { include uwsgi_params; #定義uwsgi的socket,即django項目容器的地址和端口 uwsgi_pass 10.127.2.5:8000; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } #定義項目靜態文件目錄,注意alias和root的區別 location /static { alias /usr/src/app/book/static; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { client_max_body_size 1024m; listen 8080; server_name localhost; charset utf-8; location / { include uwsgi_params; #定義uwsgi的socket,即django項目容器的地址和端口 uwsgi_pass 10.127.2.4:8000; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } #定義項目靜態文件目錄,注意alias和root的區別 location /static { alias /usr/src/app/hos/static; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
6.django項目配置文件nginx
(1) book/book/setting.py文件中數據庫設置git
DATABASES = { 'default': { # 鏈接的數據庫類型 'ENGINE': 'django.db.backends.mysql', # 數據庫的地址 'HOST': '10.127.2.2', # 數據庫使用的端口 'PORT': '3306', # 所鏈接的數據庫名字 'NAME': 'book', # 鏈接數據庫的用戶名 'USER': 'root', # 鏈接數據庫的密碼 'PASSWORD': 'DockerLNMP' } }
(2) book/book/setting.py文件中靜態文件設置github
STATIC_URL = '/static/' STATIC_ROOT=os.path.join(BASE_DIR,"/static/") STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'),
(3)項目的wsgi.py文件設置web
import os import sys from django.core.wsgi import get_wsgi_application sys.path.append("/usr/src/app/book/") # os.environ.setdefault("DJANGO_SETTINGS_MODULE", "book.settings") os.environ["DJANGO_SETTINGS_MODULE"] = "book.settings" application = get_wsgi_application()
import os import sys from django.core.wsgi import get_wsgi_application sys.path.append("/usr/src/app/hos/") # os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hos.settings') os.environ["DJANGO_SETTINGS_MODULE"] = "hos.settings" application = get_wsgi_application()
4、啓動與中止sql
1.啓動-構建鏡像&&啓動容器docker
docker-compose up 或者 docker-compose up -d 後臺運行
2.中止數據庫
docker-compose stop #中止容器的運行
3.中止並刪除容器
docker-compose down
4.收集django項目的靜態文件
python manage.py collectstatic 收集靜態文件
django項目使用 python manage.py collectstatic 命令收集靜態文件
注意:成功收集靜態文件後,重啓django、nginx容器或者重啓與此項目相關的全部容器,docker-compose restart 容器名稱 或者 docker-compose restart
4.其餘
(1)docker images 或者 docker image ls 查看本地的鏡像
(2)docker ps -a
經過以上信息得出: