失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Docker部署Django+Mysql+uWSGI+Nginx Web应用 - 笔记更新-01-04

Docker部署Django+Mysql+uWSGI+Nginx Web应用 - 笔记更新-01-04

时间:2020-08-16 02:23:32

相关推荐

Docker部署Django+Mysql+uWSGI+Nginx Web应用 - 笔记更新-01-04

Docker 部署Django+Mysql+uWSGI+Nginx Web应用

Change logs更新记录

/1/4 fixed web access problem of wide area network(如下)

/12 draft notes released

Docker 启动容器时,将端口 -p 80:80 改为 -p 8001:80(解决外网访问问题, 运营商限制了80端口,所以导致外网访问一直失败,内网访问ok)更新了uwsgi.ini配置文件,更新了nginx配置文件

主机环境

HP workstation个人笔记本电脑

OS:ubuntu 20.04 LTS

宿主机工程目录

root@HP-ZBook-Fury-17:~# tree -d

/root/code

/root/docker

.

├── code

│ └── datacube # django项目名

│ ├── apps

│ ├── datacube

│ │ └──pycache

│ ├── extra_apps

│ ├── media

│ ├── static

│ │ └── admin

│ │ ├── css

│ │ │ └── vendor

│ │ │ └── select2

│ │ ├── fonts

│ │ ├── img

│ │ │ └── gis

│ │ └── js

│ │ ├── admin

│ │ └── vendor

│ │ ├── jquery

│ │ ├── select2

│ │ │ └── i18n

│ │ └── xregexp

│ └── templates

├── docker #

│ ├── django-uwsgi-nginx

│ └── mysql

│ ├── conf.d

│ └── data

│ ├── datacube

│ ├── mysql

│ ├── performance_schema

│ └── sys

宿主机上安装Docker

省略…网上有很多教程供参考

创建Mysql容器并启动

简单说明一下, docker run就是运行容器的命令,如果本地仓库没有mysql5.7.19镜像那么docker会自动从docker hub pull 也就是下载我们制定的mysql版本

docker run 参数大白话说明

-d : 后台运行

-v : 卷挂载 本地目录:容器目录

-e : 配置环境(这边我们配置了mysql密码 和 数据库名)

-p : 端口

我的目录root@HP-ZBook-Fury-17:~/docker/mysql# lsconf.d data start.shmysql│ ├── conf.d│ └── data│ ├── datacube│ ├── mysql│ ├── performance_schema│ └── sys

文件start.sh

#!/bin/bash echo "create a mysql container.."docker run -d --name mysql \-v /root/docker/mysql/conf.d:/etc/mysql/conf.d \-v /root/docker/mysql/data:/var/lib/mysql \-e MYSQL_ROOT_PASSWORD="my-secret-password" \-e MYSQL_DATABASE="datacube" \-p 3306:3306 mysql:5.7.19 \--character-set-server=utf8 --collation-server=utf8_general_ci

文件f内容

[client]default-character-set=utf8[mysqld]character-set-server=utf8performance_schema = OFF[mysql]no-auto-rehashdefault-character-set=utf8

到这里就可以运行了

/root/docker/mysql/start.sh

然后docker查看容器已经成功运行

root@HP-ZBook-Fury-17:/# docker psCONTAINER ID IMAGE COMMAND CREATEDSTATUS PORTSNAMES38baba6f3f5b mysql:5.7 "docker-entrypoint.s…" 2 days ago Up 2 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql

进入mysql容器测试一下

docker ps 查看容器id, docker exec -it 38baba6f3f5b /bin/bash进入容器,mysql -u root -p

登录mysql, 最后show databases;

root@HP-ZBook-Fury-17:/# docker psCONTAINER ID IMAGE COMMAND CREATEDSTATUS PORTSNAMES38baba6f3f5b mysql:5.7 "docker-entrypoint.s…" 2 days ago Up 2 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysqlroot@HP-ZBook-Fury-17:/# docker exec -it 38baba6f3f5b /bin/bashroot@38baba6f3f5b:/# mysql -u root -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.36 MySQL Community Server (GPL)Copyright (c) 2000, , Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || datacube || mysql || performance_schema || sys|+--------------------+5 rows in set (0.04 sec)

创建Python+Django+Nginx+uWSGI镜像并启动测试

该容器需要与上面创建的mysql容器互联,这里通过更新环境变量的方式来做,因此需要更新django项目下的settings.py 如下:

目录:/root/code/datacube/datacube/settings.py

DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': os.environ.get('MYSQL_DATABASE_NAME'),'USER': 'root','PASSWORD': os.environ.get('MYSQL_ENV_MYSQL_ROOT_PASSWORD'),'HOST': os.environ.get('MYSQL_PORT_3306_TCP_ADDR'),'OPTIONS': {"init_command": "SET foreign_key_checks=0;",}}}

配置Dockerfile, nginx, uwsgi, requirements.txt, soruces.list

目录

├── Dockerfile

├── nginx-app.conf

├── requirements.txt

├── sources.list

├── uwsgi.ini

文件Dockerfile

FROM ubuntu:18.04MAINTAINER matt<751749090@># 安装git、python、nginx、.等,并清理缓存COPY sources.list /etc/apt/sources.listRUNapt-get update \&& apt-get install gcc -y\&& apt-get install g++ -y\&& apt-get install gdb -y\&& apt-get install python-software-properties -y\&& apt-get install software-properties-common -y\&& add-apt-repository ppa:deadsnakes/ppa -y\&& apt-get update\&& apt-get install python3.6 -y\&& rm /usr/bin/python\&& ln -s /usr/bin/python3.6 /usr/bin/python\&& rm /usr/bin/python3\&& ln -s /usr/bin/python3.6 /usr/bin/python3\&& apt-get install python3-pip -y\&& pip3 install pip -U\&& rm /usr/bin/pip3 \&& ln -s -f /usr/local/bin/pip3 /usr/bin/pip3\&& ln -s -f /usr/local/bin/pip3 /usr/bin/pip\&& pip config set global.index-url https://pypi.tuna./simple\&& rm -rf /var/lib/apt/lists/*# 环境变量ENV MYSQL_DATABASE_NAME datacubeENV EMAIL_HOST_USER user@ENV EMAIL_HOST_PASSWORD yourpassword# nginx配置#RUN echo "daemon off;" >> /etc/nginx/nginx.confCOPY nginx-app.conf /etc/nginx/sites-available/default# 安装项目所需python第三方库COPY requirements.txt /home/docker/code/RUN pip install -i /simple/ \-r /home/docker/code/requirements.txtCOPY . /home/docker/code/EXPOSE 80

创建docker镜像,查看结果

root@HP-ZBook-Fury-17:~/docker/django-uwsgi-nginx# docker build -t matt/datacubeimage /root/docker/django-uwsgi-nginx

root@HP-ZBook-Fury-17:/# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmatt/datacubeimage latest 1d1f02ae30c1 About a minute ago 561MB

Docker run 启动容器 (/1/4更新)

docker run -itd --link mysql:mysql -v /root/code/:/home/docker/code/ --name datacubeWebapp -p 8001:80 matt/datacubeimage /bin/bash

成功启动如下:

root@HP-ZBook-Fury-17:~/code# docker run -itd --link mysql:mysql -v /root/code/:/home/docker/code/ --name datacubeWebapp -p 80:80 matt/datacubeimage bash1ac66f1ccbbd3d08c9b9a943021efe26b4cc4b622a5ff7fe96abba9ff15c7e04root@HP-ZBook-Fury-17:~/code# docker psCONTAINER ID IMAGECOMMAND CREATED STATUS PORTSNAMES1ac66f1ccbbd matt/datacubeimage "bash" 6 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp datacubeWebapp38baba6f3f5b mysql:5.7 "docker-entrypoint.s…" 2 days agoUp 5 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql

进入容器

docker exec -it 1ac66f1ccbbd /bin/bash

查看通过build image时通过编写dockerfile命令给 python安装的依赖包、库

命令:pip list

root@1ac66f1ccbbd:/# pip listPackage Version-------------------- ---------------asn1crypto 0.24.0cryptography 2.1.4defusedxml 0.7.1diff-match-patchDjango2.2django-crispy-forms 1.13.0django-formtools2.3django-import-export 2.7.0et-xmlfile 1.1.0future0.18.2httplib2 0.20.2idna 2.6keyring 10.6.0keyrings.alt 3.0MarkupPy 1.14odfpy1.4.1openpyxl 3.0.9pip 21.3.1pycrypto 2.6.1PyGObject 3.26.1pyparsing 3.0.6python-apt 1.6.5+ubuntu0.7pytz .3pyxdg0.25PyYAML6.0SecretStorage 2.3.1setuptools 39.0.1six 1.16.0sqlparse 0.4.2tablib3.1.0unattended-upgrades 0.1wheel0.30.0xlrd 2.0.1xlwt 1.3.0

到这里位置,项目用到的基础镜像,django代码,mysql配置文件以及数据均已挂载好。接下来依次安装配置Nginx, uWSGI…等

安装Nginx

1 - apt-get update

root@1ac66f1ccbbd:/# apt-get updateGet:1 /ubuntu bionic InRelease [242 kB]Get:2 /deadsnakes/ppa/ubuntu bionic InRelease [15.9 kB]Get:3 /ubuntu bionic-security InRelease [88.7 kB]Get:4 /ubuntu bionic-updates InRelease [88.7 kB]Get:5 /deadsnakes/ppa/ubuntu bionic/main amd64 Packages [45.3 kB]Get:6 /ubuntu bionic-backports InRelease [74.6 kB]....Get:37 /ubuntu bionic-proposed/universe Sources [10.9 kB]Get:43 /ubuntu bionic-proposed/universe amd64 Packages [22.4 kB]Fetched 39.5 MB in 14s (2886 kB/s)Reading package lists... Done

2 - apt install nginx

以下部分内容

root@1ac66f1ccbbd:/# apt-get install nginxReading package lists... DoneBuilding dependency treeReading state information... DoneThe following additional packages will be installed:fontconfig-config fonts-dejavu-core geoip-database iproute2 libatm1 libbsd0 libfontconfig1 libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libmnl0libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libpng16-16 libtiff5 libwebp6 libx11-6libx11-data libxau6 libxcb1 libxdmcp6 libxpm4 libxslt1.1 libxtables12 multiarch-support nginx-common nginx-coreSuggested packages:iproute2-doc libgd-tools geoip-bin fcgiwrap nginx-doc ssl-certThe following NEW packages will be installed:fontconfig-config fonts-dejavu-core geoip-database iproute2 libatm1 libbsd0 libfontconfig1 libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libmnl0libnginx-mod-http-geoip li....

3 - 配置nginx.conf (/1/4更新)

我的路径:vim /etc/nginx/sites-available/default

root@1ac66f1ccbbd:/# vim /etc/nginx/sites-available/defaultupstream django {server 172.17.0.3:8000; # docker 容器ip}server {listen80;server_name 192.168.18.116; #宿主机ipcharsetutf-8;access_log /home/docker/code/logs/datacube.access.log;error_log /home/docker/code/logs/datacube.error.log;client_max_body_size 75M;location /media {alias /home/docker/code/datacube/media;}location /static {alias /home/docker/code/datacube/static;}location / {include/etc/nginx/uwsgi_params;uwsgi_pass django;uwsgi_connect_timeout 600;}}

4 - 查看nginx版本 以及 nginx重载,启动,停止命令

查看版本root@1ac66f1ccbbd:/# nginx -vnginx version: nginx/1.14.0 (Ubuntu)重载配置root@1ac66f1ccbbd:/# service nginx reload* Reloading nginx configuration nginx 启动root@1ac66f1ccbbd:/# service nginx start* Starting nginx nginx 停止root@1ac66f1ccbbd:/# service nginx stop* Stopping nginx nginx 重启root@1ac66f1ccbbd:/# service nginx restart* Restarting nginx nginx

安装uWSGI 以及常用命令

pip install uwsgi

1 - uwsgi安装使用pip install uwsgi

root@1ac66f1ccbbd:/# pip install uwsgiCollecting uwsgiDownloading uwsgi-2.0.20.tar.gz (804 kB)|################################| 804 kB 148 kB/sPreparing metadata (setup.py) ... doneBuilding wheels for collected packages: uwsgiBuilding wheel for uwsgi (setup.py) ... doneCreated wheel for uwsgi: filename=uWSGI-2.0.20-cp36-cp36m-linux_x86_64.whl size=513838 sha256=8c25ddf9b1499c0481373fbdf20575afdba3acaaf4afde1ca0cf570b637b370aStored in directory: /root/.cache/pip/wheels/4a/60/83/bd5b22ba1a9298cc00c9f6400101b3757d958e10b1b38348f8Successfully built uwsgiInstalling collected packages: uwsgiSuccessfully installed uwsgi-2.0.20

2 - uwsgi.ini 配置文件 (/1/4更新)

我的路径:vim /home/docker/code/django-uwsgi-nginx/uwsgi.ini

[uwsgi]socket = 172.17.0.3:8000 # 和nginx通讯的IP端口,要和nginx保持一致#sock path#socket = /home/docker/code/django-uwsgi-nginx/uwsgi.sockmaster = trueprocesses = 4vacuum = truebuffer-size = 60000enable-threads = truechdir = /home/docker/code/datacube#module = datacube.wsgi#module = datacube.wsgi:applicationchmod-socket = 666wsgi-file = /home/docker/code/datacube/datacube/wsgi.py#uwsgi logs pathdaemonize = /home/docker/code/logs/uwsgi.log

截止目前,

Docker部署django web项目的所有环境,配置文件,依赖包/软件如:ubuntu18.04, mysql5.7, python, django, nginx, uwsgi以及django项目的的第三方依赖包库均已安装配置完成。,接下来我们开始测试django web 项目 …

进入容器,启动nginx

root@1ac66f1ccbbd:/# service nginx reload* Reloading nginx configuration nginx

启动uwsgi

root@1ac66f1ccbbd:/# vim /home/docker/code/django-uwsgi-nginx/uwsgi.iniroot@1ac66f1ccbbd:/# uwsgi --ini /home/docker/code/django-uwsgi-nginx/uwsgi.ini[uWSGI] getting INI configuration from /home/docker/code/django-uwsgi-nginx/uwsgi.ini*** Starting uWSGI 2.0.20 (64bit) on [Sat Dec 25 14:00:06 ] ***compiled with version: 7.5.0 on 25 December 11:35:15os: Linux-5.11.0-43-generic #47~20.04.2-Ubuntu SMP Mon Dec 13 11:06:56 UTC nodename: 1ac66f1ccbbdmachine: x86_64clock source: unixdetected number of CPU cores: 16current working directory: /detected binary path: /usr/local/bin/uwsgi!!! no internal routing support, rebuild with pcre support !!!uWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***chdir() to /home/docker/code/datacubeyour memory page size is 4096 bytesdetected max file descriptor number: 1048576lock engine: pthread robust mutexesthunder lock: disabled (you can enable it with --thunder-lock)uwsgi socket 0 bound to TCP address 127.0.0.1:8000 fd 3uWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***Python version: 3.6.9 (default, Dec 8 , 21:08:43) [GCC 8.4.0]Python main interpreter initialized at 0x55e67e2031d0uWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***python threads support enabledyour server socket listen backlog is limited to 100 connectionsyour mercy for graceful operations on workers is 60 secondsmapped 644040 bytes (628 KB) for 4 cores*** Operational MODE: preforking ***WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x55e67e2031d0 pid: 126 (default app)uWSGI running as root, you can use --uid/--gid/--chroot options*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ****** uWSGI is running in multiple interpreter mode ***spawned uWSGI master process (pid: 126)spawned uWSGI worker 1 (pid: 128, cores: 1)spawned uWSGI worker 2 (pid: 129, cores: 1)spawned uWSGI worker 3 (pid: 130, cores: 1)spawned uWSGI worker 4 (pid: 131, cores: 1)

端口映射,路由器里面进行配置

我使用的华为路由器,选择 更多功能 – 安全设置 – NAT服务 --端口映射(外网IP映射到服务器IP地址端口如80:80)

游览器输入外网IP地址测试

出现django欢迎页面,恭喜你环境搭建成功,开始上传你的项目吧

以上,希望能帮到你!

附上打赏码请随意,测试一下。哈哈

如果觉得《Docker部署Django+Mysql+uWSGI+Nginx Web应用 - 笔记更新-01-04》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。