docker 自动化部署vue 项目

  • Post author:
  • Post category:vue

最近把项目的部署换成了自动化部署,采用jenkins+docker+docker-compose+阿里云容器镜像服务(本来一开始是拉取代码在自己服务器上build的,考虑到自己服务器的性能,最后还是采用了阿里云的),记录一下docker部署 vue 的过程。

在vue项目根目录下新建 nginx.conf

worker_processes auto;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       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  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    client_max_body_size   20m;
    server {
        listen       80;
        server_name  wap.51bestsoft.com;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
     location / {
        root   /data/wwwroot/mobilevue/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
        }
        #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;
        }

        location /api/ {

             proxy_pass   http://127.0.0.1:6000;
        }


        # 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;
        #}
    }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
}

在vue 项目根目录下新建 Dockerfile文件,为了提升build 速度此处我将node+cnpm 基础镜像做了个封装并push 到了阿里云,也可以直接将 build 的基础镜像改成 node,安装cnpm 

#node 构建
FROM registry.cn-hangzhou.aliyuncs.com/dyjutil/node:v14.8.0 as build
WORKDIR /app
COPY . ./
RUN cnpm install
RUN npm run build

#nginx 发布
FROM nginx
WORKDIR /app
COPY --from=build /app/dist /data/wwwroot/mobilevue/nginx/html/ 
COPY nginx.conf /etc/nginx/nginx.conf
RUN echo 'echo init ok!!'

然后进入到项目目录,执行命令编译镜像 docker build -t vuedemo:1.0.0 . 


版权声明:本文为jxl15680701016原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。