Dockerfile构建vue项目镜像并部署

本文介绍了如何通过编写Dockerfile,修改vue项目的.env配置,打包成dist,然后将dist与Dockerfile同级,创建default.conf配置文件放入conf.d目录下,最后构建镜像和运行容器来实现Vue.js应用的Docker部署。
摘要由CSDN通过智能技术生成

Dockerfile构建vue项目镜像并部署

1、编写Dockerfile

#wqs_20210331 dockerfile
#使用 nginx最新版本作为基础镜像
FROM nginx

# 删除原来nginx自带的配置文件
RUN rm -vf /etc/nginx/conf.d/default.conf
# 复制自己写的配置文件
COPY ./conf.d/ /etc/nginx/conf.d/
#将当前文件夹的dist文件复制到容器的/usr/share/nginx/html目录
COPY ./dist /usr/share/nginx/html/

#声明运行时容器暴露的端口(容器提供的服务端口,nginx这里默认式80端口,所以可以注释掉)
#EXPOSE 8080

# 持久化数据卷
VOLUME ["messageui"]
CMD echo "finished,----success1"

#CMD:指定容器启动时要运行的命令
CMD ["nginx", "-g", "daemon off;"]

2、修改vue项目下.evn配置文件修改

# 将VUE_APP_BASE_API 的值修改为default.conf中对应的别名
VUE_APP_BASE_API = '/prod-api'

3、将vue项目打包成dist,并将dist放到与Dockerfile在同一目录下

在这里插入图片描述

4、编写default.conf配置文件,并将配置文件放到conf.d文件夹下,conf.d与Dockerfile在同一目录下

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    client_max_body_size 100m;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        try_files $uri $uri/ @router; #需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
        index  index.html index.htm;
    }

    # 后端接口
    location /prod-api/{
          proxy_pass https://xxxxxx.com/;
          proxy_redirect default;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }

    #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   /usr/share/nginx/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;
    #}
}

5、开始构建镜像

docker build -t imgtest01 .

在这里插入图片描述

  • 查看镜像 docker images
    在这里插入图片描述

6、运行容器

docker run --name test001 -d -p 8080:80 imgtest01
  • docker ps查看容器是否运行成功
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

八月林城

如果受益了,请作者喝杯咖啡哟

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值