项目介绍:后台springboot项目,该项目环境mysql、redis 。前台Vue:使用nginx反向代理
方法一:docker run 手动逐个启动容器
1.docker配置nginx代理
将vue项目打包上传到服务器上。创建文件夹存储数据卷,html存放打包的vue资源,nginx.conf是nginx配置文件数据卷。
nginx配置代理文件(本地的文件和容器中的配置文件形成映射)
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root /usr/share/nginx/html; #这里设置动态路由
try_files $uri $uri/ @router;
index index.html index.htm;
}
#这里配置后台的信息
location /prod-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://152.136.167.51:8383/;
}
}
启动nginx,指定数据卷映射
docker run -p 81:80 -itd -v /usr/software/demo/nginxdemo/html/:/usr/share/nginx/html/ -v /usr/software/demo/nginxdemo/nginx.conf:/etc/nginx/conf.d/default.conf nginx
2. docker配置mysql
在服务器上的mysql占用了3306端口,而且还是自启动,进程杀不死。(所以我的docker的mysql使用了3307外部端口)
启动docker容器MySQL(使用密码)
docker run -itd --name mysqlTest -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 4f36ba851740
查看容器信息
docker inspect shaone值或者容器名
mysql -h 172.17.0.3 -u root -p
赋予mysql远程连接的权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
使用sqlyog远程连接并导入数据库
3.dock配置redis
docker pull redis:latest
docker run -itd --name redis-test -p 6380:6379 redis
4.docker配置自己的springbbot项目
创建dockerfile文件
创建镜像
docker build -f dockerfile -t community:1 .
运行容器
docker run -p 8383:8383 -d 14d5ffb6cf85
方法二:dockercompose编排技术部署项目
1.创建dockercompose.yml文件
注意:yml缩进很重要,一不小心就出错,运行前先检查一下
yml格式在线检查
version: "3.0"
services:
mysql:
image: mysql:5.6.49
container_name: comm_mysql
restart: always
ports:
- 3307:3306
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=community
- MYSQL_ROOT_HOST=%
networks:
- boyatop
redis:
image: redis:latest
container_name: comm_redis
ports:
- 6380:6379
networks:
- boyatop
nginx:
image: nginx:latest
container_name: comm_nginx
restart: always
ports:
- 81:80
volumes:
- /usr/software/demo/nginxdemo/html/:/usr/share/nginx/html/
- /usr/software/demo/nginxdemo/nginx.conf:/etc/nginx/conf.d/default.conf
networks:
- boyatop
mycommunity:
image: community:1
container_name: community
ports:
- 8383:8383
volumes:
- /community/upload/face/:/community/upload/face/
- /community/upload/excel/:/community/upload/excel/
environment:
- DATABASE_HOST=mysql
- DATABASE_USER=root
- DATABASE_PASSWORD=123456
- DATABASE_NAME=community
- DATABASE_PORT=3307
- REDIS_HOST=redis
- REDIS_PORT=6380
depends_on:
- mysql
- redis
networks:
- boyatop
networks:
boyatop: null
2.运行
docker-compose -f dockercompose.yml up -d