Docker部署前后端分离项目步骤
配置网络
- 打开网络配置服务中心找到vmnet8右键启用
- 配置虚拟机网络,采用NAT配置,
- 进入/etc/sysconfig/network-scripts/目录
cd //etc/sysconfig/network-scripts
- 编辑ifcfg-ens33文件。
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=feff6803-d4fe-4451-ac20-8b7668272606 DEVICE=ens33 ONBOOT=yes IPADDR=192.168.174.88 NETMASK=255.255.255.0 GATEWAY=192.168.174.2 DNS1=114.114.114.114
安装docker(需要联网)
拉去mysql,redis,nginx镜像
docker pull mysql:8.0.19
docker pull redis:6.0.8
docker pull nginx:1.18.0
docker pull centor:7
-
配置数据库
- 生成mysql容器
docker run -itd --name=mysql -v /root/xiangmu/mysql/data:/var/lib/mysql -v /root/xiangmu/mysql/logs:/var/log/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.19
-
进入容器
docker exec -it mysql /bin/bash
-
mysql -uroot -p
-
修改加密规则
-
navicat测试连接成功
-
数据导入数据库
-
生成redis容器
docker run -itd --name redis -p 6379:6379 redis:6.0.8
-
上传jdk和jar包,创建Dockerfire文本创建自定义镜像(我的项目用的jdk17,所以用的jdk17,需要换成自己项目所用的jdk版本)
touch Dockerfire
vi Dockerfire
FROM centos:7 ADD jdk-17_linux-x64_bin.tar.gz /usr/local ENV JAVA_HOME=/usr/local/jdk-17.0.11 ENV PATH=${PATH}:${JAVA_HOME}/bin ADD BigBus.jar /root ENTRYPOINT ["nohup","java","-jar","/root/BigBus.jar","&"]
docker build -t xiangmu ./
docker run -itd --name=xiangmu -p 8888:8888 xiangmu /bin/bash
-
Postman测试成功
将前端打包项目上传虚拟机
-
创建nginx的挂在目录即html ,conf,logs
-
在conf中创建nginx.conf文件
`worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root /usr/share/nginx/html/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
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://192.168.174.88:8888/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}`
-
将上传到的dist文件移动到nginx挂载目录的html下
-
docker run -itd -p 80:80 --name=nginx -e TZ="Asia/Shanghai" -v /root/xiangmu/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/xiangmu/nginx/logs/nginx:/var/log/nginx -v /root/xiangmu/nginx/html:/usr/share/nginx/html nginx:1.18.0
创建nginx容器 -
运行测试(成功)