001-Docker基础复习

一、基础概念类

  1. Docker 是什么?与虚拟机有何本质区别?

    • 详解:Docker 是基于容器的轻量级虚拟化技术,通过共享宿主机内核实现进程级隔离。相比虚拟机(需完整操作系统,资源占用大),Docker 启动更快(秒级)、资源占用更低(镜像仅 MB 级),但隔离性较弱(依赖 Linux Namespace/CGroups)。
    • 参考Docker 与虚拟机区别详解
  2. Docker 三大核心组件及其关系?

    • 详解
      • 镜像(Image):只读模板(如 nginx:alpine),含应用环境与代码。
      • 容器(Container):镜像的运行实例,含可写层。
      • 仓库(Registry):存储镜像的平台(如 Docker Hub)。
      • 关系:仓库 → 下载镜像 → 创建容器 。
    • 参考三大核心解析
  3. 为何使用 Docker?核心优势是什么?

    • 详解
      • 环境一致性:镜像打包应用+依赖,避免“开发-生产环境差异”。
      • 资源高效:容器共享内核,资源利用率比虚拟机高 70%+。
      • 快速部署:秒级启动,支持 CI/CD 流水线。
    • 参考Docker 核心优势分析

二、核心操作类

  1. Dockerfile 中 COPYADD 指令的区别?

    • 详解
      • COPY:仅复制文件/目录到镜像(如 COPY app.py /app)。
      • ADD:额外支持自动解压压缩包、从 URL 下载(不推荐,需手动清理缓存)。
    • 参考Dockerfile 指令详解
  2. 如何持久化容器数据?

    • 详解
      • 数据卷(Volume)docker run -v /host/path:/container/path,数据存于宿主机,容器重启不丢失。
      • 数据卷容器:专用容器管理卷,供其他容器挂载 。
    • 参考数据存储管理
  3. 容器常见状态及转换命令?

    • 状态createdrunningpaused/exiteddead
    • 命令
      1
      docker start/stop/pause/unpause/rm <container_id>

三、网络与存储

  1. Docker 网络模式有哪些?适用场景?

    模式 特点 场景
    bridge (默认) 容器通过 docker0 网桥通信 多容器隔离环境(如 Web 应用)
    host 共享宿主机网络栈 高性能场景(如负载测试)
    overlay 跨主机容器通信(Swarm/K8s) 集群部署
  2. 如何实现跨主机容器通信?

    • 详解
      • Overlay 网络:基于 VXLAN 封装数据包,通过 UDP 传输(端口 4789)。
      • Macvlan:为容器分配独立 MAC 地址,直连物理网络 。

四、容器编排与集群

  1. Docker Compose vs Docker Swarm vs Kubernetes?

    • 对比
      工具 定位 适用场景
      Docker Compose 单机多容器编排 本地开发环境
      Docker Swarm 轻量级集群管理 中小规模生产部署
      Kubernetes 企业级容器编排 大规模微服务架构
    • 参考编排工具对比
  2. 如何用 Docker Compose 定义服务依赖?

    • 示例
      1
      2
      3
      4
      5
      6
      services:
      web:
      depends_on:
      - db # 确保 db 先启动
      db:
      image: postgres
    • 参考Compose 文件规范

五、实战调优与排错

  1. 如何优化 Docker 镜像体积?

    • 技巧
      • 多阶段构建(移除编译依赖)。
      • 使用 Alpine 等轻量基础镜像。
      • 合并 RUN 指令减少镜像层 。
  2. 查看容器日志的方法?

    • 命令
      1
      2
      docker logs -f <container_id>  # 实时日志
      docker inspect --format='{{.LogPath}}' <container_id> # 日志文件路径
    • 参考日志管理

六、高阶原理

  1. Docker 如何实现资源隔离?

    • 技术栈
      • Namespace:隔离进程、网络、文件系统等(如 PIDNET)。
      • CGroups:限制 CPU/内存用量(如 docker run --cpus=2 --memory=1g)。
  2. 容器退出后数据会丢失吗?

    • 详解:默认停止的容器仍保留可写层(docker ps -a 可见),但删除容器后数据丢失,需用数据卷持久化 。

附:完整学习资源


002-Jenkins基础

直接附一个常用Jenkins模板吧,看文档就能懂

实现 stage 级别的并行和 steps 级别的并行双重嵌套/使用Matrix 进行资源利用率的最大化

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
pipeline {
agent none
options {
timeout(time: 2, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
}

environment {
ARTIFACTORY = 'https://artifactory.example.com'
VERSION = sh(script: 'git describe --tags --always', returnStdout: true).trim()
BUILD_DATE = new Date().format('yyyyMMdd-HHmmss')
}

parameters {
choice(name: 'DEPLOY_ENV', choices: ['dev', 'staging', 'prod'], description: 'Select deployment environment')
booleanParam(name: 'RUN_INTEGRATION_TESTS', defaultValue: true, description: 'Run integration tests')
string(name: 'CUSTOM_TAG', defaultValue: '', description: 'Optional custom tag for deployment')
}

stages {
stage('Initialize') {
agent any
steps {
script {
currentBuild.displayName = "#${BUILD_NUMBER}-${VERSION}-${params.DEPLOY_ENV}"
echo "Build Version: ${VERSION}"
echo "Build Date: ${BUILD_DATE}"
echo "Selected Environment: ${params.DEPLOY_ENV}"

// 初始化构建矩阵
def buildMatrix = [:]
buildMatrix['Linux'] = { buildForPlatform('linux') }
buildMatrix['Windows'] = { buildForPlatform('windows') }
buildMatrix['Mac'] = { buildForPlatform('mac') }

// 保存供后续阶段使用
env.BUILD_MATRIX = writeJSON returnText: true, json: buildMatrix.keySet() as List
}
}
}

stage('Parallel Build & Test') {
parallel {
stage('Build Multiplatform') {
stages {
stage('Build') {
steps {
script {
def builds = [:]
readJSON(text: env.BUILD_MATRIX).each { platform ->
builds["Build ${platform}"] = {
node(platform.toLowerCase()) {
checkout scm
buildForPlatform(platform.toLowerCase())
}
}
}
parallel builds
}
}
}

stage('Unit Tests') {
steps {
script {
def tests = [:]
readJSON(text: env.BUILD_MATRIX).each { platform ->
tests["Test ${platform}"] = {
node(platform.toLowerCase()) {
runUnitTests(platform.toLowerCase())
}
}
}
parallel tests
}
}
}
}
}

stage('Integration Tests') {
when { expression { params.RUN_INTEGRATION_TESTS } }
agent { label 'integration' }
stages {
stage('Prepare Environment') {
steps {
sh 'docker-compose up -d'
sleep time: 3, unit: 'MINUTES'
}
}

stage('Run Tests') {
parallel {
stage('API Tests') {
steps {
runIntegrationTests('api')
}
}
stage('UI Tests') {
steps {
runIntegrationTests('ui')
}
}
stage('Load Tests') {
steps {
runLoadTests()
}
}
}
}

stage('Cleanup') {
steps {
sh 'docker-compose down'
}
}
}
}

stage('Static Analysis') {
agent { label 'analysis' }
steps {
parallel {
stage('Code Quality') {
steps {
runSonarQubeAnalysis()
}
}
stage('Security Scan') {
steps {
runDependencyCheck()
}
}
}
}
}
}
}

stage('Package & Archive') {
agent any
steps {
script {
def packages = [:]
readJSON(text: env.BUILD_MATRIX).each { platform ->
packages["Package ${platform}"] = {
node(platform.toLowerCase()) {
packageArtifacts(platform.toLowerCase())
}
}
}
parallel packages
}

archiveArtifacts artifacts: '**/target/*.zip,**/target/*.tar.gz', fingerprint: true
stash name: 'deployment-artifacts', includes: 'deploy/**'
}
}

stage('Approval') {
when { expression { params.DEPLOY_ENV in ['staging', 'prod'] } }
steps {
script {
def approvers = getApproversForEnvironment(params.DEPLOY_ENV)
input message: "Approve deployment to ${params.DEPLOY_ENV}?",
ok: 'Deploy',
submitter: approvers.join(',')
}
}
}

stage('Deploy') {
parallel {
stage('Deploy to Infrastructure') {
agent { label params.DEPLOY_ENV }
steps {
unstash 'deployment-artifacts'
script {
deployToEnvironment(params.DEPLOY_ENV)
}
}
}

stage('Update CDN') {
when { expression { params.DEPLOY_ENV == 'prod' } }
agent { label 'cdn' }
steps {
updateCDN()
}
}

stage('Notify') {
steps {
script {
notifyDeploymentStatus()
}
}
}
}
}
}

post {
always {
script {
cleanWs()
notifyBuildCompletion(currentBuild.result)
}
}
success {
updateBuildStatus('SUCCESS')
}
failure {
updateBuildStatus('FAILURE')
}
unstable {
updateBuildStatus('UNSTABLE')
}
}
}

// 自定义方法定义
def buildForPlatform(platform) {
echo "Building for ${platform}"
switch(platform) {
case 'linux':
sh './gradlew build -Pplatform=linux'
break
case 'windows':
bat 'gradlew build -Pplatform=windows'
break
case 'mac':
sh './gradlew build -Pplatform=mac'
break
default:
error "Unsupported platform: ${platform}"
}
}

def runUnitTests(platform) {
echo "Running unit tests on ${platform}"
// 实现细节...
}

def runIntegrationTests(type) {
echo "Running ${type} integration tests"
// 实现细节...
}

def runLoadTests() {
echo "Running load tests"
// 实现细节...
}

def packageArtifacts(platform) {
echo "Packaging artifacts for ${platform}"
// 实现细节...
}

def deployToEnvironment(env) {
echo "Deploying to ${env} environment"
// 实现细节...
}

def notifyDeploymentStatus() {
echo "Notifying deployment status"
// 实现细节...
}

def getApproversForEnvironment(env) {
return env == 'prod' ? ['prod-admins@example.com'] : ['qa-team@example.com']
}

def updateBuildStatus(status) {
echo "Updating external systems with build status: ${status}"
// 实现细节...
}

def notifyBuildCompletion(result) {
echo "Build completed with status: ${result}"
// 实现细节...
}