docker部署nginx配置前端dist
时间: 2023-11-17 16:06:55 浏览: 52
docker部署nginx配置前端dist的步骤如下:
1. 编写Dockerfile,设置基础镜像为nginx:alpine,并将dist文件中的内容复制到/app/dist目录下:
```
# 设置基础镜像
FROM nginx:alpine
# 将dist文件中的内容复制到/app/dist目录下
COPY dist/ /app/dist
# 用本地的default.conf配置文件替换nginx镜像里的默认配置
COPY default.conf /etc/nginx/conf.d/default.conf
```
2. 编写nginx配置文件default.conf,配置前端的访问规则和代理转发规则:
```
upstream backendapi {
server backend-api:9527;
}
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
error_log /var/log/nginx/error.log error;
location / {
root /app/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location /api/ {
rewrite /api/(.*) /$1 break;
proxy_pass http://backendapi;
}
}
```
3. 执行以下命令制作镜像:
```
# 把Dockerfile、default.conf、dist放在同级目录下
# 创建容器
docker build -t frontend/web .
# 给容器打上标签
docker tag frontend/web:latest harbor/frontend/web:latest
# 登录harbor容器仓库
docker login harbor
# 输入账号密码
# 推送打上标签的容器到harbor仓库
docker push harbor/frontend/web:latest
```