简介
在本实验中,你将学习如何使用 docker container pause
命令暂停运行中的容器。我们将首先使用 hello-world
镜像创建并运行一个简单的容器。
之后,你将练习暂停运行中的容器并验证其暂停状态。最后,你将学习如何解除暂停以恢复容器执行。本实验通过实践操作,介绍如何使用 Docker 命令管理容器状态。
在本实验中,你将学习如何使用 docker container pause
命令暂停运行中的容器。我们将首先使用 hello-world
镜像创建并运行一个简单的容器。
之后,你将练习暂停运行中的容器并验证其暂停状态。最后,你将学习如何解除暂停以恢复容器执行。本实验通过实践操作,介绍如何使用 Docker 命令管理容器状态。
在这一步骤中,我们将学习如何创建并运行一个简单的 Docker 容器。容器是一种标准的软件单元,它将代码及其所有依赖项打包,使得应用程序能够在不同的计算环境中快速可靠地运行。
首先,我们需要拉取将要使用的 Docker 镜像。我们将使用 hello-world
镜像,这是一个非常小的镜像,专门用于演示 Docker 的工作原理。
docker pull hello-world
该命令会从 Docker Hub 下载 hello-world
镜像到你的本地机器。你将看到显示下载进度和完成的输出信息。
获取镜像后,我们就可以基于它运行容器了。
docker run hello-world
该命令会从 hello-world
镜像创建一个新容器并运行它。当你运行这个容器时,它会向终端打印一条消息,说明你的 Docker 安装运行正常。打印完消息后,容器就会退出。
你应该会看到类似以下的输出:
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.
(Assuming it was not already locally available)
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 容器。
在前一步骤中,我们运行的容器会立即退出。为了演示暂停和恢复操作,我们需要一个持续运行的容器。我们将使用一个简单的 ubuntu
容器并使其在后台保持运行状态。
首先,让我们拉取 ubuntu
镜像。
docker pull ubuntu
该命令会从 Docker Hub 下载 ubuntu
镜像。
现在,我们将在分离模式 (-d
) 下运行一个 ubuntu
容器,使其在后台运行。同时使用 tail -f /dev/null
命令让容器持续运行。
docker run -d ubuntu tail -f /dev/null
该命令会输出容器 ID。请复制这个 ID,我们稍后需要用它来暂停容器。
<container_id>
现在我们已经有了一个运行中的容器,可以使用 docker pause
命令加上容器 ID 来暂停它。
docker pause <container_id>
请将 <container_id>
替换为你实际运行的 Ubuntu 容器 ID。
该命令会暂停指定容器内的所有进程。容器仍然存在,但其进程将被挂起。
在前一步骤中,我们暂停了一个运行中的容器。现在,让我们验证该容器确实处于暂停状态。
我们可以使用 docker ps
命令来列出运行中的容器并检查它们的状态。
docker ps
该命令会显示当前运行中的容器列表。如果你的 Ubuntu 容器已被暂停,它仍会出现在列表中,但其状态会显示为暂停。请查找你在前一步骤中暂停的容器 ID。
输出结果中应该会显示你的容器状态为类似 Up ... (Paused)
的格式。
或者,你也可以使用 docker container inspect
命令来获取特定容器的详细信息,包括其状态。请将 <container_id>
替换为你实际暂停的 Ubuntu 容器 ID。
docker container inspect <container_id>
该命令会输出一个包含容器所有配置和状态信息的 JSON 对象。你需要在输出中查找 "State"
字段。
若要专门检查容器是否被暂停,你可以使用 grep
等工具来过滤 docker container inspect
的输出。
docker container inspect <container_id> | grep Paused
如果容器已被暂停,该命令会输出类似 "Paused": true,
的行。这确认了容器确实处于暂停状态。
在前一步骤中,我们验证了 Ubuntu 容器处于暂停状态。现在,让我们恢复容器并继续其执行。
我们使用 docker unpause
命令加上容器 ID 来恢复暂停的容器。请将 <container_id>
替换为你实际暂停的 Ubuntu 容器 ID。
docker unpause <container_id>
该命令会恢复指定容器内所有被暂停的进程。
恢复容器后,你可以再次使用 docker ps
命令验证其状态。
docker ps
此时,你的 Ubuntu 容器状态应显示为 Up ...
而不再带有 (Paused)
标识,这确认容器已恢复正常运行。
最后,为清理环境,你可以停止并移除该容器。首先停止容器:
docker stop <container_id>
然后移除容器:
docker rm <container_id>
这些命令将停止运行中的容器并将其从系统中移除。
在本实验中,我们学习了如何使用 docker container pause
命令来暂停容器。我们首先使用 hello-world
镜像创建并运行了一个简单容器,演示了拉取镜像和运行容器的基本流程。
在创建并运行容器后,我们探索了如何使用 docker container pause
命令暂停运行中的容器,并验证了其暂停状态。最后,我们学习了如何使用 docker container unpause
命令恢复容器的执行。