在Docker中运行Vue.js项目(开发环境/生产环境)

Logo

新冠疫情自我检测系统网页设计开发文档

Sylvan Ding 的第一个基于 Vue.js 的项目. 本项目所提供的信息,只供参考之用,不保证信息的准确性、有效性、及时性和完整性,更多内容请查看国家卫健委网站!
Explore the docs »

View Demo · Report Bug · Request Feature

homepage

在Docker中运行Vue.js项目(开发环境/生产环境)

Next, we will run our Vue.js app in a Docker container.

Environment

Ubuntu18.04.4
Docker20.10.18
Docker Compose2.10.2

Installation

# Install using the convenience script
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun
docker -v # 20.10.18
docker compose version # 2.10.2
# Change registry mirrors
vim /etc/docker/daemon.json
# input the following JSON: {"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/", "https://registry.docker-cn.com"]}
sudo systemctl daemon-reload
sudo systemctl restart docker
docker info
# Registry Mirrors:
#  https://docker.mirrors.ustc.edu.cn/
#  https://registry.docker-cn.com/
sudo docker run hello-world
# Unable to find image 'hello-world:latest' locally
# latest: Pulling from library/hello-world
# 2db29710123e: Pull complete
# Status: Downloaded newer image for hello-world:latest
# Hello from Docker!
# This message shows that your installation appears to be working correctly.

Build Images and Run Containers of Vue.js App

We create Dockerfile for both ‘dev’ and ‘prod’ environment in the root folder of our project. Add a .dockerignore to speed up the Docker build process as our local dependencies and git repo will not be sent to the Docker daemon.

docs
images
README.md
.prettierrc
node_modules
.git
.gitignore
static/.gitkeep

Dev

We start by creating a Dockerfile.dev in the root folder of our project:

# Dockerfile.devFROM node:14.16.1WORKDIR /app# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH# overwrite Dev Server settings `host` in `config/index.js`
ENV HOST 0.0.0.0# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./# copy `patches` before npm runs post-install script
COPY patches ./patches# update npm and install project dependencies
RUN npm i npm@8.18.0 -g
RUN npm i vue-cli -g --legacy-peer-deps
RUN npm install --legacy-peer-depsEXPOSE 8080# configure anonymous volume in order to 
# use the container version of the “node_modules” folder
VOLUME "/app/node_modules"CMD ["npm", "run", "dev"]

ENV HOST 0.0.0.0 sets the environment variable HOST to the value 0.0.0.0, which overwrites Dev Server settings host in config/index.js. If you keep original settings host: 'localhost', our Vue.js app in a docker container will not be accessible from outside. By inspecting container exposed port docker container port dockerize-vuejs-app-dev, we get the output 8080/tcp -> 0.0.0.0:8080, which means the docker container are listening to inner port 0.0.0.0:8080 instead of localhost:8080. To be more specific, when apps run in a Docker container the IP 127.0.0.1 is assigned to the Docker container, not the host. Changing it to 0.0.0.0 will allow us to directly access the app from the host.

COPY patches ./patches aims at providing patches before npm runs post-install script. After patching, you will get the output: Applying patches... element-ui@2.15.9 ✔.

It may seem reduntant to first copy package.json and package-lock.json and then all project files and folders in two separate steps but there is actually a very good reason for that (spoiler: it allows us to take advantage of cached Docker layers).

Now let’s build the Docker image of our Vue.js app:

docker build -f Dockerfile.dev -t dockerize-vuejs-app:dev .

Finally, let’s run our Vue.js app in a Docker container:

docker run -it -p 8080:8080 -v ${PWD}:/app --rm --name dockerize-vuejs-app-dev dockerize-vuejs-app:dev

-v ${PWD}:/app mounts our code into the container at “/app” to enable “Hot Reload”. ${PWD} is /path/to/your/project, which may not work on Windows. ( See this Stack Overflow question for more info. )

You should be able to access our Vue.js app on localhost:8080 on your host machine. The logs are as follows:

 DONE  Compiled successfully in 12336ms                                                            3:04:24 AMI  Your application is running here: http://0.0.0.0:8080I  Your Express app is listening on port 8081N  © Sylvan Ding 2022 <sylvanding@qq.com>N  https://github.com/sylvanding/

Prod

For realistically complex production use cases, it may be wiser to stand on the shoulders of some giant like NGINX or Apache and that is exactly what we are going to do next: we are about to leverage NGINX to serve our Vue.js app because it is considered to be one of the most performant and battle-tested solutions out there.

We refactor our Dockerfile.dev to use NGINX:

# Dockerfile.prod# build stage
FROM node:14.16.1 as build-stage
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package*.json ./
COPY patches ./patches
RUN npm i npm@8.18.0 -g
RUN npm i vue-cli -g --legacy-peer-deps
RUN npm install --legacy-peer-deps
COPY . .
RUN npm run build# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80CMD ["nginx", "-g", "daemon off;"]

Now let’s build the Docker image of our Vue.js app:

docker build -f Dockerfile.prod -t dockerize-vuejs-app:prod .

Finally, let’s run our Vue.js app in a Docker container:

docker run -it -p 80:80 --rm --name dockerize-vuejs-app-prod dockerize-vuejs-app:prod

We should be able to access our Vue.js app on http://localhost or http://yourPublibIPAddress. Note that you need to open 80 port in your firewall. To run a container in background, we use -d flag instead of --rm:

docker run -d -p 80:80 --name dockerize-vuejs-app-prod dockerize-vuejs-app:prod

Stop and remove that container:

docker rm -f dockerize-vuejs-app-prod

View Nginx Access Logs

Type the following command helps us view Nginx real-time access logs shown on the background container’s virtual screen:

docker attach --sig-proxy=false dockerize-vuejs-app-prod

What happens there?

  • docker attach attaches your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.
  • --sig-proxy=false prevents CTRL-c from sending a SIGINT to the container. It allows you to detach from the container with a -d flag and leave it running Nginx continuously by using the CTRL-c key sequence.

转载请注明出处:©️ Sylvan Ding 2022

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/484523.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

所有竞争,本质都是“硬核能力”之争

来源&#xff1a;中科创星文 &#xff1a;君莫笑轮值主编&#xff1a;智勇 值班编辑 &#xff1a;金木研一、科技创新&#xff0c;是世界进步的第一推动力站在5000年后的今天&#xff0c;回首人类的发展进程&#xff0c;从石器时代—青铜时代—铁器时代—蒸汽时代—电气时代—信…

解读OC中的load和initialize

在 Objective-C 中&#xff0c;NSObject 是绝大多数类的基类。而在 NSObject 中有两个类方法 load 和 initialize&#xff0c;那这两个方法是在什么时机被调用呢&#xff1f;父类、Category 的调用顺序又是怎样的呢&#xff1f;本文解读一下这两个方法的区别于联系及使用场景。…

深度报告:芯片设计EDA 2.0时代,三大路径搞定六大挑战

编辑&#xff1a;智东西内参EDA是Electronic Design Automation的缩写&#xff0c;几十年来成为芯片设计模块、工具、流程的代称。从仿真、综合到版图&#xff0c;从前端到后端&#xff0c;从模拟到数字再到混合设计&#xff0c;以及工艺制造等&#xff0c;EDA工具涵盖了芯片设…

2018上海

转载于:https://www.cnblogs.com/kakaisgood/p/10158442.html

python-类方法和属性

#类方法&#xff0c;属性和迭代器 #为了类是新型的&#xff0c;应该把语句__metaclass__type放在模块的开始&#xff0c; #或者子类化内建类object class newtype(object):pass#构造方法&#xff1a;一个对象创建后&#xff0c;会立即调用构造方法&#xff0c;即__init__ class…

taro中子父传值

其实网上很多方法,我这只是一个简单的demo,废话不多说直接上代码 import Taro, { Component } from tarojs/taro import { View, Text } from tarojs/components import ./index.less//子组件 class Child extends Component{constructor(props) {super(props);this.state ({}…

如何计算感受野(Receptive Field)

深度神经网络中的感受野(Receptive Field) 本文转载自知乎&#xff1a;深度神经网络中的感受野(Receptive Field) - 蓝木达的文章 - 知乎 在机器视觉领域的深度神经网络中有一个概念叫做感受野&#xff0c;用来表示网络内部的不同位置的神经元对原图像的感受范围的大小。神经元…

物理学需要哲学,哲学需要物理学

来源&#xff1a;微信公众号“Philosophia 哲学社”撰文&#xff1a;卡洛罗威利&#xff08;Carlo Rovelli&#xff09;翻译&#xff1a;朱科夫「反对哲学」是一位当代伟大的物理学家——诺贝尔物理奖得主、基本粒子物理「标准模型」的奠基人斯蒂文温伯格&#xff08;Steven We…

python-迭代器和生成器

property函数可以用0&#xff0c;1&#xff0c;3或4个参数来调用。如果没用参数&#xff0c;产生的属性既不可读&#xff0c;也不可写。如果只使用一个参数调用(一个取值方法)&#xff0c;产生的属性是只读的。第三个参数(可选)用于删除特性的方法(它不要参数)。第四个参数(可选…

[转帖]Oracle 11G RAC For Windows 2008 R2部署手册

Oracle 11G RAC For Windows 2008 R2部署手册&#xff08;亲测&#xff0c;成功实施多次&#xff09; https://www.cnblogs.com/yhfssp/p/7821593.html 总体规划 服务器规划 1、建议使用两台硬件配置一模一样的服务器来作为 RAC 环境的两个物理节点 2、服务器至少需要配置两块物…

生日快乐送女朋友的网页生日礼物模版

生日快乐送女朋友的网页生日礼物模版 Demo: http://sylvanding.online/happy-birthday-20221120 仓库&#xff1a;https://github.com/sylvanding/happy-birthday-20221120 参考 背景 https://codepen.io/arcs/pen/XKKYZW蛋糕 https://codepen.io/fixcl/pen/AaBNZB卡片 http…

美国独步世界的八大领域

来源&#xff1a;世界先进制造技术论坛一、美国诺贝尔奖获得者世界第一

python-文件和流

#标准库导入自己的模块:import syssys.path.append("模块路径")import sys sys.path.append(x:/Users/Dell10/Desktop/test.py)#告诉解释器还需要导入的模块路径&#xff0c;注意反斜杆的写法 import test#导入模块&#xff0c;导入时创建了.pyc文件 #模块导入类似以…

spoj839 Optimal Marks(最小割,dinic)

题目大意&#xff1a; 给你一个无向图\(G(V,E)\)。 每个顶点都有一个int范围内的整数的标记。 不同的顶点可能有相同的标记。 对于边\((u,v)\)&#xff0c;我们定义\(Cost(u,v)mark [u]\ \ xor\ \ mark [v]\)。 现在我们知道某些节点的标记了。你需要确定其他节点的标记&#x…

Hinton的胶囊网络不太行?CVPR Oral论文:不比卷积网络更「强」

来源&#xff1a;机器学习研究组订阅在一篇 CVPR 2021 Oral 论文中&#xff0c;来自慕尼黑大学、微软亚研的研究者对胶囊网络和卷积网络进行了全面的对比。一系列实验表明&#xff0c;一些被认为对胶囊网络&#xff08;CapsNet&#xff09;至关重要的设计组件实际上会损害它的鲁…

Python程序生成.exe的可执行文件

Python程序生成.exe的可执行文件 1&#xff09;编写生成exe的脚本程序setup.py #codingutf-8 #exe文件生成脚本 from distutils.core import setup import py2exe setup(console[test.py])#test.py为应用程序文件 2&#xff09;将应用程序test.py和脚本程序放在同一个文件夹下A…

数量庞大!中国成长型AI企业研究报告

来源&#xff1a;德勤Deloitte编辑&#xff1a;蒲蒲日前&#xff0c;德勤、英特尔和深圳人工智能行业协会联合发布《中国成长型AI企业研究报告》。该报告通过对数千家成长型AI企业数据的分析研究&#xff0c;几百家企业的走访以及和近百家优秀企业的深度合作&#xff0c;就中国…

Python+Selenium基础篇之2-打开和关闭火狐浏览器

本节介绍如何初始化一个webdriver实例对象driver&#xff0c;然后打开和关闭firefox浏览器。要用selenium打开fiefox浏览器。首先需要去下载一个driver插件geckodriver.exe&#xff0c; 下载地址https://github.com/mozilla/geckodriver/releases&#xff0c;下载好这个exe文件…

Oracle 数据库

Oracle 12c 数据库学习记录&#xff1a;study 1&#xff1a; 0.Oracle例程服务的启动net start oracleserviceorcl 1.查看数据库的初始化参数&#xff1a;通常登录sys用户&#xff1a;sqlplus / as sysdba&#xff1b;2. 查看内存方面的参数(模糊匹配)&#xff1a;show paramet…

除了芯片,我们还应关注这六大核心技术!

来源&#xff1a;疯狂机械控企业想发展自己还是需要掌握核心技术。除了芯片以外&#xff0c;还有哪些核心技术是我们重点关注的呢&#xff1f;01 软件定义机器▼设备智能化的体现就是典型的软件定义机器&#xff0c;包括机器轻松连接至互联网&#xff1b;将APP和分析结果嵌入机…