Docker容器化部署Python应用(centos+python+redis+mysql+uwsgi+nginx)
一、MySQL容器
拉取步驟
這里我們拉取官方的鏡像,標(biāo)簽為5.7 docker pull mysql:5.7 使用mysql鏡像運(yùn)行容器 docker run -p 3306:3306 --name mymysql -v $PWD/MySQL/conf.d:/etc/mysql/conf.d -v $PWD/MySQL/logs:/logs -v $PWD/MySQL/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 命令說明: -p 3306:3306:將容器的3306端口映射到主機(jī)的3306端口 -v $PWD/conf/my.cnf:/etc/mysql/my.cnf:將主機(jī)當(dāng)前目錄下的conf/my.cnf掛載到容器的/etc/mysql/my.cnf -v $PWD/logs:/logs:將主機(jī)當(dāng)前目錄下的logs目錄掛載到容器的/logs -v $PWD/data:/mysql_data:將主機(jī)當(dāng)前目錄下的data目錄掛載到容器的/mysql_data -e MYSQL_ROOT_PASSWORD=123456:初始化root用戶的密碼 查看容器啟動情況 docker ps -a
View Code
安裝成功后進(jìn)入數(shù)據(jù)庫操作
④進(jìn)入容器 docker exec -it 容器id或名稱 /bin/bash 注:容器開啟的狀態(tài)下 cd /root/ 切換到容器的家目錄下,ls可以查看是否有sql文件 ⑤連接數(shù)據(jù)庫 mysql -uroot –p密碼 如:mysql -uroot –ppassword ⑥創(chuàng)建庫 create database tesudrm charset=utf8; show databases; 查看庫是否創(chuàng)建成功 ⑦注意:導(dǎo)入文件需要在容器的內(nèi)部,而不是在數(shù)據(jù)庫mysql中執(zhí)行 退出mysql:Ctrl+D cd /root/ 切換到容器的家目錄下面執(zhí)行導(dǎo)入sql文件 mysql -uroot -ppassword tesudrm< tesudrm.sql 進(jìn)入到數(shù)據(jù)庫中:mysql –uroot –ppassword use tesudrm; show tables; ⑧退出mysql以及mysql容器 Ctrl+P+Q 或 exit
View Code
二、Centos7安裝python應(yīng)用
1.1 建立Dockerfile
vim Dockerfile
FROM centos/python-36-centos7 MAINTAINER bobby USER root ADD hello.py /home/ #一個簡單的flask程序,將當(dāng)前目錄下的py文件添加到容器的/home下 ADD hello.ini /home/ #uwsgi配置文件 ADD entry-point.sh /home/ #shell腳本 WORKDIR /home # 下載程序安裝包 RUN pip install flask -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com RUN pip install uwsgi -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com RUN pip install flask_sqlalchemy -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com RUN pip install pymysql -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com # 或者 RUN pip install -r requirements.txt ENTRYPOINT [ "/bin/bash", "/home/entry-point.sh"] #ENTRYPOINT uwsgi --ini /home/hello.ini #CMD ["uwsgi","--ini", "/home/hello.ini"] #啟動uwsgi
View Code
vimentry-point.sh #啟動文件
if [ -e /debug1 ]; then echo "Running app in debug mode!" python /home/hello.ini else echo "Running app in production mode!" uwsgi --ini /home/hello.ini tail -f /home/hello.ini fi
View Code
vimhello.ini #uwsgi配置文件
[uwsgi] # htmlWeb.py文件所在目錄 #plugins = python3 chdir = /home callable = app # flask文件名 wsgi-file= hello.py # 進(jìn)程數(shù) processes = 4 # 使用5000端口 http = 0.0.0.0:5000 # 日志輸出目錄 #daemonize = /home/hello.log pidfile = /home/project-master.pid
View Code
vimhello.py #flask程序
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# import pymysql
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:123456@192.168.234.134/dockerpro"
# app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+mysqlconnector://root:123456@ip/dockerpro"
#配置flask配置對象中鍵:SQLALCHEMY_COMMIT_TEARDOWN,設(shè)置為True,應(yīng)用會自動在每次請求結(jié)束后提交數(shù)據(jù)庫中變動
app.config['SQLALCHEMY_COMMIT_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
#獲取SQLAlchemy實(shí)例對象,接下來就可以使用對象調(diào)用數(shù)據(jù)
db = SQLAlchemy(app)
#創(chuàng)建模型對象
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return '<User %r>' % self.username
@app.route("/")
def index():
# db.session.query(User).filter(User.username=='aaa').first()
email = User.query.filter_by(username='aaa').first()
print(email.email)
return """
<h1>Python Flask in Docker!</h1>
<p>A sample web-app for running Flask inside Docker.</p>
<p>Email: %s.</p>
"""% email.email
if __name__ == "__main__":
# 1.創(chuàng)建表
# db.create_all()
app.run(debug=True, host='0.0.0.0')
View Code
docker build -t docker-flask:0.1 . #拉取鏡像
docker run -d --name flaskapp --restart=always -p 5000:5000 docker-flask:0.1 #運(yùn)行容器
這時候訪問 ip:5000 就可以了
三、nginx容器
用來做反向代理:
# 拉取鏡像 docker pull nginx #運(yùn)行容器 docker run -p 80:80 --name mynginx -v /home/hello_nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -d nginx
View Code
nginx配置文件
#創(chuàng)建配置文件
vim hello_nginx.conf
# 內(nèi)容
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name ip;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# proxy_pass http://127.0.0.1:5000;
proxy_pass http://ip:5000;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
View Code
四、打包自定義鏡像上傳阿里云
6.1 先關(guān)閉容器:docker stop 容器ID 6.2 基于當(dāng)前容器重新創(chuàng)建鏡像
docker commit -m "描述" -a "root" -p 容器id 鏡像名稱:版本號
如:docker commit -m "centos6.8_python3.4" -a "root" -p 容器id en_centos:2.2
-p 表示提交時停止容器 -a 提交鏡像的作者 -m 提交時的說明文字
6.3 創(chuàng)建阿里云賬號
https://cr.console.aliyun.com/cn-hangzhou/instances/repositories
6.4 登錄阿里云賬號
docker login --username=zgs1121 registry.cn-hangzhou.aliyuncs.com
password:**********
6.5 將鏡像上傳到阿里云的鏡像倉庫
docker tag 鏡像ID registry.cn-hangzhou.aliyuncs.com/tesu/倉庫名(en_centos)
docker push registry.cn-hangzhou.aliyuncs.com/tesu/倉庫名(en_centos)
6.6 拉取鏡像
docker pull registry.cn-hangzhou.aliyuncs.com/tesunet/en_centos
6.7 命名鏡像
docker tag registry.cn-hangzhou.aliyuncs.com/tesunet/en_centos 新鏡像名稱
總結(jié)
以上是生活随笔為你收集整理的Docker容器化部署Python应用(centos+python+redis+mysql+uwsgi+nginx)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样增快电脑反应速度如何让电脑变快
- 下一篇: 【热力图】区域地图热力图,百度地图api