在 Ubuntu 上使用 Docker 进行远程访问,常见有几种方式,下面按推荐程度和使用场景给你说明。
适合:绝大多数情况
sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh
ssh user@ubuntu_ip
docker ps
docker run hello-world
✅ 优点
适合:CI/CD、开发环境
⚠️ 默认 Docker 只允许本地访问
sudo vim /etc/docker/daemon.json
内容示例(仅内网使用):
{
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
}
sudo systemctl restart docker
docker -H tcp://ubuntu_ip:2375 ps
或在本地配置:
export DOCKER_HOST=tcp://ubuntu_ip:2375
⚠️ 风险
适合:生产环境
步骤简述:
tlsverifydocker --tlsverify \
--tlscacert=ca.pem \
--tlscert=client-cert.pem \
--tlskey=client-key.pem \
-H tcp://ubuntu_ip:2376 ps
✅ 优点:安全
❌ 缺点:配置复杂
适合:多主机管理
docker context create remote \
--docker "host=tcp://ubuntu_ip:2375"
docker context use remote
docker ps
docker run -d -p 9000:9000 portainer/portainer
浏览器访问:http://ubuntu_ip:9000
| 场景 | 推荐方式 |
|---|---|
| 普通远程管理 | SSH + Docker |
| 开发/CI | Docker TCP(内网) |
| 生产环境 | TLS / SSH |
| 多主机 | Docker Context |
如果你能告诉我:
我可以给你最合适的完整配置教程。