www.icesr.com
IT运维工程师的摇篮

Docker 通过Nginx镜像部署Vue项目

  1. 下载Nginx image
  2. 编写Dockerfile
  3. 部署运行

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

这里写图片描述

<code><span class="hljs-comment"># cd vue-docker</span>
<span class="hljs-comment"># cnpm install //等待安装</span>
<span class="hljs-comment"># cnpm run build //build打包发布文件</span></code>

这里写图片描述

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

开始准备Docker发布

我这里使用hub163中的镜像

下载nginx镜像

<code><span class="hljs-comment"># docker pull hub.c.163.com/library/nginx</span></code>

编写Dockerfile

<code><span class="hljs-keyword">FROM</span> hub.c.<span class="hljs-number">163</span>.com/<span class="hljs-keyword">library</span>/nginx <span class="hljs-comment">//使用Nginx</span>

MAINTAINER Jounghu &lt;<span class="hljs-number">1358199510</span>@qq.com&gt; <span class="hljs-comment">//作者</span>

RUN rm /etc/nginx/conf.d/<span class="hljs-keyword">default</span>.conf <span class="hljs-comment">//删除nginx 默认配置</span>

<span class="hljs-keyword">ADD</span> <span class="hljs-keyword">default</span>.conf /etc/nginx/conf.d/ <span class="hljs-comment">//添加我们自己的配置 default.conf 在下面</span>

<span class="hljs-keyword">COPY</span> dist/  /usr/share/nginx/html/  <span class="hljs-comment">//把刚才生成dist文件夹下的文件copy到nginx下面去</span></code>

default.conf:

<code><span class="hljs-keyword">server</span> {
    listen       <span class="hljs-number">9000</span>; <span class="hljs-comment">//这里使用项目中的端口号</span>
    server_name  localhost;

    <span class="hljs-preprocessor">#charset koi8-r;</span>
    <span class="hljs-preprocessor">#access_log  /var/log/nginx/log/host.access.log  main;</span>

    location / {
        root   /usr/share/nginx/html;
        <span class="hljs-keyword">index</span>  <span class="hljs-keyword">index</span>.html <span class="hljs-keyword">index</span>.htm;
    }

    <span class="hljs-preprocessor">#error_page  404              /404.html;</span>

    <span class="hljs-preprocessor"># redirect server error pages to the static page /50x.html</span>
    <span class="hljs-preprocessor">#</span>
    error_page   <span class="hljs-number">500</span> <span class="hljs-number">502</span> <span class="hljs-number">503</span> <span class="hljs-number">504</span>  /<span class="hljs-number">50</span>x.html;
    location = /<span class="hljs-number">50</span>x.html {
        root   html;
    }

    <span class="hljs-preprocessor"># proxy the PHP scripts to Apache listening on 127.0.0.1:80</span>
    <span class="hljs-preprocessor">#</span>
    <span class="hljs-preprocessor">#location ~ /.php$ {</span>
    <span class="hljs-preprocessor">#    proxy_pass   http://127.0.0.1;</span>
    <span class="hljs-preprocessor">#}</span>

    <span class="hljs-preprocessor"># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span>
    <span class="hljs-preprocessor">#</span>
    <span class="hljs-preprocessor">#location ~ /.php$ {</span>
    <span class="hljs-preprocessor">#    root           html;</span>
    <span class="hljs-preprocessor">#    fastcgi_pass   127.0.0.1:9000;</span>
    <span class="hljs-preprocessor">#    fastcgi_index  index.php;</span>
    <span class="hljs-preprocessor">#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;</span>
    <span class="hljs-preprocessor">#    include        fastcgi_params;</span>
    <span class="hljs-preprocessor">#}</span>

    <span class="hljs-preprocessor"># deny access to .htaccess files, if Apache's document root</span>
    <span class="hljs-preprocessor"># concurs with nginx's one</span>
    <span class="hljs-preprocessor">#</span>
    <span class="hljs-preprocessor">#location ~ //.ht {</span>
    <span class="hljs-preprocessor">#    deny  all;</span>
    <span class="hljs-preprocessor">#}</span>
}</code>

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

vue-docker 文件结构:

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

Docker打包

<code><span class="hljs-comment"># docker build -t vue-docker . </span></code>

运行

<code><span class="hljs-comment"># docker run -d -p 9000:9000 vue-docker</span></code>

查看运行结果

<code><span class="hljs-comment"># docker ps</span></code>

这里写图片描述

如上部署就全部完成

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

这里写图片描述

写在后面

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

未经允许不得转载:冰点网络 » Docker 通过Nginx镜像部署Vue项目

分享到:更多 ()

评论 抢沙发

评论前必须登录!