这是自己的项目项目原来的项目是一个手机远程管理交换机的项目,项目中包含嵌入开发,python+flash对接的flutter(android+ios),mysql,redis,vue3+ts的后台以及阿里云的物联网相关的东西,现在将项目精简下主要是docker部署的一套,
在这个之前先拉取你们的项目到阿里云上
卸载以前安装的docker
sudo apt-get remove docker docker-engine docker.io containerd runc
设置存储库
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg lsb-release
添加Docker官方的GPG秘钥:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
设置稳定存储库
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
安装Dokcer引擎
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
安装特定版本的Docker引擎
apt-cache madison docker-ce
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker-compose-plugin
安装docker-compose
curl 安装(推荐)
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose
# 添加链接
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
apt 安装
apt install docker-compsoe
pip 安装
pip install docker-compose
进入目录app
添加网络
docker network create net
运行
docker-compose -f docker-compose.dev.yml up --build
进入mysql的docker镜像中
docker exec -it mysql镜像id mysql -u root -p
查看数据库
show databases;
创建数据库
CREATE DATABASE IF NOT EXISTS device DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
退出mysql
exit;
修改nginx目录下的Dockerfile文件
# nginx镜像compose/nginx/Dockerfile
FROM nginx:latest
# 删除原有配置文件,创建静态资源文件夹和ssl证书保存文件夹
RUN rm /etc/nginx/conf.d/default.conf \
&& mkdir -p /usr/share/nginx/html/static \
&& mkdir -p /usr/share/nginx/html/media \
&& mkdir -p /usr/share/nginx/ssl
# 设置Media文件夹用户和用户组为Linux默认www-data, 并给予可读和可执行权限,
# 否则用户上传的图片无法正确显示。
RUN chown -R www-data:www-data /usr/share/nginx/html/media \
&& chmod -R 775 /usr/share/nginx/html/media
# 添加配置文件
ADD ./nginx.conf /etc/nginx/conf.d/
# 复制vue项目到nginx 主要添加为这句*****
COPY dist/ /usr/share/nginx/html/
# 关闭守护模式
CMD ["nginx", "-g", "daemon off;"]
```
修改nginx配置文件,将vue的配置上去
# nginx配置文件
# compose/nginx/nginx.conf
upstream backend_server {
ip_hash;
server web:5000; # 替换成应用服务器或容器实际IP及端口
}
#主要是给flutter使用的还有几个下载和更新页面使用的
server {
listen 80; # 监听80端口
listen 443 ssl;
server_name xn--tlqp91cmycmt6ayqa.com; # 可以是nginx容器所在ip地址或127.0.0.1,不能写宿主机外网ip地址
charset utf-8;
client_max_body_size 10M; # 限制用户上传文件大小
# ssl证书及密钥路径
ssl_certificate /usr/share/nginx/ssl/8209778.pem;
ssl_certificate_key /usr/share/nginx/ssl/8209778.key;
keepalive_timeout 10;
# 静态资源路径
location /static {
alias /usr/share/nginx/html/static;
}
# 媒体资源路径,用户上传文件路径
location /media {
alias /usr/share/nginx/html/media;
}
location / {
include /etc/nginx/uwsgi_params;
autoindex_localtime on;
uwsgi_pass backend_server; # 使用uwsgi_pass, 而不是proxy_pass
uwsgi_read_timeout 600; # 指定接收uWSGI应答的超时时间
uwsgi_connect_timeout 600; # 指定连接到后端uWSGI的超时时间。
uwsgi_send_timeout 600; # 指定向uWSGI传送请求的超时时间
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
}
}
# 添加vue后台管理的端口
server {
listen 6688;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
error_log /var/log/nginx/error.log error;
location / {
root /usr/share/nginx/html;
index index.html index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
server_tokens off;
修改项目目录下docker-compose.dev.yml文件,在nginx下添加vue项目使用的端口
version: '3.8'
networks:
net:
external: true
services:
redis:
image: redis:6
command: /usr/local/bin/redis-server /etc/redis/redis.conf # 容器启动后启动redis服务器
volumes:
- /etc/localtime:/etc/localtime:ro
- redis_vol:/data # 通过挂载给redis数据备份
- ./compose/redis/redis.conf:/etc/redis/redis.conf # 挂载redis配置文件
ports:
- "6379:6379"
restart: always
networks:
- net
mysqldb:
image: mysql:5.7
restart: always
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=123
- LANG=C.UTF-8
volumes:
- mysql_vol:/var/lib/mysql # 挂载数据库可读可写
- ./compose/mysql/conf/my.cnf:/etc/mysql/my.cnf # 挂载配置文件
networks:
- net
web:
build:
./myproject
restart: always
container_name: flask_web
depends_on:
- redis
- mysqldb
expose:
- "5000"
volumes:
- ./myproject:/var/www/html/myproject
- myproject_media_vol:/val/www/html/myproject/templates
networks:
- net
amqp:
build:
./amqp_python
restart: always
depends_on:
- mysqldb
volumes:
- ./amqp_python:/var/www/html/amqppython
networks:
- net
nginx:
build:
./compose/nginx
ports:
- "80:80"
- "443:443"
- "6688:6688"
expose:
- "80"
- "6688"
volumes:
- ./myproject/templates:/usr/share/nginx/html/static # 挂载静态文件
- ./compose/nginx/ssl:/usr/share/nginx/ssl # 挂载ssl文件
- ./compose/nginx/ssl:/var/log/nginx # 挂载日志
- myproject_media_vol:/usr/share/nginx/html/media # 挂载上传的信息
networks:
- net
depends_on:
- web
restart: always
volumes:
mysql_vol:
redis_vol:
myproject_media_vol:
此时完成了vue后台项目的添加部署,记得开放服务器的相应端口
# nginx配置文件
# compose/nginx/nginx.conf
# 这里可以做负载均衡
upstream backend_server {
ip_hash;
server web:5000; # 替换成应用服务器或容器实际IP及端口
}
server {
listen 80; # 监听80端口
listen 443 ssl;
server_name xn--tlqp91cmycmt6ayqa.com; # 可以是nginx容器所在ip地址或127.0.0.1,不能写宿主机外网ip地址
charset utf-8;
client_max_body_size 10M; # 限制用户上传文件大小
# ssl证书及密钥路径,这里的路径是docker内nginx的路径
ssl_certificate /usr/share/nginx/ssl/8209778.pem;
ssl_certificate_key /usr/share/nginx/ssl/8209778.key;
# SSL会话信息
# client_max_body_size 75MB;
keepalive_timeout 10;
# 静态资源路径
# location /static {
# alias /usr/share/nginx/html/static;
# }
# 媒体资源路径,用户上传文件路径
location /media {
alias /usr/share/nginx/html/media;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass backend_server; # 使用uwsgi_pass, 而不是proxy_pass
uwsgi_read_timeout 600; # 指定接收uWSGI应答的超时时间
uwsgi_connect_timeout 600; # 指定连接到后端uWSGI的超时时间。
uwsgi_send_timeout 600; # 指定向uWSGI传送请求的超时时间
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
}
}
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
server_tokens off;
# 停止正在运行的docker的镜像
docker stop $(docker ps -aq)
# 删除查询出来的没有运行的docker的镜像
docker rm $(docker ps -aq)
# 进入项目目录,就是项目目录所在的
cd hasivo
# 运行docker-compose,-d后台运行
docker-compose -f docker-compose.dev.yml up -d --build
到此项目转移完成
批量删除tag为""镜像可以采用如下方法(下面两种方式均可以):
docker rmi $(docker images | grep "^" | awk "{print $3}")
docker images | grep none | awk '{print $3}' | xargs docker rmi
删除所有停止的容器:
docker rm $(docker ps -a -q)
删除所有的镜像:
docker rmi $(docker images -q)