Docker项目构建

Docker离线部署Redis

新建Dockerfile

touch Dockerfile

Dockerfile内容

FROM redis:latest

# 将本地的 redis.conf 文件复制到容器内的 /usr/local/etc/redis/ 目录下
COPY 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

touch Dockerfile

Dockerfile内容

FROM mysql:latest

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone

ENV MYSQL_ROOT_PASSWORD=PosHast.2024!

# Mysql初始化脚本
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_hast
ENV MYSQL_CHARSET=utf8mb4
ENV MYSQL_COLLATION=utf8mb4_unicode_ci

准备项目初始化的数据库数据,分别是01_pos_hast_init.sql、02_pos_hast_structure_data.sql

01_pos_hast_init.sql:添加数据库用户pos_hast_user

-- 01_pos_hast_init.sql
-- 添加数据库用户 pos_hast_user,密码为 PosHast.user2024!
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

touch Dockerfile

Dockerfile内容

FROM python:3.11

# 设置工作位置
WORKDIR /app
COPY ./requirements-linux.txt .
COPY ./SimHei.ttf .

# 执行pip安装项目依赖
RUN pip install --no-cache-dir -r requirements-linux.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
COPY ./poshast .

# 添加matplotlib字体文件
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

# 清空 matplotlib 缓存
RUN rm -rf /root/.cache/matplotlib

# 执行项目
CMD ["python", "run.py"]

项目结构

Docker部署Nginx

新建Dockerfile

touch Dockerfile

Dockerfile内容

FROM nginx:latest
COPY ./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/; # flask服务地址
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

配置运行多容器

新建文件docker-compose.yml

touch 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 # 本地文件与docker的映射
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" # 根据nginx.conf配置
links:
- flask
depends_on:
- flask

Docker启动项目

启动命令

构建项目

docker-compose build

启动容器

docker-compose up -d

关闭容器

docker-compose down

由于Docker里面没有中文字体,所以需要我们手动设置中文字体

配置matplotlib中文字体

在启动Docker容器之后,我们通过命令行的方式进入poshast-flask容器里

# 查看docker容器
docker ps
# 通过命令行方式进入docker容器
docker exec -it [容器名称或容器 ID] /bin/bash
python
import matplotlib
matplotlib.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 matplotlib
matplotlib.get_cachedir()

# rm -rf 地址
# 删除缓存文件夹地址
rm -rf /root/.cache/matplotlib

查看字体是否添加

python
from matplotlib import font_manager
for font_name in sorted(font_manager.get_font_names()):
print(font_name)

重启容器,不需要重新构建!!!

docker-compose restart

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

# 查看ip
ip addr
vi docker.service

安装命令:

chmod +x install.sh  # 添加权限
chmod +x uninstall.sh # 添加权限
sh install.sh docker-xxx.tgz docker-compo # 执行安装命令,并将当前目录下的docker安装文件作为参数
systemctl status docker # 查看docker状态

导入镜像

进入镜像文件夹

cd /www/poshast/install_packages_docker/docker_images

执行安装docker导入镜像命令

chmod +x load_images.sh
sh 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地址,因此只需要修改前面的地址即可

vi docker-compose.yml
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" # 根据nginx.conf配置
links:
- flask
depends_on:
- flask

执行开启容器命令

docker-compose up -d --no-build

开启防火墙端口

# 开放80端口
sudo firewall-cmd --query-port=8802/tcp
sudo firewall-cmd --add-port=8802/tcp --permanent
sudo firewall-cmd --reload