docker部署vue项目

使用vue create hello命令创建一个名为hello的vue项目

在项目根目录新建一个文件,命名为Dockerfile
在这里插入图片描述

使用记事本打开Dockerfile文件,输入内容为:(注意如果你的项目名不是hello的话,记得将文件内容里的hello替换为你项目的项目名)

FROM node:12
COPY ./ /hello
WORKDIR /hello
RUN npm install && npm run build

FROM nginx
RUN mkdir /hello
COPY --from=0 /hello/dist /hello
COPY nginx.conf /etc/nginx/nginx.conf

继续在项目根目录创建.dockerignore文件,内容为:

**/node_modules
**/dist

继续在项目根目录创建nginx.conf文件,内容为:(同样是注意将hello修改为你自己项目的项目名)

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server {
      listen       80;
      server_name  localhost;
      location / {
          root   /hello;
          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;
      }
    }
}

将项目上传至云服务器(项目的dist以及node_modules文件夹不需要上传)

在远程服务器端

cd进入到项目根目录中

构建 Docker 镜像

docker build . -t hello

运行容器

docker run -d --name hello -p 9090:80 hello

OK,这样vue项目就在服务器上跑起来了,之后就可以访问查看效果了(云服务器对应端口记得开放哦)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_47659279/article/details/127191685