首页 > 其他分享 >Docker安装-基于wsl

Docker安装-基于wsl

时间:2024-06-13 15:21:54浏览次数:10  
标签:sudo wsl apt https ubuntu docker 安装 Docker

本文参考: Windows 下 wsl2 安装 Linux 原生 Docker

使用 wsl2 而非 Docker desktop 安装 Docker,体验完全接近 Linux 的使用 Docekr 方式。并解决 wsl2 中 Docker 启动不了的问题。

一、安装 wsl2

官方文档:https://learn.microsoft.com/zh-cn/windows/wsl/install-manual

  1. 启用 Linux 的 Windows 子系统
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  1. 启用虚拟机功能
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  1. 下载 Linux 内核更新包
  • amd64(大部分电脑)
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
  • arm64
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_arm64.msi
  1. 将 WSL 2 设置为默认版本
wsl --set-default-version 2
  1. 安装所选的 Linux 分发
    打开微软商店,搜例如 Ubuntu,选择合适的版本下载安装即可。

二、安装 Docker

官方文档:https://docs.docker.com/engine/install/ubuntu/

  1. 卸载旧版本
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
  1. 配置 docker 仓库
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

sudo chmod a+r /etc/apt/keyrings/docker.gpg

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

sudo apt-get update

注:

  • 若出现域名无法解析的错误,可尝试使用阿里云的 DNS 服务器。vim /etc/resolv.conf
nameserver 114.114.114.114
  • 若出现源不可用/速度慢的情况,可尝试使用国内的 apt 镜像源。vim /etc/apt/sources.list
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
  1. 安装 Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  1. 加入 Docker 用户组
sudo usermod -aG docker $USER
exit # 执行后重新再打开终端
  1. 配置防火墙

为什么需要多这一步?因为 wsl2 系统中使用的是经过修改的 nftables,而 Docker 安装程序使用 iptables 进行 NAT。为了解决这个问题,必须使用以下命令将系统切换回使用传统的 iptables,否则无法启动成功。

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
  1. 配置 Docker

/etc/docker/daemon.json

{
  "insecure-registries": ["registry.cn-hangzhou.aliyuncs.com"],
  "registry-mirrors": [
    "https://uh06sk44.mirror.aliyuncs.com",
    "http://registry.cn-hangzhou.aliyuncs.com"
  ],
  "log-driver": "json-file",
  "log-opts": { "max-size": "500m", "max-file": "3" }
}
  1. 启动 Docker,并配置自启
sudo systemctl enable docker
# 也可以用此命令启动了
sudo systemctl start docker
  1. 配置 WSL 开机自启

用户目录下新建 .wslconfig 文件,添加systemd=true, 在此也可以配置 cpu 和内存等。

# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB
# Sets the VM to use two virtual processors
processors=4
# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=0
# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostForwarding=true
# Support systemcel cmd
systemd=true
  1. 检测状态
    进入 Linux 子系统后执行命令,输出systemd 则开启,init 则是关闭
$ ps --no-headers -o comm 1
systemd

标签:sudo,wsl,apt,https,ubuntu,docker,安装,Docker
From: https://www.cnblogs.com/longkang/p/18245959

相关文章

  • jsoncpp的安装及使用
    目录前言安装方法一:apt包管理器方法二:源码编译安装使用编写示例代码写JSON读JSON编译链接输出写JSON读JSON前言本文操作均在ubuntu20.04下进行。安装方法一:apt包管理器①使用包管理器安装这种方法比较简单,直接使用apt包管理器安装jsoncpp:aptinstalllibjsoncpp-dev这条......
  • es-spark +docker
    本项目使用es+spark的技术,并且采用容器进行项目的构建FROMubuntu:20.04RUNapt-getupdate-y&&apt-getinstallvim-y&&apt-getinstallwget-y&&apt-getinstallssh-y&&apt-getinstallopenjdk-8-jdk-y&&apt-getinstallsudo-y&......
  • DockerCompose+Jenkins+Pipeline流水线打包Vue项目(解压安装配置Node)入门
    场景DockerCompose+Jenkins+Pipeline流水线打包SpringBoot项目(解压安装配置JDK、Maven等)入门:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/139631755以上使用流水线配置和打包springboot后台项目,如果要使用流水线和配置node打包vue项目,可按如下步骤。注:博......
  • linux下pyenv的安装和使用
    pyenv可以根据需求使用户在系统里安装和管理多个Python版本:配置当前用户的python的版本配置当前shell的python版本配置某个项目(目录及子目录)的python版本配置多个虚拟环境 一、安装pyenv1、安装pyenvcurl-Lhttps://github.com/pyenv/pyenv-installer/raw/master......
  • anaconda里虚拟环境安装jupyter notebook
    安装jupyternotebook打开anacondaprompt,进入虚拟环境condaactivatePytorch_learning下载安装jupyternotebookcondainstalljupyternotebook或用pip安装pipinstalljupyter运行jupyternotebook进入环境之后,输入命令jupyternotebook运行完成后会自动打开浏......
  • 从零开始写 Docker(十八)---容器网络实现(下):为容器插上”网线“
    本文为从零开始写Docker系列第十八篇,利用linux下的Veth、Bridge、iptables等等相关技术,构建容器网络模型,为容器插上”网线“。完整代码见:https://github.com/lixd/mydocker欢迎Star推荐阅读以下文章对docker基本实现有一个大致认识:核心原理:深入理解Docker核心......
  • 微信小程序分销商城源码系统 致力于中小企业低成本建站 带完整的安装代码包以及搭建教
    系统概述微信小程序凭借其无需下载、即用即走的特点,已成为连接用户与服务的重要桥梁。微信小程序分销商城源码系统正是基于微信小程序平台,集商品展示、在线交易、分销推广、订单管理等功能于一体的综合电商解决方案。该系统设计之初,就充分考虑了中小企业的实际需求,力求在不增......
  • 多校园跑腿小程序源码系统 帮取+代拿+外卖配送 功能超多 带完整的安装代码包+搭建部署
    系统概述多校园跑腿小程序源码系统是一款基于微信小程序平台开发的全功能校园服务应用。该系统采用先进的前后端分离架构,结合云服务技术,确保了系统的高可用性、可扩展性和安全性。系统设计之初就充分考虑到了多校园环境的特殊性,支持多校区独立运营或跨校联合运营模式,满足不同......
  • OpenEuler22.03安装openGauss5.0.2LTS版本
    OpenEuler22.03安装openGauss5.0.2LTS版本文章目录OpenEuler22.03安装openGauss5.0.2LTS版本一、安装规划二、安装准备2.1修改主机名2.2安装依赖2.3同步时间2.4关闭防火墙2.5关闭selinux2.6关闭透明大页2.7重启机器2.8设置网卡MTU2.9设置字符......
  • MySQL MariaDB 安装
    MySQLMariaDB安装指南MariaDB是MySQL数据库管理系统的一个分支,旨在保持与MySQL兼容并添加更多的功能和改进。以下是如何在不同操作系统上安装MariaDB的详细步骤。在Linux上安装MariaDB1.在Ubuntu/Debian上安装MariaDB首先,更新你的软件包索引:sudoaptu......