📜  如何在 ubuntu 中检查状态 docker - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:52:36.563000             🧑  作者: Mango

如何在 Ubuntu 中检查 Docker 的状态

在 Ubuntu 上使用 Docker 很常见,但是有时候需要检查 Docker 的状态。本文将介绍如何在 Ubuntu 中检查 Docker 的状态。

检查 Docker 是否安装

首先,需要检查 Docker 是否已经安装。可以使用下面的命令来检查 Docker 是否安装:

docker --version

如果输出了 Docker 的版本号,说明已经安装。如果没有输出,需要先安装 Docker。

检查 Docker 服务是否运行

如果 Docker 已经安装,需要检查 Docker 服务是否正在运行。可以使用下面的命令来检查 Docker 服务的状态:

systemctl status docker.service

如果 Docker 服务正在运行,应该会输出类似下面这样的信息:

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-09-30 14:47:36 CEST; 52s ago
     Docs: https://docs.docker.com
 Main PID: 21216 (dockerd)
    Tasks: 8
   Memory: 16.4M
      CPU: 465ms
   CGroup: /system.slice/docker.service
           ├─21216 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
           └─21229 containerd --config /var/run/containerd/containerd.toml --log-level info

Sep 30 14:47:35 ubuntu systemd[1]: Starting Docker Application Container Engine...
Sep 30 14:47:36 ubuntu dockerd[21216]: time="2020-09-30T14:47:36.958712920+02:00" level=warning msg="Your kernel does not support cgroup rt period"
Sep 30 14:47:36 ubuntu dockerd[21216]: time="2020-09-30T14:47:36.958797670+02:00" level=warning msg="Your kernel does not support cgroup rt runtime"
Sep 30 14:47:36 ubuntu dockerd[21216]: time="2020-09-30T14:47:36.962246366+02:00" level=info msg="Loading containers: start."
Sep 30 14:47:36 ubuntu dockerd[21216]: time="2020-09-30T14:47:36.978095708+02:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. …
Sep 30 14:47:36 ubuntu dockerd[21216]: time="2020-09-30T14:47:36.997781936+02:00" level=info msg="Loading containers: done."
Sep 30 14:47:36 ubuntu dockerd[21216]: time="2020-09-30T14:47:37.035815091+02:00" level=info msg="Docker daemon" commit=9bea5a8 kernel=5.4.0-48-generic os=ubu…
Sep 30 14:47:37 ubuntu systemd[1]: Started Docker Application Container Engine.

如果 Docker 服务没有运行,应该会输出类似下面这样的信息:

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Wed 2020-09-30 14:45:42 CEST; 3min 5s ago
     Docs: https://docs.docker.com
  Process: 20685 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=1/FAILURE)
 Main PID: 20685 (code=exited, status=1/FAILURE)

Sep 30 14:45:42 ubuntu systemd[1]: docker.service: Service RestartSec=100ms expired, scheduling restart.
Sep 30 14:45:42 ubuntu systemd[1]: docker.service: Scheduled restart job, restart counter is at 3.
Sep 30 14:45:42 ubuntu systemd[1]: Stopped Docker Application Container Engine.
Sep 30 14:45:42 ubuntu systemd[1]: docker.service: Start request repeated too quickly.
Sep 30 14:45:42 ubuntu systemd[1]: docker.service: Failed with result 'exit-code'.
Sep 30 14:45:42 ubuntu systemd[1]: Failed to start Docker Application Container Engine.
启动和停止 Docker 服务

如果 Docker 服务没有运行,需要启动 Docker 服务。可以使用下面的命令来启动 Docker 服务:

sudo systemctl start docker.service

如果 Docker 服务正在运行,但是需要停止它,可以使用下面的命令来停止 Docker 服务:

sudo systemctl stop docker.service
总结

本文介绍了如何在 Ubuntu 中检查 Docker 的状态。通过检查 Docker 是否安装和 Docker 服务是否运行,可以轻松地检查 Docker 的状态。如果需要,也可以启动和停止 Docker 服务。