Docker Machine
Machine 是什么、为什么已被取代,以及用 Docker context 管理远程主机的现代做法。
Key PointsMachine 用来在远程主机上装 Docker 并生成客户端证书;它已停止维护,现在用 docker context + ssh。
它当年解决什么
One command installed Docker on a cloud VM or VirtualBox host and generated TLS certs; eval $(docker-machine env host) switched targets. Before multi-host orchestration, that was the main way to manage remote Docker.
Bash
docker-machine create --driver virtualbox dev
eval $(docker-machine env dev)
docker ps
docker-machine ls为什么不用了
- 2019 年起官方不再维护,最后一个版本 0.16.2
- 功能被
docker context与各家云厂商的工具取代 - Swarm 也不再是主流编排,Kubernetes 才是
现在的做法:context
Bash
docker context create remote --docker "host=ssh://deploy@10.0.0.5"
docker context use remote
docker ps # 实际跑在 10.0.0.5 上
docker context use defaultssh:// tunnels over SSH: no 2376 port, no certificate distribution — SSH access is enough.
对比
| Machine | Context | |
|---|---|---|
| Status | Deprecated | Supported |
| Transport | TCP + TLS certs | SSH or TCP |
| Setup | Installs Docker for you | Host must already have Docker |
If scripts still call
docker-machine env, docker context use is usually a one-line replacement.它当年解决什么
在云主机、VirtualBox 上一条命令装好 Docker 并配好 TLS 证书,然后用 eval $(docker-machine env host) 切换目标。在多机编排出现之前,这是管理远程 Docker 的主要方式。
Bash
docker-machine create --driver virtualbox dev
eval $(docker-machine env dev)
docker ps
docker-machine ls为什么不用了
- 2019 年起官方不再维护,最后一个版本 0.16.2
- 功能被
docker context与各家云厂商的工具取代 - Swarm 也不再是主流编排,Kubernetes 才是
现在的做法:context
Bash
docker context create remote --docker "host=ssh://deploy@10.0.0.5"
docker context use remote
docker ps # 实际跑在 10.0.0.5 上
docker context use defaultssh:// 走 SSH 隧道,不需要开放 2376 端口、也不需要单独发证书——只要有 SSH 权限就能用。
对比
| Machine | Context | |
|---|---|---|
| 状态 | 已废弃 | 官方支持 |
| 传输 | TCP + TLS 证书 | SSH 或 TCP |
| 准备 | 顺手帮你装 Docker | 主机需已装 Docker |
还有脚本在用
docker-machine env 的话,换成 docker context use 通常一行就能替换掉。