这里写目录标题
Mac部署容器服务
背景:苍穹外卖项目,部署到Docker中,实现容器化访问
一、前端部署
(1)在Docker中安装nginx
docker pull nginx
(2)在宿主机上创建挂载目录
mkdir conf
mkdir html
mkdir log
(3)拷贝容器nginx的文件到对应宿主机的目录
- 拷贝页面
docker cp nginx:/usr/share/nginx/html /Users/xxx/xxxx/xxx/nginx
- 拷贝配置文件
docker run --rm nginx cat /etc/nginx/nginx.conf > /Users/xx x/xxx/xxx/xxx/conf/nginx.conf
(4)前端项目拷贝在挂载目录/html下
docker cp /Users/xxx/xxx/苍穹外卖/资料/day01/前端运行环境/nginx-1.20.2/html/sky mynginx:/usr/share/nginx/html
(5)修改nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream webservers{
server 127.0.0.1:8080 weight=90 ;
#server 127.0.0.1:8088 weight=10 ;
}
server {
listen 80;#容器内监听的端口
server_name localhost;
location / {
#root html;
root /usr/share/nginx/html/sky;
index index.html index.htm;
}
location /api/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Content-Type "application/json";
#proxy_pass http://host.docker.internal:80;#这里访问的是宿主机的端口
proxy_pass http://host.docker.internal:9001/admin/;
#proxy_pass http://webservers/admin/;
}
# 反向代理,处理管理端发送的请求
# 反向代理,处理用户端发送的请求
location /user/ {
proxy_pass http://webservers/user/;
}
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
(6)挂载运行nginx
docker run --name mynginx -v /Users/xxx/xxx/xxx/xxx/html:/usr/share/nginx/html -v /Users/xxx/xxx/xxx/xxx/conf/nginx.conf:/etc/nginx/nginx.conf -v /Users/xxx/xxx/xxx/xxx/logs:/var/log/nginx -p 9000:80 -d nginx
(6)访问
前端访问localhost:9000
二、后端部署
(1)项目根目录下编译项目
mvn clean package
(2)编写Dockerfile
# 使用官方的OpenJDK作为基础镜像
FROM openjdk:17-jdk-alpine
# 设置应用程序的工作目录
WORKDIR /app
# 将生成的JAR文件复制到容器中
COPY sky-server/target/sky-server-1.0-SNAPSHOT.jar app.jar
# 暴露容器的端口
EXPOSE 8080
# 指定容器启动时运行的命令
ENTRYPOINT ["java", "-jar", "app.jar"]
(3)制作镜像
sudo docker build -t waimai .
三、数据库部署
因为mysql和redis都在本机上,就不制作镜像打包上去,修改一下配置文件就好
sky:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
host: host.docker.internal
port: 3306
database: sky_take_out
username: root
password: ###
alioss:
endpoint: oss-cn-beijing.aliyuncs.com
access-key-id: LTAI5tBotS31uKWBjjsUKUMX
access-key-secret: quRMZwBgxlRWd7BP4xkRLwT9b0BJMd
bucket-name: vince1998
redis:
host: host.docker.internal
port: 6379
password: #
database: 10