This commit is contained in:
编码猿
2025-09-09 09:11:29 +08:00
parent 01c6885a2b
commit 1909ff27ae
3 changed files with 90 additions and 14 deletions

72
.drone.yml Normal file
View File

@@ -0,0 +1,72 @@
kind: pipeline
type: docker
name: nodejs-host-network-pipeline
steps:
# 第一步:准备环境并安装依赖
- name: install-dependencies
image: node:18-bullseye
commands:
# 安装iproute2工具
- apt-get update && apt-get install -y iproute2
# 验证ip命令是否可用
- ip -V
# 安装项目依赖
- npm install
volumes:
- name: node_modules
path: /app/node_modules
# 第二步:运行代码检查或测试(可选)
- name: run-tests
image: node:18-bullseye
commands:
- npm test # 如果有测试脚本
volumes:
- name: node_modules
path: /app/node_modules
# 第三步构建Docker镜像
- name: build-image
image: docker:20.10
volumes:
- name: docker-socket
path: /var/run/docker.sock
- name: node_modules
path: /app/node_modules
commands:
- docker build -t node-app:${DRONE_COMMIT_SHA:0:8} -f Dockerfile .
# 第四步使用host网络运行应用
- name: run-with-host-network
image: docker:20.10
volumes:
- name: docker-socket
path: /var/run/docker.sock
commands:
- |
docker run -d \
--restart always \
--network host \
--name node-app-${DRONE_COMMIT_SHA:0:8} \
node-app:${DRONE_COMMIT_SHA:0:8}
when:
branch:
- main
# 定义卷用于共享依赖和Docker socket
volumes:
- name: node_modules
temp: {}
- name: docker-socket
host:
path: /var/run/docker.sock
# 构建触发条件
trigger:
branch:
- main
- develop
event:
- push
- pull_request

View File

@@ -1,20 +1,24 @@
# 使用基础镜像(根据需要选择合适的版本)
FROM debian:bullseye-slim # 或 ubuntu:22.04
FROM node:18-bullseye-slim
# 设置非交互模式,避免安装过程中出现交互提示
ENV DEBIAN_FRONTEND=noninteractive
# 更新软件源并安装iproute2同时清理缓存减小镜像体积
# 安装iproute2
RUN apt-get update && \
apt-get install -y --no-install-recommends \
iproute2 \ # 包含ip命令的工具包
&& \
apt-get install -y --no-install-recommends iproute2 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# 可选:设置工作目录
# 创建非root用户
RUN useradd -m appuser
USER appuser
WORKDIR /app
# 可选:添加启动命令
CMD ["bash"]
# 复制项目文件和依赖
COPY --chown=appuser:appuser package*.json ./
COPY --chown=appuser:appuser node_modules ./node_modules
COPY --chown=appuser:appuser . .
# 暴露应用端口host模式下实际使用宿主机端口
EXPOSE 9900
# 启动命令
CMD ["npm", "start"]

View File

@@ -8,7 +8,7 @@
},
"devDependencies": {},
"scripts": {
"start": "node-dev index.js"
"start": "node index.js"
},
"keywords": [],
"author": "",