docker 部署多个前端vue项目

一、docker 部署前端项目方案
1. 方案1

一个docker容器对应一个前端项目
使用Dockerfile构建镜像,而镜像内部使用nginx,最后把前端构建好的静态文件放到nginxhtml目录下面就可
多个前端项目依次创建多个docker容器即可

2. 方案2

使用一个docker容器部署多个前端项目
在构建之前规划好按照不同路径访问前端项目,在配置文件中配置访问路径即可,
举例:
项目1 /emos-vue
项目2 /emos-vue2
以此类推…

在这里插入图片描述
然后,将多个构建后的前端项目静态目录上传到nginx的html文件夹中,前端访问按照规划好的路径访问即可。
在这里插入图片描述

这样好处是不用浪费资源,缺点是项目之间还是有耦合度

二、Nginx配置运行
2.1. 拉取nginx镜像

首先下载安装Nginx镜像,这里我依然使用特定版本的镜像。

docker pull nginx:1.21.3
2.2. 创建配置目录

在root目录中,创建nginx文件夹。然后把课程git上面“其他”目录中的nginx.conf文件,上传到该目录。并且创建html文件夹,把index.html和50x.html文件上传到该目录。

具体文件内容:见文章末尾附录

mkdir /root/nginx/html -p

在这里插入图片描述

2.3. 创建Nginx容器

执行下面的命令,创建Nginx容器,然后用浏览器访问云主机的80端口,可以看到Nginx的欢迎画面。

docker run -it -d --name nginx -m 200m --net=host \
-v /root/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /root/nginx/html:/usr/share/nginx/html:ro \
-e TZ=Asia/Shanghai \
nginx:1.21.3

在这里插入图片描述

在这里插入图片描述

三、部署前端项目
3.1. 压缩

把 dist.zip 文件上传到 /root/nginx/html 目录中,然后执行unzip命令解压缩。
在这里插入图片描述
在这里插入图片描述

3.2. 上传
#进入到html目录
cd /root/nginx/html

#解压缩文件夹
unzip dist.zip

#删除压缩文件
rm -rf dist.zip

#给文件夹改名
mv dist emos-vue

在这里插入图片描述

3.3. 验证

打开浏览器,访问 http://云主机IP:80/emos-vue ,如果能看到登陆页面,说明程序部署成功

在这里插入图片描述

附录
index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

50x.html
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>

nginx.conf

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;

    include /etc/nginx/conf.d/*.conf;
}
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Docker 是一种流行的容器技术,可以用来部署不同的应用程序,包括前端应用程序。Vue 是一种流行的前端框架,用于构建现代化的 SPA 单页面应用程序。当我们使用 Docker部署 Vue 应用程序时,我们需要将 Vue 应用程序打包到 Docker 镜像中,并设置端口映射以便能够通过 Web 浏览器访问该应用程序。 Vue 应用程序通常使用 Node.js 作为运行时环境,因此我们可以使用 Node.js Docker 镜像来部署 Vue 应用程序。下面是一个示例 Dockerfile: ``` FROM node:14.16.0-alpine3.13 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 8080 8081 8082 CMD ["npm", "run", "serve"] ``` 在上面的 Dockerfile 中,我们首先从 Node.js 14.16.0-alpine3.13 镜像中构建一个容器。工作目录被设置为 /app。然后我们将 package.json 和 package-lock.json 文件复制到容器中,并运行 npm install 安装所有依赖。复制所有其他文件后,我们设置 EXPOSE 指令以将端口 8080、8081 和 8082 映射到宿主环境。最后,我们可以使用 CMD 指令启动和运行 Vue 应用程序。 在 Docker 容器中运行 Vue 应用程序后,我们可以使用 docker run 命令将容器映射到计算机上的端口。例如,如果我们在本地计算机上使用端口 8000 作为映射端口,我们可以运行以下命令: ``` docker run -p 8000:8080 -p 8001:8081 -p 8002:8082 vue-app ``` 在上述命令中,-p 选项指定了端口映射。我们将容器中端口 8080、8081 和 8082 映射到宿主计算机上的端口 8000、8001 和 8002。vue-app 是我们构建的 Docker 镜像的名称。 总结来说,我们可以使用 Docker 容器来部署 Vue 应用程序,并使用端口映射将容器中的端口映射到宿主计算机上的端口。因此,我们可以在一个计算机上同时运行多个 Vue 应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gblfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值