基于 SOC 职业分类
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/microwind/ai-skills --skill docker命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
| name | Docker容器化 |
| description | 当实施Docker容器化时,分析容器架构设计,优化容器性能,解决容器相关问题。验证容器配置,设计微服务架构,和最佳实践。 |
| license | MIT |
Docker容器化是现代应用部署的核心技术。不当的容器化会导致资源浪费、性能问题和安全风险。在设计容器化方案前需要仔细分析应用需求。
核心原则: 好的容器化应该提升部署效率和可移植性,同时保证资源利用率。坏的容器化会增加运维复杂性,甚至影响应用性能。
始终:
触发短语:
问题:
容器资源配置不合理,导致性能问题
错误示例:
- CPU和内存限制过高或过低
- 没有设置资源限制
- 忽略资源使用监控
- 不合理的重启策略
解决方案:
1. 根据应用需求设置合理资源限制
2. 实施资源监控和告警
3. 优化容器启动参数
4. 配置合适的重启策略
问题:
Docker网络配置不当导致通信问题
错误示例:
- 使用默认桥接网络
- 端口映射冲突
- DNS解析问题
- 网络安全配置缺失
解决方案:
1. 使用自定义网络
2. 合理规划端口映射
3. 配置DNS服务
4. 实施网络隔离和安全策略
问题:
容器数据持久化和存储管理不当
错误示例:
- 数据存储在容器内部
- 没有备份策略
- 存储卷权限问题
- 存储空间不足
解决方案:
1. 使用数据卷持久化数据
2. 实施定期备份策略
3. 正确配置存储权限
4. 监控存储使用情况
import docker
import json
import time
import psutil
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass
from collections import defaultdict
@dataclass
class ContainerMetrics:
"""容器指标"""
container_id: str
name: str
status: str
cpu_usage: float
memory_usage: float
memory_limit: float
network_io: Dict[str, int]
block_io: Dict[str, int]
pid_count: int
uptime: float
@dataclass
class ContainerIssue:
"""容器问题"""
container_id: str
severity: str # critical, high, medium, low
type: str
message: str
suggestion: str
metric_value: Optional[float] = None
class DockerContainerAnalyzer:
def __init__():
.client = docker.from_env()
.containers: [ContainerMetrics] = []
.issues: [ContainerIssue] = []
() -> [, ]:
:
containers = .client.containers.(=)
container containers:
metrics = .analyze_container(container.)
metrics:
.containers.append(metrics)
report = {
: (containers),
: ([c c containers c.status == ]),
: .containers,
: .issues,
: .generate_resource_summary(),
: .generate_recommendations(),
: .calculate_health_score()
}
report
Exception e:
{: }
() -> [ContainerMetrics]:
:
container = .client.containers.get(container_id)
stats = container.stats(stream=)
cpu_usage = .calculate_cpu_usage(stats)
memory_usage, memory_limit = .calculate_memory_usage(stats)
network_io = .calculate_network_io(stats)
block_io = .calculate_block_io(stats)
pid_count = stats.get(, {}).get(, )
uptime = .calculate_uptime(container)
metrics = ContainerMetrics(
container_id=container.,
name=container.name,
status=container.status,
cpu_usage=cpu_usage,
memory_usage=memory_usage,
memory_limit=memory_limit,
network_io=network_io,
block_io=block_io,
pid_count=pid_count,
uptime=uptime
)
.check_container_issues(metrics)
metrics
Exception e:
()
() -> :
:
cpu_stats = stats.get(, {})
precpu_stats = stats.get(, {})
cpu_delta = cpu_stats.get(, {}).get(, ) - \
precpu_stats.get(, {}).get(, )
system_cpu_delta = cpu_stats.get(, ) - \
precpu_stats.get(, )
system_cpu_delta > :
cpu_usage = (cpu_delta / system_cpu_delta) * \
(cpu_stats.get(, {}).get(, [])) *
:
cpu_usage =
(cpu_usage, )
Exception:
() -> [, ]:
:
memory_stats = stats.get(, {})
memory_usage = memory_stats.get(, )
memory_limit = memory_stats.get(, )
memory_usage, memory_limit
Exception:
,
() -> [, ]:
:
networks = stats.get(, {})
total_rx = (net.get(, ) net networks.values())
total_tx = (net.get(, ) net networks.values())
{
: total_rx,
: total_tx,
: total_rx + total_tx
}
Exception:
{: , : , : }
() -> [, ]:
:
blkio_stats = stats.get(, {})
io_service_bytes = blkio_stats.get(, [])
total_read = (item.get(, ) item io_service_bytes
item.get() == )
total_write = (item.get(, ) item io_service_bytes
item.get() == )
{
: total_read,
: total_write,
: total_read + total_write
}
Exception:
{: , : , : }
() -> :
:
container.status == :
info = container.attrs
started_at = info.get(, {}).get(, )
started_at:
start_time = time.strptime(started_at[:], )
uptime = time.time() - time.mktime(start_time)
uptime
Exception:
() -> :
metrics.cpu_usage > :
.issues.append(ContainerIssue(
container_id=metrics.container_id,
severity=,
=,
message=,
suggestion=,
metric_value=metrics.cpu_usage
))
metrics.memory_limit > :
memory_percent = (metrics.memory_usage / metrics.memory_limit) *
memory_percent > :
.issues.append(ContainerIssue(
container_id=metrics.container_id,
severity=,
=,
message=,
suggestion=,
metric_value=memory_percent
))
metrics.status == :
.issues.append(ContainerIssue(
container_id=metrics.container_id,
severity=,
=,
message=,
suggestion=
))
metrics.pid_count > :
.issues.append(ContainerIssue(
container_id=metrics.container_id,
severity=,
=,
message=,
suggestion=,
metric_value=metrics.pid_count
))
() -> [, ]:
.containers:
{}
total_cpu = (c.cpu_usage c .containers)
total_memory = (c.memory_usage c .containers)
total_memory_limit = (c.memory_limit c .containers c.memory_limit > )
running_containers = [c c .containers c.status == ]
{
: total_cpu,
: total_memory,
: total_memory_limit,
: (total_memory / total_memory_limit * ) total_memory_limit > ,
: (running_containers),
: total_cpu / (running_containers) running_containers ,
: total_memory / (running_containers) running_containers
}
() -> [[, ]]:
recommendations = []
issue_counts = defaultdict()
issue .issues:
issue_counts[issue.] +=
issue_counts[] > :
recommendations.append({
: ,
: ,
: ,
:
})
issue_counts[] > :
recommendations.append({
: ,
: ,
: ,
:
})
resource_summary = .generate_resource_summary()
resource_summary.get(, ) > :
recommendations.append({
: ,
: ,
: ,
:
})
recommendations
() -> :
.containers:
score =
issue .issues:
issue.severity == :
score -=
issue.severity == :
score -=
issue.severity == :
score -=
issue.severity == :
score -=
running_containers = ([c c .containers c.status == ])
total_containers = (.containers)
total_containers > :
running_ratio = running_containers / total_containers
score = score * running_ratio
(, (score))
:
():
.client = docker.from_env()
() -> [, ]:
:
networks = .client.networks.()
network_analysis = []
network networks:
analysis = {
: network.name,
: network.,
: network.attrs.get(, ),
: network.attrs.get(, ),
: (network.attrs.get(, {})),
: network.attrs.get(, ),
: []
}
analysis[] == analysis[] == :
analysis[].append({
: ,
: ,
:
})
analysis[] > :
analysis[].append({
: ,
: ,
:
})
network_analysis.append(analysis)
{
: (networks),
: network_analysis,
: .generate_network_recommendations(network_analysis)
}
Exception e:
{: }
() -> [[, ]]:
recommendations = []
bridge_networks = ([n n networks n[] == ])
overlay_networks = ([n n networks n[] == ])
bridge_networks > :
recommendations.append({
: ,
: ,
:
})
overlay_networks == (networks) > :
recommendations.append({
: ,
: ,
:
})
recommendations
:
():
.client = docker.from_env()
() -> [, ]:
:
volumes = .client.volumes.()
volume_analysis = []
volume volumes:
analysis = {
: volume.name,
: volume.attrs.get(, ),
: volume.attrs.get(, ),
: volume.attrs.get(, ),
: volume.attrs.get(, {}),
: .estimate_volume_usage(volume),
: []
}
analysis[] == :
analysis[].append({
: ,
: ,
:
})
volume_analysis.append(analysis)
{
: (volumes),
: volume_analysis,
: .generate_storage_recommendations(volume_analysis)
}
Exception e:
{: }
() -> [, ]:
:
mountpoint = volume.attrs.get(, )
mountpoint os.path.exists(mountpoint):
stat = os.statvfs(mountpoint)
total = stat.f_blocks * stat.f_frsize
free = stat.f_bfree * stat.f_frsize
used = total - free
{
: total,
: used,
: free,
: (used / total * ) total >
}
Exception:
{: , : , : , : }
() -> [[, ]]:
recommendations = []
local_volumes = ([v v volumes v[] == ])
local_volumes > :
recommendations.append({
: ,
: ,
:
})
high_usage_volumes = [v v volumes v[].get(, ) > ]
high_usage_volumes:
recommendations.append({
: ,
: ,
:
})
recommendations
():
container_analyzer = DockerContainerAnalyzer()
container_report = container_analyzer.analyze_all_containers()
()
()
()
()
network_manager = DockerNetworkManager()
network_report = network_manager.analyze_networks()
()
()
storage_manager = DockerStorageManager()
storage_report = storage_manager.analyze_volumes()
()
()
__name__ == :
main()
import docker
import yaml
import json
from typing import List, Dict, Any, Optional
from pathlib import Path
class DockerContainerOptimizer:
def __init__(self):
self.client = docker.from_env()
self.optimizations = []
def optimize_container_config(self, container_name: str) -> Dict[str, Any]:
"""优化容器配置"""
try:
container = self.client.containers.get(container_name)
# 获取当前配置
current_config = self.get_container_config(container)
# 分析优化建议
optimization_plan = self.analyze_optimization_opportunities(current_config)
# 生成优化后的配置
optimized_config = self.generate_optimized_config(current_config, optimization_plan)
return {
'container_name': container_name,
'current_config': current_config,
'optimization_plan': optimization_plan,
'optimized_config': optimized_config,
'estimated_improvements': self.estimate_improvements(current_config, optimized_config)
}
Exception e:
{: }
() -> [, ]:
:
info = container.attrs
config = {
: container.name,
: info.get(, {}).get(, ),
: info.get(, {}).get(, []),
: info.get(, {}).get(, []),
: info.get(, {}).get(, {}),
: info.get(, []),
: info.get(, {}).get(, {}),
: {
: info.get(, {}).get(, ),
: info.get(, {}).get(, ),
: info.get(, {}).get(, )
},
: info.get(, {}).get(, ),
: info.get(, {}).get(, ),
: info.get(, {}).get(, )
}
config
Exception e:
Exception()
() -> [[, ]]:
opportunities = []
config[][] == :
opportunities.append({
: ,
: ,
: ,
: ,
:
})
config[][] == :
opportunities.append({
: ,
: ,
: ,
: ,
:
})
config[]:
opportunities.append({
: ,
: ,
: ,
: ,
:
})
config[]:
opportunities.append({
: ,
: ,
: ,
: ,
:
})
restart_policy = config.get(, {})
restart_policy.get() == :
opportunities.append({
: ,
: ,
: ,
: ,
:
})
config[] == :
opportunities.append({
: ,
: ,
: ,
: ,
:
})
sensitive_env_vars = []
env config[]:
(key env.upper() key [, , , ]):
sensitive_env_vars.append(env)
sensitive_env_vars:
opportunities.append({
: ,
: ,
: ,
: ,
:
})
opportunities
() -> [, ]:
optimized_config = current_config.copy()
opportunity opportunities:
opportunity[] == :
opportunity[] == :
optimized_config[][] = * *
opportunity[] == :
optimized_config[][] =
opportunity[] == :
opportunity[] == :
optimized_config[] =
opportunity[] == :
optimized_config[] =
opportunity[] == :
opportunity[] == :
optimized_config[] = {
: ,
:
}
opportunity[] == :
opportunity[] == :
optimized_config[] =
optimized_config
() -> [, ]:
improvements = {
: ,
: ,
: ,
:
}
current_config[] optimized_config[]:
improvements[] +=
current_config[] optimized_config[]:
improvements[] +=
current_config[][] == optimized_config[][] > :
improvements[] +=
improvements[] +=
restart_policy = current_config.get(, {}).get(, )
optimized_restart_policy = optimized_config.get(, {}).get(, )
restart_policy == optimized_restart_policy != :
improvements[] +=
current_config[] == optimized_config[] != :
improvements[] +=
improvements[] = (
improvements[] +
improvements[] +
improvements[]
) //
improvements
() -> [, ]:
:
(compose_file, , encoding=) f:
compose_content = yaml.safe_load(f)
optimizations = []
services = compose_content.get(, {})
service_name, service_config services.items():
service_optimizations = .analyze_service_config(service_name, service_config)
optimizations.extend(service_optimizations)
optimized_compose = .generate_optimized_compose(compose_content, optimizations)
{
: compose_content,
: optimizations,
: optimized_compose,
: {
: (optimizations),
: ([o o optimizations o[] == ]),
: ([o o optimizations o[] == ])
}
}
Exception e:
{: }
() -> [[, ]]:
optimizations = []
deploy_config = service_config.get(, {})
deploy_config.get():
optimizations.append({
: service_name,
: ,
: ,
: ,
:
})
service_config.get():
optimizations.append({
: service_name,
: ,
: ,
: ,
:
})
logging_config = service_config.get(, {})
logging_config.get():
optimizations.append({
: service_name,
: ,
: ,
: ,
:
})
optimizations
() -> [, ]:
optimized_compose = original_compose.copy()
services = optimized_compose.get(, {})
optimization optimizations:
service_name = optimization[]
service_config = services.get(service_name, {})
optimization[] == :
service_config:
service_config[] = {}
service_config[][] = {
: {
: ,
:
},
: {
: ,
:
}
}
optimization[] == :
service_config[] = {
: [, , , ],
: ,
: ,
:
}
optimization[] == :
service_config[] = {
: ,
: {
: ,
:
}
}
services[service_name] = service_config
optimized_compose[] = services
optimized_compose
():
optimizer = DockerContainerOptimizer()
container_optimization = optimizer.optimize_container_config()
()
opt container_optimization[]:
()
compose_optimization = optimizer.generate_docker_compose_optimization()
()
()
__name__ == :
main()