1. 简介

Docker 是一个用于构建、分发和运行容器化应用的平台,原生支持 Linux 系统。虽然最初为 Linux 设计,但通过 Windows Subsystem for Linux(WSL),我们也可以在 Windows 上运行 Docker。

在 Windows 10/11 和 Windows Server 上安装 Docker 的方式有所不同。 在 Windows Server 上,我们可以使用支持的容器运行时来运行 Windows 容器。此外,如果你打算在云平台(如 AWS、Azure)中运行 Docker,Windows Server 是唯一的选择。

在本教程中,我们将学习如何在 Windows Server 上安装和使用 Docker。前提是你已经有一台安装了 Windows Server(2022、2019 或 2016)的机器。


2. 在 Windows Server 上安装 Docker

Docker 提供了多个支持的容器运行时,包括:

  • Docker CE / Moby(社区版)
  • containerd
  • Mirantis Container Runtime

我们选择使用 Docker CE,它由开源项目 Moby 管理。微软提供了一个 PowerShell 脚本,可以自动安装 Docker 并配置容器运行环境。

打开 PowerShell,执行以下命令安装 Docker CE:

PS C:\Windows\system32> Invoke-WebRequest `
  -UseBasicParsing "https://raw.githubusercontent.com/microsoft/Windows-Containers/Main/helpful_tools/Install-DockerCE/install-docker-ce.ps1" `
  -OutFile install-docker-ce.ps1
.\install-docker-ce.ps1

该脚本首次运行时会:

✅ 自动启用 Windows 功能:Containers
✅ 安装 Docker
✅ 安装 Docker Daemon
✅ 配置 Docker 服务
✅ 连接 Docker Daemon

⚠️ 安装过程中系统会自动重启一次。安装完成后,可以再次运行脚本来验证是否安装成功。

输出示例:

Querying status of Windows feature: Containers...
Feature Containers is already enabled.
Installing Docker...
Installing Docker daemon...
Configuring the docker service...
Successfully connected to Docker Daemon.
Script complete!

接着,使用以下命令验证安装是否成功:

docker version
docker info

输出示例:

Client:
 Version:           27.3.1
 OS/Arch:           windows/amd64

Server: Docker Engine - Community
 Engine:
  Version:          27.3.1
  OS/Arch:          windows/amd64

至此,Docker 已成功安装在 Windows Server 上。


3. 拉取 Windows 容器镜像

接下来我们从微软的容器镜像仓库(Microsoft Container Registry)拉取一个 Windows Server 的基础镜像。Nano Server 是一个轻量级的 Windows 容器基础镜像。

docker pull mcr.microsoft.com/windows/nanoserver:ltsc2022

输出示例:

ltsc2022: Pulling from windows/nanoserver
f66768ec6d65: Pull complete
Digest: sha256:be37c9e8bf388c4e3ef27b4a2592b94adab551e231644ce3c9d4d3dc0a50af41
Status: Downloaded newer image for mcr.microsoft.com/windows/nanoserver:ltsc2022

注意:镜像标签应与当前 Windows Server 版本一致,如:

  • ltsc2022 对应 Windows Server 2022
  • ltsc2019 对应 Windows Server 2019

查看已下载的镜像:

docker images

输出示例:

REPOSITORY                             TAG        IMAGE ID       CREATED       SIZE
mcr.microsoft.com/windows/nanoserver   ltsc2022   890e4cf2e171   3 weeks ago   293MB

4. 运行 Windows 容器

使用刚刚拉取的镜像启动一个 Windows 容器:

docker run -it mcr.microsoft.com/windows/nanoserver:ltsc2022 cmd.exe

进入容器后,你会看到类似如下命令行界面:

Microsoft Windows [Version 10.0.20348.2849]
C:\>

你可以执行一些基础命令,比如 direcho 等。完成后输入 exit 退出容器:

C:\> exit

退出后,使用以下命令查看容器状态:

docker ps -a

输出示例:

CONTAINER ID   IMAGE                                           COMMAND     CREATED         STATUS                     NAMES
79976e7b1972   mcr.microsoft.com/windows/nanoserver:ltsc2022   "cmd.exe"   4 minutes ago   Exited (0) 1 minute ago    hopeful_tharp

5. 构建新的容器镜像

我们可以基于已运行的容器构建一个新的镜像。例如,我们对刚才运行的容器进行提交(commit)操作:

docker commit 79976e7b1972 helloworld

输出示例:

sha256:4c6db347d185012ac56555e9d31dcbe0601e739985c477a1e23b88e349d6b252

查看镜像列表:

docker images

输出示例:

REPOSITORY                             TAG        IMAGE ID       CREATED          SIZE
helloworld                             latest     4c6db347d185   34 seconds ago   294MB
mcr.microsoft.com/windows/nanoserver   ltsc2022   890e4cf2e171   3 weeks ago      293MB

虽然我们没有对基础镜像做任何修改,但在实际开发中,我们通常会对基础镜像进行定制,构建自己的镜像。


6. 在容器中运行命令

现在我们使用刚刚构建的 helloworld 镜像运行一个容器,并在其中执行一条命令:

docker run --rm helloworld cmd.exe /s /c echo 'Hello Docker on Windows Server'

输出:

"Hello Docker on Windows Server"

使用 --rm 参数可以让容器在执行完命令后自动删除,节省资源。


7. 与 Docker Hub 交互

我们可以将镜像推送到 Docker Hub 或从 Hub 拉取镜像。

7.1 登录 Docker Hub

docker login -u username -p password

输出示例:

Login Succeeded

7.2 给镜像打标签

我们需要为镜像打上与 Docker Hub 仓库匹配的标签。例如我们要将镜像推送到 baeldung 用户下的仓库:

docker image tag helloworld baeldung/helloworld

再次查看镜像列表:

docker images

输出示例:

REPOSITORY                             TAG        IMAGE ID       CREATED       SIZE
helloworld                             latest     4c6db347d185   2 hours ago   294MB
baeldung/helloworld                    latest     4c6db347d185   2 hours ago   294MB
mcr.microsoft.com/windows/nanoserver   ltsc2022   890e4cf2e171   3 weeks ago   293MB

7.3 推送镜像到 Docker Hub

确保已登录 Docker Hub,然后执行:

docker push baeldung/helloworld

输出示例:

Using default tag: latest
The push refers to repository [docker.io/baeldung/helloworld]
8290390d708d: Pushed
afe057336777: Pushed
latest: digest: sha256:30880b447432edc2f6abf67140e477334d70b130dbc34a94e486224585897b06 size: 740

你可以在 Docker Hub 上查看该镜像是否已成功上传。

7.4 删除本地镜像

如果不再需要本地镜像,可以删除以释放磁盘空间:

docker rmi baeldung/helloworld

输出示例:

Untagged: baeldung/helloworld:latest
Untagged: baeldung/helloworld@sha256:30880b447432edc2f6abf67140e477334d70b130dbc34a94e486224585897b06

7.5 从 Docker Hub 拉取镜像

最后,我们可以从 Docker Hub 拉取我们刚刚上传的镜像:

docker pull baeldung/helloworld

输出示例:

Using default tag: latest
latest: Pulling from baeldung/helloworld
Digest: sha256:30880b447432edc2f6abf67140e477334d70b130dbc34a94e486224585897b06
Status: Downloaded newer image for baeldung/helloworld:latest

运行一个命令验证是否正常:

docker run --rm baeldung/helloworld cmd.exe /s /c echo "Hello!"

输出:

Hello!

8. 总结

在本教程中,我们学习了如何在 Windows Server 上安装 Docker,并完成了以下操作:

✅ 安装 Docker CE
✅ 拉取 Windows 容器基础镜像
✅ 运行 Windows 容器
✅ 构建自定义镜像
✅ 在容器中运行命令
✅ 与 Docker Hub 交互(登录、打标签、推送、拉取)

这些操作适用于所有支持 Docker 的 Windows Server 版本(2016、2019、2022)。所有命令和脚本可在 GitHub 仓库 中找到。


原始标题:How to Set Up Docker on Windows Server