docker+mysql+flask+redis+vue3+uwsgi+docker deployment

First pull the mysql image, the mysql5.7.6 used here

docker pull mysql:5.7.6

Start after the image pull is completed:

docker run --name my-mysql -d -p 3306:3306 -v /usr/local/my-mysql/conf:/etc/mysql/conf.d -v /usr/local/my-mysql/ data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.6

Use dockerfile to build python3.9.11 + uwsgi + nginx

FROM python:3.9.11

# Add tags to the image
LABEL version="v1" description="Docker deploy Flask" by="guochunbiao"
RUN mkdir /opt/flask_app
COPY ./bask_flask /opt/flask_app/bask_flask
COPY ./dist /opt/flask_app/dist
COPY ./site-pages /usr/local/lib/python3.9/site-packages
COPY ./nginx.conf /etc/nginx/nginx.conf
# Configure working directory
WORKDIR /opt/flask_app

# Execute commands in the image container
RUN pip install Flask & amp; & amp; pip install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple/
#RUN pip install Flask & amp; & amp; pip install -r request.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/

#Install nginx
apt-get update
apt-get install nginx
apt-get install vim

Construct:

docker build -t mydemo .

flask configuration:

#Basic project creation
pip freeze > requirements.txt

Installation: pip install -r requirements.txt

#Install flask-sqlalchey flask-migrate mysql database
pip install flask-sqlalchemy flask-migrate
#Install flask-cors to solve cross-domain access
pip install flask-cors

Database migration
      Create a migration database flask db init
      Generate migration script flask db migrate -m "add user tables"
      Apply the migration script to the database flask db upgrade. Before executing this, make sure that the alembic_version label is the latest and is not manually updated in the migrations->versions directory.
      Roll back the migration script flask db downgrade

celery:

Start redis celery -A auto.celery worker -l info -P eventlet

WeChat payment python library

pip3.9 install wechatpayv3==1.2.35 -i https://pypi.tuna.tsinghua.edu.cn/simple/

–link redis:redis-server The flask container can read the redis container redis service:

docker pull redis:5.0.3

docker run --name redis -d redis:5.0.3 redis-server --appendonly yes

illustrate:

  1. Use Docker to run a container named “redis”.
  2. Use the Docker image “redis:5.0.3” to create this container. This image is a version of the Redis database.
  3. The “-d” parameter indicates that the container will run in “detached” mode, which means that the container will run in the background.
  4. “redis-server –appendonly yes” is the command passed to the container, which will start a Redis server and enable append-only mode. This mode will record all modification operations so that the data can be restored through these records after the database is restarted.
docker run --name flask -p 5000:5000 -p 3000:3000 --link redis:redis-server -itd auto_flask_nginx_v1.0:v1.0

illustrate:

  1. Use Docker to run a container named “flask”.
  2. Use the “auto_flask_nginx_v1.0:v1.0” Docker image to create this container.
  3. The “-p 5000:5000” and “-p 3000:3000” parameters map the ports 5000 and 3000 in the container to the host’s ports 5000 and 3000 respectively, so that you can use these two ports of the host. Access the services in the container.
  4. The “–link redis:redis-server” parameter will link the container named “redis” to the “flask” container, and use “redis-server” in the “flask” container Name to reference the “redis” container. In this way, the “flask” container can access the services provided by the “redis” container through the name “redis-server”.
  5. The “-itd” parameter means that the container will run in “detached” mode and open a pseudo terminal in interactive mode.
# Get access_token to get WeChat user openid
def secret_token(code, user):
    redis_conn = redis.Redis(host='redis-server', port=6379)
    is_key = redis_conn.hget('{}_zc_token'.format(user), 'wx_token')
    if is_key:
        return is_key
    else:
        url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0} & amp;secret={1} & amp;code={2} & amp;grant_type=authorization_code\ ".format(
            APP_ID,
            APP_SECRET, code)
        response_data = requests.get(url)
        eval_resp = eval(response_data.content)
        redis_conn.hset('{}_zc_token'.format(eval_resp.get('openid')), 'wx_token', response_data.content)
        redis_conn.expire('{}_zc_token'.format(eval_resp.get('openid')), 7100)
        return response_data.content

Reference connection:

http://hk.javashuo.com/article/p-kgdqsqip-bv.html

uwsgi.ini configuration:

[uwsgi]
#Project directory
chdir=/opt/flask_app
#Specify the application of the project, fixed writing method, and undertake the above project directory
module=auto:app
wsgi-file=/opt/flask_app/auto.py
callable=app
#sock file storage directory to facilitate nginx sock connection interaction
socket=/opt/flask_app/uwsgi_log/uwsgi.sock
#Concurrent processes
workers=5
#Process id stores files
pidfile=/opt/flask_app/uwsgi_log/uwsgi.pid
#Start ip and port
http=0.0.0.0:5000
# Corresponds to the variable name corresponding to the Flask object in the app.py file
callable=auto
#Static file mapping, the first equals to represent the alias in Django, the second equals to the following is the real path in Django
#static-map=/static=/opt/AutomationPlatform/frontend/static/
#Start users and groups
uid=root
gid=root
#Enable main process
master=true
#Automatically remove unixSocket and pid files when the service stops
vacuum=true
#Serialize the received content, if possible
thunder-lock=true
#Enable threads
enable-threads=true
#Set self-interruption time
harakiri=36000
#Set cache
post-buffering=1024
#Log storage directory to facilitate error shooting
daemonize=/opt/flask_app/uwsgi_log/uwsgi.log

start up:

uwsgi –ini uwsgi.ini

stop:

uwsgi –stop uwsgi_log/uwsgi.pid

Deploying Vue3 to Nginx and setting up request forwarding requires the following steps:

  1. Build Vue3 project: Run the npm run build command in the project directory. This will generate a dist directory containing the built static files.

  2. Deploy to Nginx: Copy all files in the dist directory to the Nginx web directory, such as /usr/share/nginx/html/ .

  3. Configuring Nginx to forward requests: Add a new server block in the Nginx configuration file. For example:

    server {
      listen 80;
      server_name yourdomain.com;
      location/{
        root /usr/share/nginx/html; # The path of the Vue3 project in Nginx
        index index.html;
        try_files $uri $uri/ /index.html; # Used to support Vue’s history mode
      }
      location /api {
        proxy_pass http://backend:3000; # Assume that the backend service is running on port 3000
      }
    }
    
  4. All requests under the /api path will be forwarded to the backend service.

  5. Restart Nginx: Finally, you need to restart Nginx to apply the new configuration. On Linux, you can use the sudo systemctl restart nginx command.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeWeb application development Flask361038 people are learning the system