Windows系统安装docker教程

使用WSL 2安装docker

一.安装wsl2

1.在任务管理器中,检查系统虚拟化是否开启

2.以管理员身份运行cmd.

3.输入:wsl --install

4.启用wsl功能

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

5.重启,再次管理员打开命令窗口,输入下面的命令

wsl --install -d Ubuntu

6.输入用户名和密码

二.安装docker(主要)

1. 清理之前的操作

首先,清理之前可能的错误安装和配置:

a. 移除旧版本 Docker

如果系统中已经安装了旧版本的 Docker 或其依赖,先移除:

sudo apt remove -y docker docker-engine docker.io containerd runc

b. 删除旧的 Docker 源

清除可能存在的无效源文件:

sudo rm -f /etc/apt/sources.list.d/docker.list sudo apt update

c. 检查是否有遗留的 Docker 文件

确保系统中没有遗留文件或配置:

sudo rm -rf /var/lib/docker sudo rm -rf /var/lib/containerd


2. 添加 Docker 的官方源

a. 安装依赖

确保系统具备添加源所需的软件:

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

b. 添加 GPG 密钥

添加 Docker 官方的 GPG 密钥:

sudo mkdir -p /etc/apt/keyrings curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

c. 添加源地址

将阿里云镜像作为源,专为 Ubuntu 20.04(代号 focal):

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


3. 安装 Docker

a. 更新包索引

更新系统软件包列表:

sudo apt update

b. 安装 Docker

安装最新版本的 Docker 和相关组件:

sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin


4. 验证安装

a. 检查 Docker 版本

运行以下命令确认 Docker 安装成功:

docker --version

b. 测试 Docker 是否正常运行

运行测试镜像 hello-world

sudo docker run hello-world

输出应包含以下内容,表示 Docker 安装成功:

Hello from Docker! This message shows that your installation appears to be working correctly.


5. 配置非 root 用户使用 Docker

为避免每次运行 Docker 都需要使用 sudo

  1. 将当前用户添加到 docker 用户组:

    sudo usermod -aG docker $USER

  2. 退出当前终端并重新登录以生效。

  3. 测试运行: docker run hello-world


6. 启动和设置 Docker 开机启动

确保 Docker 服务已启动并设置为开机自动启动:

sudo systemctl start docker sudo systemctl enable docker


7. 常见问题排查

a. 如果安装包未找到
  • 确认 apt-cache policy docker-ce 的输出中有可用版本。
  • 如果仍未找到,检查 /etc/apt/sources.list.d/docker.list 是否正确配置了 focal 作为版本代号。
b. 如果无法访问镜像
  • 确保可以访问阿里云镜像,或者尝试其他国内源,如:
    • 清华大学镜像

      https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu

相关推荐
蓁蓁啊3 小时前
GIT使用SSH 多账户配置
运维·git·ssh
程序猿小三5 小时前
Linux下基于关键词文件搜索
linux·运维·服务器
虚拟指尖6 小时前
Ubuntu编译安装COLMAP【实测编译成功】
linux·运维·ubuntu
椎4957 小时前
苍穹外卖前端nginx错误之一解决
运维·前端·nginx
刘某的Cloud7 小时前
parted磁盘管理
linux·运维·系统·parted
极验7 小时前
iPhone17实体卡槽消失?eSIM 普及下的安全挑战与应对
大数据·运维·安全
爱倒腾的老唐7 小时前
24、Linux 路由管理
linux·运维·网络
yannan201903137 小时前
Docker容器
运维·docker·容器
_清浅7 小时前
计算机网络【第六章-应用层】
运维·服务器·计算机网络
正在努力的小河8 小时前
Linux 自带的 LED 灯驱动实验
linux·运维·服务器