插件窝 干货文章 windows怎么玩docker

windows怎么玩docker

Docker 安装 Windows powershell 181    来源:    2025-04-30

在 Windows 上使用 Docker 主要有两种方式,根据你的 Windows 版本和需求选择适合的方案:

方案一:Windows 10/11 专业版/企业版(推荐)

使用 Docker Desktop for Windows(支持原生 Hyper-V 虚拟化)

  1. 系统要求

    • Windows 10/11 64位(专业版/企业版/教育版)
    • 启用 Hyper-V 和 WSL 2 功能
    • 至少 4GB 内存(建议 8GB+)
  2. 安装步骤

    # 1. 启用 WSL 2 和 Hyper-V(管理员权限运行)
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    wsl --set-default-version 2
    
    # 2. 下载并安装 Docker Desktop
    # 官网下载:https://www.docker.com/products/docker-desktop/
    
    # 3. 安装后启动 Docker Desktop,在设置中选择:
    #    - 使用 WSL 2 引擎(推荐)
    #    - 在 WSL 中集成(可选但推荐)
    
  3. 验证安装

    docker --version
    docker run hello-world
    

方案二:Windows 家庭版或旧版本

使用 Docker Toolbox(基于 VirtualBox)

  1. 安装步骤

    # 1. 安装 VirtualBox:https://www.virtualbox.org/
    # 2. 下载 Docker Toolbox:https://github.com/docker/toolbox/releases
    # 3. 运行安装程序(会自动配置环境)
    
  2. 使用注意

    • 通过 Docker Quickstart Terminal 操作
    • 容器IP为 VirtualBox 虚拟机的IP(默认192.168.99.100)

常用操作示例

# 拉取镜像
docker pull nginx

# 运行容器
docker run -d -p 80:80 --name mynginx nginx

# 查看容器
docker ps -a

# 进入容器
docker exec -it mynginx bash

# 构建镜像(需有 Dockerfile)
docker build -t myimage .

性能优化建议

  1. 对于 WSL 2:
    • %UserProfile%\.wslconfig 中添加: [wsl2] memory=6GB # 限制内存使用 processors=4 # CPU核心数
  2. 镜像加速:
    • 在 Docker Desktop 设置中添加国内镜像源: https://registry.docker-cn.com http://hub-mirror.c.163.com

常见问题解决

  1. WSL 2 安装失败

    • 确保已安装 WSL 2 内核更新包
    • 运行 wsl --update 升级
  2. 端口冲突

    netstat -ano | findstr :80  # 查找占用端口的进程
    
  3. 磁盘空间不足

    docker system prune  # 清理无用资源
    

对于生产环境,建议使用 Linux 主机或云服务器运行 Docker,Windows 更适合开发和测试用途。