Docker Compose 停止、关闭、启动和启动之间的区别
Docker Compose 有多个类似的命令,但功能完全不同。 一些示例是命令 docker compose stop 和 docker compose down 以及命令 docker compose start 和 docker compose up。
本文将相应地讨论它们的区别。
docker compose stop 和 docker compose down 的区别
使用 docker compose stop
运行 docker compose stop
命令的主要功能是停止正在运行的容器的服务。 该命令将停止我们的容器,但不一定会删除它们。
例如,如果我们不指定容器名称或 ID,下面的命令将停止所有容器:
$ docker compose stop
输出:
Going to stop file, web, test
Stopping file ... done
Stopping web ... done
Stopping test ... done
我们可以通过指定容器名称或 ID 来指定停止哪个容器:
$ docker compose stop file
输出:
Going to stop file
Stopping file ... done
此外,我们可以在运行 docker compose stop
时指定关闭超时。 Docker 通常会在停止我们的容器之前等待 10 秒。
例如,如果我们知道我们的容器在停止时可能需要更多时间,我们可能希望增加此超时值,如下所示。
$ docker compose stop file -t 60
使用 docker compose down
另一方面,docker compose down 命令在这个过程中给了我们一个额外的步骤。 该命令将停止并删除容器和其中运行的服务; 删除包括容器和网络。
docker compose down 在顺序执行时与 docker stop <container name or id>
和 docker <prune or rm>
的行为类似。 我们可以说 docker compose down 命令是两个组合命令的快捷方式。
因此,不要运行下面的两个命令:
$ docker compose stop file && docker compose rm -f
我们可以改用 docker compose down。
$ docker compose down file
我们可以通过将 -v 或 --volumes 标志附加到我们的 docker compose down 命令来提升它。 该命令将停止并删除容器、它们的网络以及执行时附加的卷。
通过添加上述标志,我们将三个命令合并为一个命令。
$ docker compose down file -v
我们不仅可以对容器使用 docker compose down 命令,还可以对镜像使用。 例如,我们可以运行 docker compose down --rmi <all or local>
命令来移除镜像。
该命令在原生 Docker 中使用时类似于 docker rmi 命令。
docker compose start 和 docker compose up 的区别
使用docker compose start
与我们上一节一样,命令 docker compose start 和 docker compose up 听起来很相似,但在功能上却有所不同。 例如,docker compose start 命令重新启动之前停止的特定容器。
$ docker compose start file
此外,上述命令仅适用于已创建的容器。
那么,如何才能直接启动一个未创建的容器呢? 我们可以在下面使用以下命令。
使用 docker compose up
docker compose up 命令基于 docker-compose.yml 文件启动一个新容器。 它类似于在彼此顺序运行时运行 docker create 和 docker start。
由于我们使用的是 YAML 文件,因此我们可以仅使用一个命令来创建和启动多个容器。
docker compose up
输出:
Creating file
Creating web
Creating test
当我们使用 docker compose up
时,如果源 YAML 文件有任何变化,基于该 YAML 文件的容器将被停止并重新创建。
为避免这种情况,我们可以在 docker compose up
期间使用 --no-recreate 选项,如下所示。 总而言之,如果容器已经存在,该命令将不会重新创建它。
$ docker-compose up -d --no-recreate
此外,就像 docker compose stop
一样,我们可以指定超时值。
$ docker-compose up -d -t 30
我们也可以说这是在 vanilla Docker 环境中运行和启动新容器时命令 docker run 的主要对应项。
相关文章
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