VsCode远程docker作为环境

状态: 已完成

  • 注意,切勿用在生产环境,仅在测试环境中使用

0x00 安装Docker

更新软件包索引,并且安装必要的依赖软件,来添加一个新的 HTTPS 软件源

sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

导入源仓库的 GPG key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

将 Docker APT 软件源添加到你的系统

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

安装 Docker 最新版本

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

查看状态

sudo systemctl status docker

以非 Root 用户身份执行 Docker

sudo usermod -aG docker $USER

$USER是一个环境变量,代表当前用户名

0x01 远程Docker开启端口

  • 注意,切勿用在生产环境,仅在测试环境中使用
  1. 创建文件 daemon.json 到目录 /etc/docker

    {"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]}
    
  2. 创建文件 /etc/systemd/system/docker.service.d/override.conf

  3. 重启docker

    systemctl daemon-reload
    systemctl restart docker.service
    

0x02 客户端生成 ssh 秘钥对

生成秘钥对id_rsa.pub id_rsa.pub

ssh-keygen

Your identification has been saved in C:\Users\admin/.ssh/id_rsa.
Your public key has been saved in C:\Users\admin/.ssh/id_rsa.pub.

把这个id_rsa.pub 传送到远程主机,复制成文件: ~/.ssh/authorized_keys

cat id_rsa.pub >> ~/.ssh/authorized_keys

本地运行客户端加上私钥

ssh-add id_rsa

0x02 本地配置 Docker 客户端

  • Linux/Mac

包管理器安装

  • Windows
  • 方法一:安装docker
  • 方法二:下载 docker.exe放到Path包含的路径

配置本地使用远程docker

docker context create <context name> --docker "host=ssh://<user>@<host>"

切换到上述context

docker context use <context name>

测试docker

docker info

示例

C:\Users\admin>docker context create docker --docker "host=ssh://user@192.168.235.140"
docker
Successfully created context "docker"

C:\Users\admin>docker context use docker
docker
Current context is now "docker"

C:\Users\admin>docker info
Client:
 Context:    docker
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.8

vscode安装docker插件

Untitled

此时本地可直接操作远程docker

PS C:\Users\admin> docker  ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
comments powered by Disqus