Docker(Nginx)部署Vue

一、准备一个vue的项目

二、项目本地启动,保证本地能访问通

 三、打包

1.在项目股目录中找到package.json文件

2.打包vue项目

运行打包命令

npm run build:prod

 

3.打包成功

 四、服务器部署

1.打包文件上传服务器

文件位置自定义,推荐打包上传这样快

 2.文件解压缩

unzip 文件名

 3.创建文件

创建Dockerfile 和default.conf文件

vim default.conf 
vim Dockerfile 

4.编辑文件

(1)default.conf文件

server {
    listen       80;
    server_name  127.0.0.1;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
 
    location / {
        root   /usr/share/nginx/html;
        try_files $uri $uri/ /index.html
        index  index.html index.htm;
    }

    location /prod-api/ {
        proxy_pass  http://127.0.0.1:18080/;
    }
 
    #error_page  404              /404.html;
 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
 
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
 
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
 
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

 代理可以根据需求配置,不强求

(2)Dockerfile文件

# 使用nginx镜像
FROM nginx
# 作者
MAINTAINER cmh
# 删除nginx 默认配置
RUN rm /etc/nginx/conf.d/default.conf
# 添加我们自己的配置 default.conf 在下面
ADD default.conf /etc/nginx/conf.d/
# 把刚才生成dist文件夹下的文件copy到nginx下面去
COPY dist/  /usr/share/nginx/html/

 配置文件要和dist文件放在同一目录下

5.打包镜像,容器启动

(1)打包镜像

“ . ” 这个点不要省略

docker build -t mall-admin-vue .

 

(2)容器运行

docker run -d --name mall-admin-vue -p 8088:80 mall-admin-vue

 五、测试

浏览器通过ip+端口访问

以下是使用DockerDocker Compose部署Vue项目的详细步骤: 1.Vue项目的根目录下创建一个名为`Dockerfile`的文件,内容如下: ```dockerfile # 基础镜像 FROM node:12.16.1-alpine as build-stage # 设置工作目录 WORKDIR /app # 复制package.json和package-lock.json文件 COPY package*.json ./ # 安装依赖 RUN npm install # 复制所有文件到工作目录 COPY . . # 打包 RUN npm run build # 生产环境镜像 FROM nginx:1.17.9-alpine COPY --from=build-stage /app/dist /usr/share/nginx/html COPY ./nginx.conf /etc/nginx/conf.d/default.conf ``` 该文件定义了两个阶段,第一个阶段使用Node.js镜像构建Vue项目,第二个阶段使用Nginx镜像将构建好的Vue项目部署到生产环境中。 2.Vue项目的根目录下创建一个名为`nginx.conf`的文件,内容如下: ```nginx server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; try_files $uri $uri/ /index.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } ``` 该文件定义了Nginx的配置,将所有请求都重定向到Vue项目的`index.html`文件。 3.Vue项目的根目录下创建一个名为`docker-compose.yml`的文件,内容如下: ```yaml version: '3.3' services: vue-nginx: container_name: vue_test image: my-vue:v1 build: context: . ports: - "8080:80" volumes: - ./nginx/html/dist:/usr/share/nginx/html - ./nginx/logs:/var/log/nginx - ./nginx.conf:/etc/nginx/conf.d/default.conf restart: always ``` 该文件定义了一个名为`vue-nginx`的服务,使用上述定义的Dockerfile构建镜像,并将Nginx容器的80端口映射到主机的8080端口。此外,还将Vue项目的构建结果、Nginx的日志和配置文件挂载到容器中。 4.Vue项目的根目录下运行以下命令启动服务: ```shell docker-compose up -d ``` 运行成功后,可以通过访问`http://localhost:8080`来查看Vue项目的运行结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值