解决 Nomad 报错 failed to start task failed to create container
分析 HashiCorp Nomad Client 启动 Docker 驱动任务时报 failed to create container 的排查与解决方案。
问题现象
在 Nomad 提交 Job 后,Allocation 状态处于 failed,事件列表中记录:
Task "api" failed
Recent Events:
* Driver Failure: failed to start task "api": failed to create container: Error response from daemon: Conflict. The container name "/api-e4f8d2..." is already in use by container "...".
或出现:
Driver Failure: failed to start task "api": failed to create container: invalid volume specification
触发根因
- 宿主机 Docker 守护进程中存在同名僵尸容器未正常清理。
- Job 文件中声明的
volumes宿主机挂载目录不存在或权限不足。 - 容器资源限制参数(如内存、CPU 配额)超出了 Docker 或系统 cgroup 支持的范围。
解决步骤与操作命令
步骤一:清理宿主机残留的冲突容器
登录分配失败的 Nomad Client 节点:
docker ps -a | grep Exited
# 清理已停止的残余容器
docker container prune -f
步骤二:验证挂载目录与宿主机权限
检查 nomad job 配置文件中的 volume 映射段:
task "api" {
driver = "docker"
config {
image = "my-registry/app:v1"
volumes = [
"/data/app/logs:/logs"
]
}
}
确保宿主机 /data/app/logs 目录已提前创建且具备写权限:
sudo mkdir -p /data/app/logs
sudo chmod 777 /data/app/logs
步骤三:重新调度 Allocation
在管理机上强制重启失败的任务分配:
nomad alloc stop <ALLOC_ID>
# 或者重新运行 job
nomad job run jobspec.nomad
验证确认
执行状态追踪:
nomad alloc status <ALLOC_ID>
确认任务状态转为 running,端口健康检查通过。


