检查 Docker 容器是否正在运行
在 Docker 中,有多种方法可以检查容器的状态。 当显示这些信息时,我们还可以检查 Docker 容器是否正在运行。
本文将讨论检查 Docker 容器是否正在运行的命令。
检查 Docker 容器是否正在运行
在 Docker 中,我们有多个命令来检查所有已创建容器的状态。 在下一节中,我们将列出这些命令的各种示例。
docker ps
在 Docker 中,我们有一个名为 docker ps
的命令,它列出了所有容器。 如果您接受过 Docker 培训,docker ps 可能已经成为基本 Docker 生命周期的一部分。
docker ps
命令有多个选项; 但是,本节将讨论两个最重要的问题。
第一个是显示所有容器的命令 --all 或 -a 选项。 默认情况下,运行不带 -a 选项的命令只会显示正在运行的容器。
示例代码:
docker ps -a
docker ps
输出:
此外,我们可以使用一个附加选项来仅显示正在运行的容器。 例如,我们可以使用 --filter 选项,只查找状态等于运行的容器。
示例代码:
docker ps -a --filter status=running
输出结果:
上面的命令类似于 docker container ls -a
命令,它列出了容器级别的所有容器及其状态。
Bash 和 docker inspect
我们可以用来显示正在运行的容器的另一种方法是编程。 例如,我们可以使用 docker inspect 列出容器的属性。
由于上述命令有一个 JSON 输出,我们可以将它与 bash 一起使用。
示例代码:
if [ $(docker inspect -f '{{.State.Running}}' "zen_dirac") = "true" ]; then echo Running; else echo NotRunning; fi
下面的代码片段搜索名为 zen_dirac 的特定容器及其 State.Running 属性。 如果该属性等于 True,则该命令将显示最终输出 Running,否则显示 NotRunning。
如果我们管理数百个正在运行的容器并且只需要单个容器的信息,此代码片段会很有帮助。 在这种情况下,我们的 zen_dirac 容器正在运行,因此它应该在我们的命令行中产生 Running 的输出。
输出:
Running
docker info
此外,如果我们需要容器的高级摘要报告,我们可以使用 docker info
命令。 docker info
命令显示系统范围的信息,包括几个正在运行的容器。
如果我们不需要输入容器的名称但想检查容器是否正在运行,此命令很有用。
示例代码:
docker info
输出:
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
发布时间:2025/03/26 浏览次数:107 分类:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
发布时间:2025/03/26 浏览次数:97 分类:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
发布时间:2025/03/26 浏览次数:124 分类:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
发布时间:2025/03/26 浏览次数:202 分类:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
发布时间:2025/03/26 浏览次数:87 分类:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
发布时间:2025/03/26 浏览次数:140 分类:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application