被实验室前端项目整裂开的我想起来还有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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FROM node:12

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN yarn install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 3000
CMD [ "yarn", "start" ]

运行

运行只需要执行如下命令

1
2
docker build . -t <your tag username>
docker run -p 3000:3000 -d <your tag username>

.dockerignore

有些文件并不需要被加入docker镜像中,比如node_modulesnode_modules较大且会覆盖镜像中的依赖,因此需要忽略掉。完整.dockerignore文件见下。

1
2
3
4
5
node_modules
.idea
npm-debug.log*
yarn-debug.log*
yarn-error.log*

docker-compose.yml

有些时候我们需要将镜像进行服务化,因此需要编写docker-compoes.yml文件,在项目目录中新建docker-compose.yml文件,编写如下。

1
2
3
4
5
6
version: "3.9"
services:
web:
build: .
ports:
- "3000:3000"

有时候需要多个docker compose之间通信,此时可以先通过执行docker network ls命令来查找网络

1
2
3
4
5
6
7
8
# 运行结果
> docker network ls
NETWORK ID NAME DRIVER SCOPE
dfe18b9ff74e alsprj_lnmp bridge local
764431292ca3 bridge bridge local
99ba555f79f4 host host local
0fe0af6d1a07 live2d_api_default bridge local
2eaa1467e191 none null local

如果我想要当前项目与alsprj_lnmp处于同一网络环境,就只需在我要连接的docker-compose.yml中添加如下代码

1
2
3
4
networks:
default:
external:
name: alsprj_lnmp

运行

运行只需要执行如下命令

1
docker compose up -d

结果

通过docker,实验室前端项目总算跑起来了。

参考资料

把一个 Node.js web 应用程序给 Docker 化

Building Efficient Dockerfiles - Node.js

多个docker-compose之间的网络通信