Vue Nginx Docker 项目部署

Vue Nginx Docker项目部署

1.打包Vue项目

npm run build //执行命令生成dist目录

把生成的dist目录放到服务器上,譬如 /Projectj,下文就以这个目录来讲故事

2.配置Nginx

在dist目录同级目录下,也就是/Project目录下创建nginx.conf文件。
以下是nginx.conf文件内容:


user  root;
worker_processes  1;

#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;

    server {
        listen       8077;
        server_name  http://192.168.1.110;

        #charset koi8-r;

#        access_log  logs/host.access_accs.log;

#        location / {
#            root   html;
#            index  index.html index.htm;
#        }

        location / {
            root   /usr/share/nginx/html/dist;
            try_files $uri $uri/ /index.html; #解决页面刷新404问题
            index  index.html index.htm;
        }

        location /api_accs/ {
            proxy_pass  http://192.168.1.110:8101/;
        }

#        location /haier/{
#		# 后端的真实接口
#          proxy_pass https://haiyunyutest-jm.haierbiomedical.com/;
#	   }
        #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;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3.配置Docker

在dist目录同级目录下,也就是/Project目录下创建dockerfile文件。
以下是dockerfile文件内容:

FROM nginx

#RUN rm /etc/nginx/conf.d/default.conf
#
#ADD default.conf /etc/nginx/conf.d/
#
#COPY dist/ /root/Download/songmingyu_Docker_test/nginx/

RUN rm -f /etc/nginx/nginx.conf \
    && rm -f /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/nginx.conf

RUN mkdir /usr/share/nginx/html/dist 
COPY dist /usr/share/nginx/html/dist

ENTRYPOINT ["nginx", "-g", "daemon off;"]
3.1创建docker image 镜像
docker build -t accsv4-image .

上面的命令是创建一个镜像,accsv4-image 是镜像的名称; 点号 “.” 表示使用当前目录中的dockerfile规则来创建。
生成image镜像后,查看生成的镜像

docker image ls
3.2创建并运行docker容器
docker run --name accsv4-container -p 8077:8077 -d accsv4-image

accsv4-container是容器名称
-p之后的端口号,服务器外部访问的端口 :容器内部端口
accsv4-image是镜像名称
-d 后台运行容器并返回容器id

Vue项目的Docker+Nginx部署可以通过以下步骤完成: 1. 创建Dockerfile:在Vue项目的根目录下创建一个名为Dockerfile的文件,内容如下: ``` # 使用Node作为基础镜像 FROM node:14 as build-stage # 设置工作目录 WORKDIR /app # 复制package.json和package-lock.json到工作目录 COPY package*.json ./ # 安装依赖 RUN npm install # 复制所有文件到工作目录 COPY . . # 构建项目 RUN npm run build # 使用Nginx作为基础镜像 FROM nginx:1.21-alpine # 将构建好的项目复制到Nginx的默认静态文件目录 COPY --from=build-stage /app/dist /usr/share/nginx/html # 复制Nginx配置文件到容器中 COPY nginx.conf /etc/nginx/conf.d/default.conf # 暴露80端口 EXPOSE 80 # 启动Nginx服务 CMD ["nginx", "-g", "daemon off;"] ``` 2. 创建Nginx配置文件:在Vue项目的根目录下创建一个名为nginx.conf的文件,内容如下: ``` server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; try_files $uri $uri/ /index.html; } } ``` 3. 构建Docker镜像:在终端中进入Vue项目的根目录,执行以下命令构建Docker镜像: ``` docker build -t vue-app . ``` 其中,`vue-app`是镜像的名称,可以根据需要自行修改。 4. 运行Docker容器:执行以下命令运行Docker容器,并将容器的80端口映射到主机的指定端口(例如8888): ``` docker run -d -p 8888:80 vue-app ``` 其中,`8888`是主机的端口号,可以根据需要自行修改。 至此,Vue项目的Docker+Nginx部署就完成了。你可以通过访问`http://localhost:8888`来查看部署后的项目。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值