【Docker】WSL 2 上的 Docker 搭建和入门

这篇具有很好参考价值的文章主要介绍了【Docker】WSL 2 上的 Docker 搭建和入门。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

🛫 导读

开发环境

版本号 描述
文章日期 2023-12-14
操作系统 Win11 - 22H2 22621.2715
WSL 2
C:\Windows\System32>docker version
Client:
 Cloud integration: v1.0.35+desktop.5
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:08:44 2023
 OS/Arch:           windows/amd64
 Context:           default

Server: Docker Desktop 4.26.0 (130397)
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:08:02 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.25
  GitCommit:        d8f198a4ed8892c764191ef7b3b06d8a2eeb5c7f
 runc:
  Version:          1.1.10
  GitCommit:        v1.1.10-0-g18a0cb0
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

1️⃣ 安装

首先安装WSL,可以参考文章《【WSL】Windows下的Linux子系统使用方法指南 https://blog.csdn.net/kinghzking/article/details/134984077》。
否则,启动Docker,会报下面的错误:
wsl 安装centos docker,# Docker,docker,容器

安装Docker Desktop for Windows

官网教程:https://docs.docker.com/desktop/install/windows-install/

  1. 双击 Docker Desktop Installer .exe 以运行安装程序。

  2. 出现提示时,请确保选择或不选择“配置”页上的“使用 WSL 2 而不是 Hyper-V”选项,具体取决于所选的后端。

    如果您的系统仅支持这两个选项之一,您将无法选择要使用的后端。

  3. 按照安装向导上的说明授权安装程序并继续安装。

  4. 安装成功后,选择**“关闭**”以完成安装过程。

  5. 如果管理员帐户与用户帐户不同,则必须将用户添加到 docker-users 组。

    • 管理员身份运行**“计算机管理”**,
    • 然后导航到 “本地用户和组” > “组”> docker-users
    • 单击鼠标右键将用户添加到组中。
    • 注销并重新登录以使更改生效。

2️⃣ 环境配置

  1. Docker Desktop安装后,从 Windows 开始菜单启动 Docker Desktop,然后从任务栏的隐藏图标菜单中选择 Docker 图标。 右键单击该图标以显示 Docker 命令菜单,然后选择“设置”。
    wsl 安装centos docker,# Docker,docker,容器
  1. 确保在“设置”>“常规”中选中“使用基于 WSL 2 的引擎”。
    wsl 安装centos docker,# Docker,docker,容器
  1. 通过转到“设置”>“资源”>“WSL 集成”,从要启用 Docker 集成的已安装 WSL 2 发行版中进行选择。
    并打开 WSL 发行版(小编只有一个选项 Ubuntu)
    wsl 安装centos docker,# Docker,docker,容器

3️⃣ hello world

关于Docker,可以找到很多教程,这里推荐《【狂神说Java】Docker最新超详细版教程通俗易懂 https://www.bilibili.com/video/BV1og4y1q7M4》,真的很细致,庖丁解牛。
本小节以hello-world为例,简单了解下什么是Docker。

第一次运行

在命令行中执行docker run hello-world,将显示下面的内容,其流程整理如下:

  1. 本地查找名为hello-world:latestimage(未找到)
  2. 服务器下载image,显示其信息。
  3. 执行Container(会创建一个Container)
  4. 打印了额外的说明信息
C:\Windows\System32>docker run hello-world
# 1. 本地查找名为`hello-world:latest`的`images`(未找到)
Unable to find image 'hello-world:latest' locally

# 2. 服务器下载`image`,显示其信息。
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:3155e04f30ad5e4629fac67d6789f8809d74fea22d4e9a82f757d28cee79e0c5
Status: Downloaded newer image for hello-world:latest

# 3. 执行docker(会创建一个Container)
Hello from Docker!

# 4. 打印了额外的说明信息
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/

再次运行

在命令行中再次执行docker run hello-world,将显示下面的内容,内容与第一次不一样了。
其流程也发生了变换,整理如下:

  1. 本地查找名为hello-world:latestimage(找到了)
  2. 拉取本地image,创建一个新的Container。
  3. 执行新的Container
  4. 打印了额外的说明信息
docker run hello-world

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/

分析总结

  1. 两次执行只有一个image hello-world
    wsl 安装centos docker,# Docker,docker,容器
  1. 两次执行生成两个container,但是都使用的是同一个image hello-world,如下图:
    wsl 安装centos docker,# Docker,docker,容器

所以说,docker run 命令首先creates在指定映像上创建一个可写容器层,然后starts使用指定的命令。
如果想使用已经有的容器,执行docker start 容器Id或者 docker exec -it 容器Id /bin/bash命令。
效果如下:

C:\Windows\System32>docker start f8d2fef7a3f3e0e35319b5864dcf6ac1dc0e5b79a34dfa5d97c19de6adf2362b
f8d2fef7a3f3e0e35319b5864dcf6ac1dc0e5b79a34dfa5d97c19de6adf2362b

C:\Windows\System32>docker exec -it f8d2fef7a3f3e0e35319b5864dcf6ac1dc0e5b79a34dfa5d97c19de6adf2362b  /bin/bash
Error response from daemon: Container f8d2fef7a3f3e0e35319b5864dcf6ac1dc0e5b79a34dfa5d97c19de6adf2362b is not running

C:\Windows\System32>docker logs -tf f8d2fef7a3f3e0e35319b5864dcf6ac1dc0e5b79a34dfa5d97c19de6adf2362b
2023-12-14T15:20:21.141914310Z
2023-12-14T15:20:21.142064937Z Hello from Docker!
2023-12-14T15:20:21.142072020Z This message shows that your installation appears to be working correctly.
2023-12-14T15:20:21.142077010Z
2023-12-14T15:20:21.142081638Z To generate this message, Docker took the following steps:
2023-12-14T15:20:21.142085977Z  1. The Docker client contacted the Docker daemon.

附一张docker run流程图:
wsl 安装centos docker,# Docker,docker,容器文章来源地址https://www.toymoban.com/news/detail-797116.html

📖 参考资料

  • 【WSL】Windows下的Linux子系统使用方法指南 https://blog.csdn.net/kinghzking/article/details/134984077
  • WSL 2 上的 Docker 远程容器入门 https://learn.microsoft.com/zh-cn/windows/wsl/tutorials/wsl-containers
  • 【狂神说Java】Docker最新超详细版教程通俗易懂 https://www.bilibili.com/video/BV1og4y1q7M4
  • Windows系统 WSL安装 CentOS7 和docker https://www.jianshu.com/p/ed0d23e742d0

到了这里,关于【Docker】WSL 2 上的 Docker 搭建和入门的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • docker安装 unexpected wsl error

    很诡异的一个问题 大概现象和这个帖子很像 https://developer.aliyun.com/article/1395485 docker版本4.26.1 系统: windows 10 win+R 输入winver可以看见自己的版本号 华为matebook 16s 重装的Win10 首先按照这个哥们给的说法 https://zhuanlan.zhihu.com/p/666788196 把 控制面板-程序-程序和功能 配置成上面这个

    2024年01月18日
    浏览(37)
  • Windows系统安装WSL,并安装docker服务

    背景 因为工作需要,要在电脑上执行sh脚本,并启动docker服务执行具体逻辑。因为我的电脑是windows系统,对做本任务来说,比较吃力,所以想到使用wsl,让windows电脑具有linux电脑的能力。 什么是 WSL 2 WSL 2 是适用于 Linux 的 Windows 子系统体系结构的一个新版本,它支持适用于

    2024年02月07日
    浏览(30)
  • 在 Windows 上安装 WSL 和 Docker

    在这个简短的教程中,我们将在 Windows 11(或 Windows 10)上安装 WSL 和 Docker。Docker 允许我们在不影响我们的主操作系统的情况下运行容器化应用程序。安装 WSL 和 Docker 后,我们将运行“hello-world”容器来确认 docker 是否正常运行。 这些步骤适用于 Windows 11 和 Windows 10(内部版本

    2024年02月10日
    浏览(24)
  • 基于WSL2+Docker+VScode搭建机器学习(深度学习)开发环境

    内容概述 :由于最近配发了新的工作电脑但不想装双系统,因此通过本博文来记录基于Windows子系统WSL+Docker搭建机器学习与深度学习开发环境的流程步骤,同时记录该过程中所遇到的相关问题及解决方案。期待为同行学习者提供参考; 最终效果 :在Windows电脑上,无需安装

    2024年02月04日
    浏览(31)
  • 本地wsl的Ubuntu安装docker,不使用docker桌面版

    建议大家去docker官网那边看 Install Docker Engine on Ubuntu | Docker Docs 选1就对了

    2024年04月28日
    浏览(28)
  • 【WSL】Ubuntu 22.04 安装配置docker

    WSL就是个坑! WSL就是个坑! WSL就是个坑! 我第一次安装使用 Ubuntu 还是第一台笔记本,装了双系统,版本是18.04 LTS,但是我那个时候只有机械硬盘,因此 Ubuntu 桌面十分的卡。 兜兜转转,大三的时候发现微软的 WSL2 很好用,想装一个学习 Linux,当然选择了那个时候最新的

    2024年02月08日
    浏览(41)
  • 2023保姆级:深度学习环境在Docker上搭建(基于Linux和WSL)

    在深度学习领域,配置环境的常见做法是使用conda来实现环境隔离。conda是一个广泛使用的工具,用于创建和管理特定的环境,通过在不同的环境中设置特定的Python环境变量路径,实现了环境的特异化。为了实现这一目的,需要将终端置于conda的管理之下。如果你已经安装了

    2024年02月06日
    浏览(34)
  • windows10 安装WSL2, Ubuntu,docker

    阅读时长:10分钟 本文内容: window上安装ubuntu虚拟机,并在虚拟机中安装docker,通过docker部署数字人模型,通过vscode链接到虚拟机进行开发调试.调试完成后,直接部署在云端即可. WSL2(Windows Subsystem for Linux)是windows系统默认支持的功能,也就是Windows Linux子系统,类似于一台

    2024年02月12日
    浏览(42)
  • wsl2安装docker引擎(Install Docker Engine on Debian)

    官方地址: 在 Debian 上安装 Docker 引擎  https://docs.docker.com/engine/install/debian/ 1.卸载旧版本 在安装 Docker 引擎之前,您必须首先确保卸载任何冲突的软件包。 发行版维护者在他们的存储库。必须先卸载这些软件包,然后才能安装 Docker 引擎的正式版本。 要卸载的非官方软件包是

    2024年02月13日
    浏览(35)
  • Docker Desktop+WSL2并安装到D盘

    1. 安装路径建立软连接 docker desktop默认安装到上图位置,使用如下命令建立软连接,命令如下: 注:以管理员身份打开cmd mklink /j “C:Program FilesDocker” “D:Program FilesDocker” 2、镜像存储路径建立软连接 上图为docker desktop默认的存储镜像路径,命令如下 mklink /j “C:Users139

    2024年02月03日
    浏览(84)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包