ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Windows 安装 Docker 并使用 vscode 连接

2022-05-29 02:00:07  阅读:264  来源: 互联网

标签:Windows wsl vscode Ubuntu -- Docker 安装 docker


安装前提

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

微软安装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

重新运行即可。

 

安装deepo镜像CPU版

docker pull ufoym/deepo:cpu

 

 

使用 vscode 连接Docker

在vscode中安装 docker 扩展,powershell 中启动容器:

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

 

在 vscode 中 docker 选项中连接 container 栏 右键 启动的容器,点击 Attach Visual Studio Code,新弹出的 vscode 界面既是在启动的容器环境中了。

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

docker container start docker-go

 


docker pull ufoym/deepo:cpu

标签:Windows,wsl,vscode,Ubuntu,--,Docker,安装,docker
来源: https://www.cnblogs.com/jiayouba/p/16321687.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有