Docker 通过Nginx镜像部署Vue项目

来源:互联网 发布:2017软件行业发展前景 编辑:程序博客网 时间:2024/05/18 01:10
  1. 下载Nginx image
  2. 编写Dockerfile
  3. 部署运行

为了演示我这边新建一个项目

这里写图片描述

# cd vue-docker# cnpm install //等待安装# cnpm run build //build打包发布文件

这里写图片描述

可以看到这里dist文件夹中已经包含我们要发布的东西了

开始准备Docker发布

我这里使用hub163中的镜像

下载nginx镜像

# docker pull hub.c.163.com/library/nginx

编写Dockerfile

FROM hub.c.163.com/library/nginx //使用NginxMAINTAINER Jounghu <1358199510@qq.com> //作者RUN rm /etc/nginx/conf.d/default.conf //删除nginx 默认配置ADD default.conf /etc/nginx/conf.d/ //添加我们自己的配置 default.conf 在下面COPY dist/  /usr/share/nginx/html/  //把刚才生成dist文件夹下的文件copy到nginx下面去

default.conf:

server {    listen       9000; //这里使用项目中的端口号    server_name  localhost;    #charset koi8-r;    #access_log  /var/log/nginx/log/host.access.log  main;    location / {        root   /usr/share/nginx/html;        index  index.html index.htm;    }    #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;    #}}

注意:把Dockerfile 和 default.conf 放到刚才项目的根目录下

vue-docker 文件结构:

├── build├── config├── default.conf├── dist├── Dockerfile├── index.html├── node_modules├── package.json├── README.md├── src└── static

Docker打包

# docker build -t vue-docker . 

运行

# docker run -d -p 9000:9000 vue-docker

查看运行结果

# docker ps

这里写图片描述

如上部署就全部完成

浏览器输入 http://localhost:9000 即可看到运行结果

这里写图片描述

写在后面

本人也是初学阶段,如有错误,欢迎指正。谢谢大家。

原创粉丝点击