docker封装node项目
通过docker封装node项目
项目架构
项目根目录创建Dockfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19# 使用官方 Node.js 镜像作为基础镜像
FROM node:18
# 设置工作目录
WORKDIR /app
# 复制 package.json 和 package-lock.json 并安装依赖
COPY package.json package-lock.json ./
RUN npm install
# 复制项目文件到容器中
COPY . .
# 暴露 3000 端口
EXPOSE 3000
# 启动应用
CMD ["node", "src/index.js"]编写docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31version: '3.3'
services:
node-app:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- DB_HOST=mysql
- DB_USER=xyx
- DB_PASSWORD=xyx
- DB_NAME=hospital
depends_on:
- mysql
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: hospital
volumes:
- mysql_data:/var/lib/mysql
- ./hospital.sql:/docker-entrypoint-initdb.d/hospital.sql
- ./create_user.sql:/docker-entrypoint-initdb.d/create_user.sql # 添加用户创建脚本
volumes:
mysql_data:强烈建议不使用root用户链接数据库,这种方式可能链接不上,所以通过下面的脚本来创建新用户并赋予远程登录权限
1
2
3
4--- create_user.sql
CREATE USER 'xyx'@'%' IDENTIFIED BY 'xyx';
GRANT ALL PRIVILEGES ON *.* TO 'xyx'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;修改node中链接数据库的方式
此项目为修改config.js文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16// config.js
// 启动配置
module.exports = {
port: 3000, // express 服务启动端口
/* 数据库相关配置 */
db: {
host: process.env.DB_HOST, // 主机名
port: 3306, // MySQL 默认端口为 3306
user: process.env.DB_USER, // 使用 用户登入 MySQL
password: process.env.DB_PASSWORD, // MySQL 密码,用你自己的
database: process.env.DB_NAME, // 使用数据库
timezone: "08:00",
}
}创建并启动容器
1
docker-compose up --build
这会创建两个容器
访问3000端口,项目部署成功
将镜像推送到远程仓库
给镜像打标记
需要指定用户名
1
docker tag node-mysql_node-app xyx138/node-mysql:latest
效果如下
1
2
3
4
5
6
7
8
9
10
11
12
13(base) wslxyx2@LAPTOP-KA17J01D:~/node-mysql$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
node-mysql_node-app latest d0b11dbffa50 46 minutes ago 1.11GB
xyx138/node-mysql latest d0b11dbffa50 46 minutes ago 1.11GB
test/ubuntu v1.0 86345dfffe74 42 hours ago 63.2MB
myip latest 99d0c0376652 42 hours ago 77.5MB
nginx alpine cb8f91112b6b 13 days ago 47MB
nginx latest 7f553e8bbc89 13 days ago 192MB
busybox latest 27a71e19c956 2 weeks ago 4.27MB
node 18 2e348d98bd63 3 months ago 1.09GB
mysql 5.7 5107333e08a8 10 months ago 501MB
ubuntu 18.04 f9a80a55f492 16 months ago 63.2MB
xyx138/ubuntu 18.04 f9a80a55f492 16 months ago 63.2MB推送
先登录
1
docker login
再推送
1
docker push xyx138/node-mysql:latest
结果如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15(base) wslxyx2@LAPTOP-KA17J01D:~/node-mysql$ docker push xyx138/node-mysql:latest
The push refers to repository [docker.io/xyx138/node-mysql]
96f006e40c42: Pushed
0188bc205d6c: Pushed
10ebccf26eb4: Pushed
3301608ced23: Pushed
69bd3a0bcbca: Pushed
9c2e3cad5dd9: Pushed
46468d86f161: Pushed
f48867d95974: Pushed
2bce433c3a29: Pushed
f91dc7a486d9: Pushed
3e14a6961052: Pushed
d50132f2fe78: Pushed
latest: digest: sha256:e3b52adc153af027a8c3f0ab8fbe40f788c3bc0759df143bd10fa1d2f9945207 size: 2840
docker封装node项目
http://xyxblog.com/2024/10/15/docker封装node项目/