安装前提

Docker是基于linux的,在win10中安装wsl2:Windows Subsystem for Linux,让win10能够原生运行Linux二进制可执行文件的兼容层,且不会产生传统虚拟机或双启动设置开销。

总的来说类似安装一个 Windows 的子系统,在子系统里面运行 Linux 、Docker,并且以此为运行 VS code 的环境,就像在 Linux或Docker镜像 环境开发啦。

微软安装wsl2教程 或者安装:Hyper-V 微软开发的虚拟机

管理员 PowerShell 或 Windows 命令提示符中用命令操作,直接 wsl --install 就可以。

如果没完成不小心关了窗口...需要指定安装 分发:wsl --install -d Ubuntu

PS C:\Windows\system32> wsl --install -d Ubuntu
正在下载: Ubuntu
安装过程中出现错误。分发名称: 'Ubuntu' 错误代码: 0x80072f78]
PS C:\Windows\system32> wsl --list --online
以下是可安装的有效分发的列表。
请使用“wsl --install -d <分发>”安装。 NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
openSUSE-42 openSUSE Leap 42
SLES-12 SUSE Linux Enterprise Server v12
Ubuntu-16.04 Ubuntu 16.04 LTS
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
PS C:\Windows\system32> wsl -l -v
适用于 Linux 的 Windows 子系统没有已安装的分发版。
可以通过访问 Microsoft Store 来安装分发版:
https://aka.ms/wslstore
PS C:\Windows\system32> wsl --install -d Ubuntu
正在下载: Ubuntu
安装过程中出现错误。分发名称: 'Ubuntu' 错误代码: 0x80072f78]
PS C:\Windows\system32> wsl --install -d Ubuntu
正在下载: Ubuntu
安装过程中出现错误。分发名称: 'Ubuntu' 错误代码: 0x80072f78]
PS C:\Windows\system32> wsl --install -d Ubuntu
正在下载: Ubuntu
安装过程中出现错误。分发名称: 'Ubuntu' 错误代码: 0x80072eff]
PS C:\Windows\system32> wsl --install -d Ubuntu
正在下载: Ubuntu
正在安装: Ubuntu
已安装 Ubuntu。
正在启动 Ubuntu…
PS C:\Windows\system32>

安装过程中出现错误。分发名称: 'Ubuntu' 错误代码: 0x80072f78]  这个是因为网络问题,就和访问 GitHub 类似。

启动 Ubuntu 过后稍等一下,提示设置用户名和密码,完事!

Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: fyfh
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details. Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64) * Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage System information as of Sat May 28 20:13:43 CST 2022 System load: 0.52 Processes: 7
Usage of /home: unknown Users logged in: 0
Memory usage: 38% IPv4 address for eth0: 192.168.0.108
Swap usage: 1% 0 updates can be installed immediately.
0 of these updates are security updates. The list of available updates is more than a week old.
To check for new updates run: sudo apt update This message is shown once once a day. To disable it please create the
/home/fyfh/.hushlogin file.
fyfh@DESKTOP-UP9N3U4:~$

在开始菜单会有 Ubuntu 启动图标。

安装 Docker Desktop WSL 2 backend

微软教程安装docker并使用vscode连接   Docker官方说明

下载 Docker Desktop

安装 Docker Desktop , 提示启用 WSL 2 代替 Hyper-V 和 添加桌面快捷方式,勾上即可(360阻止程序,全允许了,文件关联开启启动啥的)。安装成功按钮要求登出,保存好文件,点击。

PS:第一次运行 Docker Desktop 提示 WSL 2 installation is incomplete ... wsl2安装未完成,不知道是不是没装完关了窗口的原因,要求下载安装 Linux 内核更新包后重启 Docker,do as it says.

设置里面 选中使用基于 WSL 2 的引擎复选框。

进入powershell输入

docker run hello-world

会下载一个 简单镜像,正常运行就安装好啦!

PS C:\Windows\system32> docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
Status: Downloaded newer image for hello-world:latest Hello from Docker!
This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal. To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/ For more examples and ideas, visit:
https://docs.docker.com/get-started/

一些设置

WSL 可以在 v1 或 v2 模式下运行分发,确保分发在 WSL 2 模式下运行。

要检查 WSL 模式

wsl -l -v

将现有的 Linux 发行版升级到 v2

wsl --set-version (发行版名称) 2

将 v2 设置为将来安装的默认版本

wsl --set-default-version 2

当 Docker Desktop 启动时,转到Settings > Resources > WSL Integration,可以看到已经安装的发行版,只有 Ubuntu (可以勾上),Docker-WSL 集成将在默认 WSL 发行版上启用。

更改默认 WSL 发行版

wsl --set-default <distro name>

将 Ubuntu 设置为默认 WSL 发行版:wsl --set-default ubuntu

修改Docker Engine配置,增加镜像仓库地址

{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"features": {
"buildkit": true
},
"registry-mirrors": [
"https://xxxxx.mirror.aliyuncs.com",
"http://hub-mirror.c.163.com",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn"
]
}

registry-mirrors 镜像地址,阿里云,网易,Docker中国区官方镜像地址,中国科技大学地址。

更改路径使应用获得最佳的文件系统性能

Docker 默认安装在c盘且安装过程中不可修改安装位置,安装、运行镜像挂载文件和win c盘在一起会影响性能,移动到其他盘符。

删除所有容器(或在图形界面操作)

docker rm $(docker ps -a -q)

关闭所有发行版

wsl --shutdown

指定盘符文件夹(提前创建好文件夹),备份已有image数据

wsl --export docker-desktop-data D:\docker-desktop\docker-desktop-data.tar

注销当前的docker-desktop-data发行版

wsl --unregister docker-desktop-data

指定盘符文件位置(D:\docker-desktop\data),导入先前的备份

wsl --import docker-desktop-data D:\docker-desktop\data D:\docker-desktop\docker-desktop-data.tar --version 2

重新运行即可。

PS:到此处,即可在 VS code 中安装 Remote Containers 扩展,然后在左侧扩展小图标找到它,远程资源管理器,点击,出来选择 WSL Targets ,就会看见默认的 Ubuntu

点击红线上按钮,打开新的 VS code 窗口,既是已 Ubuntu 为环境。不过此时打开文件,是没有项目文件的,也不好访问 Ubuntu 外面(主机)的文件,用其他的扩展好像可以(Remote Development 一系列微软出的扩展),没测试。

下面安装其他镜像容器,并且配置映射主机文件到容器内,方便每次使用。

deepo 比较大,可以在 https://hub.docker.com/ ,上搜索其他很小的镜像,专门的内置了 mysql 的, redis 的,或者 golang 的,很小,可以几个一起跑。

安装deepo镜像CPU版

powershell 中

docker pull ufoym/deepo:cpu

设置、启动容器:

docker run -it -p 8088:88 --name docker-go -v D:/workspace:/home/workspace ufoym/deepo:cpu
  • -it   -t 让docker分配一个伪终端并绑定到容器的标准输入上, -i  让容器的标准输入保持打开。不加 -it ,命令执行了会自动退出。因为如果没有衔接输入流,马上就结束了。加 -it 后docker命令会为容器分配一个伪终端,并接管其stdin/stdout支持交互操作,这时候bash命令不会自动退出。
  • -p  表示对端口号进行映射,即将 docker 容器的 88 号端口映射到宿主机的 8088 端口。
  • --name 将容器命名为 docker-go,指定名称方面下次运行,不指定则会随机命名。
  • -v D:/workspace:/home/workspace 将宿主机的 D:/workspace 目录映射到容器的 /home/workspace 目录,方便容器和宿主机间共享数据,可以共享多个目录(-v D:/workspace:/home/workspace -v D:/other:/other)
  • ufoym/deepo:cpu 镜像名称

或者用图形界面启动,填写参数,左侧 images 镜像选项,能看到所有下载的镜像,点击镜像对应的 Run 上面的配置按钮,就是没让配置 容器的端口,不过无所谓啦。

填好配置过后 Run ,会跳到  containers 栏,看到刚命名的  docker-go 已经跑起来了。

在 VS code 中安装 Docker 扩展、Remote Containers 扩展,然后点击左侧 docker 图标,在 container 栏 右键 已经启动的容器,点击 Attach Visual Studio Code,新弹出的 VS code 界面既是在启动的容器环境中了,并且打开文件夹选择时可以看到主机映射进去的工作目录

下次使用直接按容器名称运行

docker container start docker-go

或者图形界面保存的有,点击按钮直接运行。

或者就都在 VS code 里面操作!此时 Docker 扩展 和 Remote Containers(选择 containers) 扩展 里面都可以直接启动、打开容器 VS code,前提是运行期 Docker Desktop。

PS:如果不安装 Remote Containers,会没有 Attach Visual Studio Code 选项,只有一个 Attach Shell ,会打开操作容器的终端。

容器环境配置

新开的 VS code 窗口相当于在容器里打开,而容器里面不能直接使用外面 VS code 安装的扩展,其他的类似GO环境、Flutter环境,都需要在容器里面安装。

扩展分为了本地和容器内

安装容器扩展,正常搜索,然后点击安装

其他的,就在VS code 终端里面用命令安装了。

Windows 安装 Docker 并使用 VS code 连接的更多相关文章

  1. windows使用docker运行mysql等工具(一)windows安装docker

    由于我本地的mysql版本比较低,最近想着要升级一下mysql版本,鉴于docker容器的火热,就想着在本地装个docker环境,然后下载最新版的mysql镜像,完成mysql8.0的安装.电脑是wi ...

  2. [已解决]windows安装docker的问题

    windows下载安装docker出现的问题 进入powershell后输入docker --version报错: could not read CA certificate "C:\\Us ...

  3. Windows安装Docker & Docker-Compose & 配置docker私有仓库

    一定要给windows先创建软连接,不然系统盘会爆表的: mklink /j .docker D:\Administrator\.docker Win7安装Docker Dockerfile # FR ...

  4. windows 安装docker报错:Error checking TLS connection: ssh command error: command : ip addr show

    今天安装docker部署的时候总是再报这个错误. 报错的原因是初始化的时候出错了. 在docker 安装目录下有一个文件,如下图所示 将它复制到你电脑用户名目录下生成.docker 的文件夹中,如下图 ...

  5. windows安装docker

    主要參考:http://docs.docker.com/installation/windows/ [1]安装完毕后同意后可能会报错: error in run: Failed to start ma ...

  6. Windows安装docker (带安装包)

    docker安装包链接 链接:https://pan.baidu.com/s/1JBk8GCH6j_WeGdoaUuIoWw 提取码:8kgg 我电脑上有了git所以没有勾选最后一个 安装完成后将此目 ...

  7. windows 安装 docker

    .https://download.docker.com/win/stable/InstallDocker.msi .设置环境变量 C:\Program Files (x86)\Git\bin .如果 ...

  8. Windows 安装 docker 以及1709的简单使用

    PS C:\> Install-Module -Name DockerMsftProvider -Repository PSGallery -Force PS C:\> Install-P ...

  9. windows安装docker,快捷启动方式无法启动

    1.在双击“Docker Quickstart Terminal”时弹出缺少快捷方式,截图如下 2.单机快捷方式查看属性,发现配置的git位置是有问题的 现在只需要把git的正确地址配置好就可以了 现 ...

  10. windows使用docker运行mysql等工具(二)安装运行mysql

    今天接着上一篇的内容继续来学习安装运行mysql.建议先阅读第一篇:windows安装docker 一 查看mysql版本 如果想知道mysql镜像具体有哪几个版本,需要去docker hub查看. ...

随机推荐

  1. [C#]C++/CLI中interior_ptr和pin_ptr的区别

    interior_ptr 当垃圾回收器移动对象时,Interior pointer能随之移动,并始终指向该对象. 但是如果把这个指针返回给外部函数,那么当垃圾回收时(垃圾回收期间会压缩对象,),对象地 ...

  2. Ansible 学习笔记 - 定位主机和组的模式

    中英文对照表 英文 中文 备注 host 主机 group (主机)组 pattern 模式 ad hoc 特别命令 playbook 剧本 Ansible 专有名词,一段复杂的编排 inventor ...

  3. 分布式协议与算法-Quorum NWR

    1.强一致性与最终一致性 1.1强一致性 强一致性能保证写操作完成后,任何后续访问都能读到更新后的值:强一致性可以保证从库有与主库一致的数据.如果主库突然宕机,我们仍可以保证数据完整.但如果从库宕机或 ...

  4. redisConfig+redisUtil开箱即用

    RedisConfig @Configuration public class RedisConfig { //编写redisTemplate //固定模板,开箱即用 @Bean public Red ...

  5. 多线程之Semaphore登录限流示例

    public static void main(String[] args) { //允许最大的登录数 int slots=10; ExecutorService executorService = ...

  6. springcloud 08 Hystrix图形化DashBoard

    #1.构建一个服务监控模块 ##1.1创建模块cloud-consumer-hystrix-dashboard9001 ##1.2pom文件依赖 <dependencies> <!- ...

  7. Dapr v1.10.0 版本已发布

    Dapr是一套开源.可移植的事件驱动型运行时,允许开发人员轻松立足云端与边缘位置运行弹性.微服务.无状态以及有状态等应用程序类型.Dapr能够确保开发人员专注于编写业务逻辑,而不必分神于解决分布式系统 ...

  8. Eureka 注册中心和服务提供者

    什么是Eureka组件 spring cloud Eureka ,提供服务注册和服务发现的功能. 一:spring cloud Eureka Eureka Server 服务端 Eureka Clie ...

  9. 抛砖系列之k8s HorizontalPodAutoscaler(HPA)

    前言 "大伙得眼里有活,看见同事忙的时候要互相帮助,这样我们团队才能快速成长,出成绩,多干点活没坏处的,领导都看在眼里记在心里,不会亏待大伙." 看到这也许你还有点懵,不是要讲k8 ...

  10. LG P2839 [国家集训队]middle

    \(\text{Solution}\) 不考虑起点区间和终点区间的限制,求区间中位数 可以二分中位数,大于等于中位数的位置赋为 \(1\),小于的位置赋 \(-1\) 当区间和大于等于 \(0\) 时 ...