通过 ssh 桥接方式连接远程 docker 中的 ubuntu

CListery ARE YOU OK?

很多时候我们的 docker 环境都不在本地,而通过 ssh 连接远程服务器然后在通过 docker 命令进入容器貌似又有点繁琐,所以直接一步到位搞个跳板可以让我们直接访问到容器

配置容器

DockerCompose

  • 配置 docker-compose.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    version: "3.8"

    services:
    ccc:
    image: ubuntu:20.04
    container_name: "ubuntu_2004"
    restart: always
    tty: true
    ports:
    - "9500:80"
    - "9501:22"
  • 运行

    1
    docker-compose up -d
  • 进入容器

    1
    docker exec -it ubuntu_2004 /bin/bash
  • 安装 openssh-server

    1
    apt update && apt install openssh-server
  • 配置 ssh 公钥

    1
    mkdir ~/.ssh && touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys

最后将公钥写入到 ~/.ssh/authorized_keys 文件中,就完成了容器配置

本地跳板配置

1
2
3
4
5
6
7
8
9
10
11
12
# 远程主机(跳板)
Host ubuntu
Port 22
User ubuntu
IdentityFile ~/.ssh/id_rsa
# docker 容器
Host docker_ubuntu
HostName 0.0.0.0
Port 9501
User root
IdentityFile ~/.ssh/id_rsa
ProxyCommand ssh -q -W %h:%p ubuntu

然后通过 ssh docker_ubuntu 命令就可以直接访问到远程 docker 容器了~

  • 标题: 通过 ssh 桥接方式连接远程 docker 中的 ubuntu
  • 作者: CListery
  • 创建于 : 2022-11-11 14:44:56
  • 更新于 : 2026-01-23 16:48:26
  • 链接: http://clistery.github.io/2022/11/11/docker/ssh-bridge/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
通过 ssh 桥接方式连接远程 docker 中的 ubuntu