被实验室前端项目整裂开的我想起来还有docker这一方法
封面《PARQUET》
前言
运行实验室项目的时候发现有个node-sass
模块,安装的时候说需要python2环境。网上有解决方法是删除node-modules
,然后安装windows-build-tools
在重新装依赖。结果我试完发现不行,于是有说法说装最新版的node-sass
,装完后运行后报错说版本不兼容,直接把我气死。最后我忽然想起来还有docker
可以使用,因此写下此文,记录将node项目docker化。
docker化
Dockerfile
在项目里新建Dockerfile
文件,写下以下内容。项目中node-sass
的版本为4.12.0
,查询node-sass推荐的版本为node 12。此外还可以看到先拷贝package.json
文件而非全部文件,这是利用docker
的缓存,减少每次重建镜像的时间,解释见此
1 | FROM node:12 |
运行
运行只需要执行如下命令
1 | docker build . -t <your tag username> |
.dockerignore
有些文件并不需要被加入docker镜像中,比如node_modules
。node_modules
较大且会覆盖镜像中的依赖,因此需要忽略掉。完整.dockerignore
文件见下。
1 | node_modules |
docker-compose.yml
有些时候我们需要将镜像进行服务化,因此需要编写docker-compoes.yml
文件,在项目目录中新建docker-compose.yml
文件,编写如下。
1 | version: "3.9" |
有时候需要多个docker compose
之间通信,此时可以先通过执行docker network ls
命令来查找网络
1 | # 运行结果 |
如果我想要当前项目与alsprj_lnmp
处于同一网络环境,就只需在我要连接的docker-compose.yml
中添加如下代码
1 | networks: |
运行
运行只需要执行如下命令
1 | docker compose up -d |
结果
通过docker
,实验室前端项目总算跑起来了。
参考资料
把一个 Node.js web 应用程序给 Docker 化
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 qxdn的乐园!
评论
TwikooDisqus