【Vue】Docker + Nginx 部署 Vue3.0 项目

Docker + Nginx 部署 Vue3.0 项目

1、用指令 npm run build 打包vue.js项目(该项目是在WebStorm里面新建的Vue空项目)。
在这里插入图片描述
打包成功后,会生成一个目录dist
在这里插入图片描述

2、把该文件夹拷贝到服务器(操作系统是 Centos7)下的/usr/share目录下
在这里插入图片描述

3、在/usr/share目录下新建Dockerfile文件

# 设置基础镜像
FROM nginx
# 定义作者
MAINTAINER longdb
# 将dist文件中的内容复制到 /usr/share/nginx/html/ 这个目录下面
COPY dist/  /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/nginx.conf
RUN echo 'echo init ok!!'

4:在/usr/share目录下新建 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  www.longdb.com;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
     location / {
        root   /usr/share/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;
        }
 
        # 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;
    #    }
    #}
 
 
}

5:cd 进入share目录,然后 docker build -t longdb-vue:V1.0.0 .构建镜像(注意最后的 . )
在这里插入图片描述
6、运行生成成功的镜像:
docker run -p 8083:80 -d --name test_nginx_vue 581b4731eba6
参数讲解:

run: 创建一个新的容器并运行一个命令
-d: 后台运行容器,并返回容器ID
-p: 端口映射,格式为:主机(宿主)端口:容器端口
–name=“test_nginx_vue”: 为容器指定一个名称
581b4731eba6 --为生成好的images_id.

在这里插入图片描述

7、创建完成后,使用 ip地址:端口号,就可以访问我们部署的 Vue 项目了!
在这里插入图片描述

发布了610 篇原创文章 · 获赞 232 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/sinat_42483341/article/details/104597836
今日推荐