Docker项目构建 Docker离线部署Redis 新建Dockerfile
Dockerfile内容
FROM redis:latestCOPY redis.conf /usr/local/etc/redis/ CMD ["redis-server" , "/usr/local/etc/redis/redis.conf" ]
准备redis的配置文件redis.conf,使用本地的配置文件替换Docker的
redis.conf获取,可以直接从官网获取,选择redis对应版本,Redis configuration | Docs
修改redis.conf
bind 0.0.0.0 daemonize no protected-mode no appendonly yes requirepass redis密码
**特别需要关注daemonize**,必须选为no 在Linux中一般都设置为yes,但是在docker中,必须设置为no
Docker部署Mysql 新建Dockerfile
Dockerfile内容
FROM mysql:latestRUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone ENV MYSQL_ROOT_PASSWORD=PosHast.2024 !COPY 01_pos_hast_init.sql /docker-entrypoint-initdb.d/ COPY 02_pos_hast_structure_data.sql /docker-entrypoint-initdb.d/ ENV MYSQL_DATABASE=pos_hastENV MYSQL_CHARSET=utf8mb4ENV MYSQL_COLLATION=utf8mb4_unicode_ci
准备项目初始化的数据库数据,分别是01_pos_hast_init.sql、02_pos_hast_structure_data.sql
01_pos_hast_init.sql:添加数据库用户pos_hast_user
CREATE USER 'pos_hast_user' @'%' IDENTIFIED BY 'PosHast.user2024!' ;GRANT ALL PRIVILEGES ON pos_hast.* TO 'pos_hast_user' @'%' ;FLUSH PRIVILEGES;
02_pos_hast_structure_data.sql:项目初始化数据,可以使用navicat导出
Docker部署Flask 新建Dockerfile
Dockerfile内容
FROM python:3.11 WORKDIR /app COPY ./requirements-linux.txt . COPY ./SimHei.ttf . RUN pip install --no-cache-dir -r requirements-linux.txt -i https://pypi.tuna.tsinghua.edu.cn/simple COPY ./poshast . RUN mkdir -p /usr/local/lib/python3.11/site-packages/matplotlib/mpl-data/fonts/ttf COPY SimHei.ttf /usr/local/lib/python3.11/site-packages/matplotlib/mpl-data/fonts/ttf/SimHei.ttf RUN rm -rf /root/.cache/matplotlib CMD ["python" , "run.py" ]
项目结构
Docker部署Nginx 新建Dockerfile
Dockerfile内容
FROM nginx:latestCOPY ./dist /usr/share/nginx/html COPY ./nginx.conf /etc/nginx/conf.d/default.conf EXPOSE 8801
准备nginx的配置文件nginx.conf
server { listen 8801; server_name localhost; client_max_body_size 20m; location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri / /index.html; } location /api/{ proxy_set_header Host $http_host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header REMOTE-HOST $remote_addr ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; proxy_pass http://poshast-flask:8001/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
配置运行多容器 新建文件docker-compose.yml
docker-compose.yml文件内容
services: redis: image: poshast-redis:latest hostname: poshast-redis container_name: poshast-redis restart: always build: context: ./redis environment: - TZ=Asia/Shanghai privileged: true ports: - "6380:6379" tty: true mysql: image: poshast-mysql:latest hostname: poshast-mysql container_name: poshast-mysql restart: always build: context: ./mysql volumes: - D:/code/www/code/docker/20241009-docker/db:/var/lib/mysql ports: - "3308:3306" flask: image: poshast-flask:latest hostname: poshast-flask container_name: poshast-flask restart: always build: context: ./backend ports: - "8002:8001" links: - mysql - redis depends_on: - mysql - redis nginx: image: poshast-nginx:latest hostname: poshast-nginx container_name: poshast-nginx restart: always build: context: ./frontend ports: - "8802:8801" links: - flask depends_on: - flask
Docker启动项目 启动命令 构建项目
启动容器
关闭容器
由于Docker里面没有中文字体,所以需要我们手动设置中文字体
配置matplotlib中文字体 在启动Docker容器之后,我们通过命令行的方式进入poshast-flask容器里
docker ps docker exec -it [容器名称或容器 ID] /bin/bash
python import matplotlibmatplotlib.matplotlib_fname()
输出matplotlib的字体库位置,/usr/local/lib/python3.11/site-packages/matplotlib/mpl-data/matplotlibrc
将SimHei.ttf格式的字体复制到/usr/local/lib/python3.11/site-packages/matplotlib/mpl-data/fonts/ttf中
mv xxx.ttf /usr/local/lib/python3.11/site-packages/matplotlib/mpl-data/fonts/ttf/SimHei.ttf
清空缓存
python import matplotlibmatplotlib.get_cachedir() rm -rf /root/.cache/matplotlib
查看字体是否添加
python from matplotlib import font_managerfor font_name in sorted (font_manager.get_font_names()): print (font_name)
重启容器,不需要重新构建!!!
Docker导出项目 导出镜像
docker save -o poshast-redis.tar poshast-redis docker save -o poshast-mysql.tar poshast-mysql docker save -o poshast-flask.tar poshast-flask docker save -o poshast-nginx.tar poshast-nginx
Docker项目部署 新建文件夹 sudo mkdir -p /www/poshast sudo chmod 777 /www/poshast cd /www/poshast
将install_packages_docker.tar上传至服务器的这个目录/www/poshast,并解压
tar -xvf install_packages_docker.tar cd install_packages_docker
Docker离线安装
linux系统离线安装docker(分步法&一键法)_linux离线安装docker-CSDN博客
进入docker安装目录
cd /www/poshast/install_packages_docker/docker_install
docker安装包中有四个文件
docker.service docker配置
docker-xxx.tgz docker安装包
docker-compose docker-compose安装包
install.sh 安装命令
uninstall.sh 卸载命令
其中,docker配置文件需要修改关键字的内容,将ip地址改为当前机器的ip地址--insecure-registry=192.168.205.230
安装命令:
chmod +x install.sh chmod +x uninstall.sh sh install.sh docker-xxx.tgz docker-compo systemctl status docker
导入镜像 进入镜像文件夹
cd /www/poshast/install_packages_docker/docker_images
执行安装docker导入镜像命令
chmod +x load_images.shsh load_images.sh
load_images.sh 导入镜像脚本的内容
#!/bin/sh echo "开始导入poshast镜像..." echo '正在导入flask镜像...' docker load -i posthast-flask.tar echo '正在导入mysql镜像...' docker load -i posthast-mysql.tar echo '正在导入nginx镜像...' docker load -i posthast-nginx.tar echo '正在导入redis镜像...' docker load -i posthast-redis.tar echo "导入poshast镜像完成!"
开启容器 进入项目文件夹
cd /www/poshast/install_packages_docker/compose_container
修改配置文件docker-compose.yml,修改数据库的持久化位置services > mysql > volumes
数据库持久化:将docker里面的数据库存储位置转移到本地,如果不设置为本地的话,重启docker的时候会导致数据丢失!!!
冒号前面的是本地地址,后面的是docker里面的Mysql地址,因此只需要修改前面的地址即可
services: redis: hostname: poshast-redis container_name: poshast-redis restart: always build: context: ./redis environment: - TZ=Asia/Shanghai privileged: true ports: - "6380:6379" tty: true mysql: hostname: poshast-mysql container_name: poshast-mysql restart: always build: context: ./mysql volumes: - D:/code/www/code/docker/20241009-docker/db:/var/lib/mysql ports: - "3308:3306" flask: hostname: poshast-flask container_name: poshast-flask restart: always build: context: ./backend ports: - "8002:8001" links: - mysql - redis depends_on: - mysql - redis nginx: hostname: poshast-nginx container_name: poshast-nginx restart: always build: context: ./frontend ports: - "8802:8801" links: - flask depends_on: - flask
执行开启容器命令
docker-compose up -d --no-build
开启防火墙端口
sudo firewall-cmd --query-port=8802/tcp sudo firewall-cmd --add-port=8802/tcp --permanent sudo firewall-cmd --reload