网站搜索

Docker Compose Up 与 Start 以及 Down 与 Stop [区别]


对于 docker-compose 的初学者来说,像 docker-compose up、start、down 和 stop 这样的术语可能会让人不知所措。本文通过详细示例解释了其中的差异。

如果您是 Docker Compose 新手,并且通过学习各种教程来学习它,您可能会遇到 docker-compose up、docker-compose up -d、docker-compose stop、docker-compose down 甚至 docker 等术语-撰写停止。

这些术语足以让 docker 初学者感到困惑,因为许多 docker-compose 命令的行为方式似乎非常相似。

事实上,一开始要立即区分 docker-compose up 和 docker-compose start 之间的区别可能特别困难。

通过 Docker Compose 启动容器和运行 up 命令不一样吗?不完全是。

让我详细向您解释一下。

Docker Compose up、up -d、stop、start、down 和 down -v 之间的区别

这些命令的作用:

Docker Compose up 命令部署 Web 应用服务并从 docker 映像创建全新的容器,同时设置网络、卷以及 Docker Compose 文件中指定的每个配置。当您指定 -d 时,这意味着您告诉它以分离模式运行,以便它通过为您提供终端控制权在后台运行(稍后将通过下面的一些示例进行讨论)。

Docker Compose stop 命令停止与 Docker Compose 配置关联的所有服务。它不会删除任何容器或关联的内部卷或网络。

Docker Compose 启动命令将启动基于相同 Docker Compose 文件的停止配置上指定的任何已停止服务。

Docker Compose down 命令停止与 Docker Compose 配置关联的所有服务。与停止不同,它还会删除与服务关联的任何容器和内部网络。但不是内部指定的卷。为此,您还需要在 down 命令后额外指定 -v 标志。

这听起来类似于 Docker run vs start 命令,对吧?

理论已经足够了,现在让我们看一些实践示例。

通过实际示例了解差异

如果您想遵循示例,请确保您已经安装了 Docker 和 Docker Compose 安装程序。

假设您正在使用在 Linux 服务器上运行的基于 Docker Compose 的 Ghost 博客设置。

在我们所有的自托管教程中,每当我们在服务器上部署配置时,我大多倾向于使用 -d 标志。但如果你不指定怎么办?

avimanyu@localhost:~/ghost$ docker-compose up
Pulling ghost (ghost:4.20.3)...
4.20.3: Pulling from library/ghost
b380bbd43752: Pull complete
8d36a6ce056a: Pull complete
f75fe68b8e22: Pull complete
44f6d143e12f: Pull complete
0ebe8063dedd: Pull complete
f984e0e37c5a: Pull complete
ce2320facea8: Pull complete
898c3dbc1716: Pull complete
45c37559f24a: Pull complete
Digest: sha256:b332684117bfa05329298712ad0ffcfc4a83ce6314332e073978f46be3c05e81
Status: Downloaded newer image for ghost:4.20.3
Creating ghost_ghost_1 ... done
Attaching to ghost_ghost_1
ghost_1  | [2021-10-26 07:02:05] INFO Ghost is running in production...
ghost_1  | [2021-10-26 07:02:05] INFO Your site is now available on https://ghost.domain.com/
ghost_1  | [2021-10-26 07:02:05] INFO Ctrl+C to shut down
ghost_1  | [2021-10-26 07:02:05] INFO Ghost server started in 0.369s
ghost_1  | [2021-10-26 07:02:06] WARN Database state requires initialisation.
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: posts
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: posts_meta
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: users
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: oauth
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: posts_authors
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: roles
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: roles_users
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: permissions
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: permissions_users
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: permissions_roles
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: settings
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: tags
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: posts_tags
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: invites
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: brute
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: sessions
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: integrations
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: webhooks
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: api_keys
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: mobiledoc_revisions
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: products
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: offers
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: benefits
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: products_benefits
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_products
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_payment_events
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_login_events
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_email_change_events
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_status_events
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_product_events
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_paid_subscription_events
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: labels
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_labels
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_stripe_customers
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_stripe_customers_subscriptions
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: offer_redemptions
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: members_subscribe_events
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: stripe_products
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: stripe_prices
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: actions
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: emails
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: email_batches
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: email_recipients
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: tokens
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: snippets
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: temp_member_analytic_events
ghost_1  | [2021-10-26 07:02:06] INFO Creating table: custom_theme_settings
ghost_1  | [2021-10-26 07:02:06] INFO Model: Product
ghost_1  | [2021-10-26 07:02:06] INFO Model: Tag
ghost_1  | [2021-10-26 07:02:06] INFO Model: Role
ghost_1  | [2021-10-26 07:02:06] INFO Model: Permission
ghost_1  | [2021-10-26 07:02:07] INFO Model: User
ghost_1  | [2021-10-26 07:02:07] INFO Model: Post
ghost_1  | [2021-10-26 07:02:08] INFO Model: Integration
ghost_1  | [2021-10-26 07:02:08] INFO Relation: Role to Permission
ghost_1  | [2021-10-26 07:02:08] INFO Relation: Post to Tag
ghost_1  | [2021-10-26 07:02:08] INFO Relation: User to Role
ghost_1  | [2021-10-26 07:02:08] INFO Database is in a ready state.
ghost_1  | [2021-10-26 07:02:08] INFO Ghost database ready in 3.309s
ghost_1  | [2021-10-26 07:02:09] INFO Ghost booted in 4.457s
ghost_1  | [2021-10-26 07:02:09] INFO Adding offloaded job to the queue
ghost_1  | [2021-10-26 07:02:09] INFO Scheduling job update-check at 49 27 22 * * *. Next run on: Tue Oct 26 2021 22:27:49 GMT+0000 (Coordinated Universal Time)
ghost_1  | [2021-10-26 07:02:51] INFO "GET /favicon.ico" 200 7ms
ghost_1  | [2021-10-26 07:02:51] INFO "GET /" 200 605ms
ghost_1  | [2021-10-26 07:02:51] INFO "GET /assets/built/screen.css?v=dde6c321bb" 200 5ms
ghost_1  | [2021-10-26 07:02:51] INFO "GET /assets/built/casper.js?v=dde6c321bb" 200 3ms
ghost_1  | [2021-10-26 07:02:52] INFO "GET /members/api/member/" 204 1ms
ghost_1  | [2021-10-26 07:02:52] INFO "GET /members/api/site/" 200 14ms
ghost_1  | [2021-10-26 07:02:52] INFO "GET /favicon.ico" 200 2ms

所以你看?如果没有 -d 选项,您确实会启动配置,但它会以详细模式发生,而不会返回到终端提示符。有点帮助,不是吗?当你打开浏览器并访问幽灵博客时,你会发现它很快就可以访问。但是如果您使用 Ctrl+Z 退出控制台怎么办?它将保持进程在后台运行,您可以使用docker ps命令进行检查:

avimanyu@localhost:~/ghost$ docker ps
CONTAINER ID   IMAGE                                    COMMAND                  CREATED         STATUS          PORTS                                                                      NAMES
563a45d049cf   ghost:4.20.3                             "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes    2368/tcp                                                                   ghost_ghost_1

但是如果您使用 Ctrl+C 来代替,容器会发生什么情况呢?该进程立即被终止。

avimanyu@localhost:~/ghost$ docker ps
CONTAINER ID   IMAGE                                    COMMAND                  CREATED         STATUS          PORTS                                                                      NAMES

当您使用 -d 选项时,它的作用与 Ctrl+Z 非常相似:它从控制台分离并继续在后台运行容器,并打印新的容器名称(在本例中为 >ghost_ghost_1)。

现在,我们不使用 docker-compose down ,而是使用 docker-compose stop :

avimanyu@localhost:~/ghost$ docker-compose stop
avimanyu@localhost:~/ghost$

现在让我们检查一下正在运行的容器。预计应该没有:

avimanyu@localhost:~/ghost$ docker-compose ps
    Name                   Command               State    Ports
---------------------------------------------------------------
ghost_ghost_1   docker-entrypoint.sh node  ...   Exit 0 

请注意,我没有使用 docker ps。相反,我运行了 docker-compose ps,因为我想向您展示另一种验证 Exit 0 状态的方法。这意味着容器已退出/停止。

让我们使用 docker ps -a 重新检查一下。 -a 标志还将查找已停止的容器:

avimanyu@localhost:~/ghost$ docker ps -a
CONTAINER ID   IMAGE                                    COMMAND                  CREATED         STATUS                     PORTS                                                                      NAMES
44d09e778a91   ghost:4.20.3                             "docker-entrypoint.s…"   8 minutes ago   Exited (0) 7 minutes ago                                                                              ghost_ghost_1

那么什么是 docker-compose start 呢?

仅当您未使用 docker-compose down 删除任何容器时,Docker Compose Start 才有意义(我在本教程命令行中尚未完成此操作)。所以基本上,这里的区别在于它启动已停止且未删除的容器。

因此,首先让我们使用 start 而不是 up 来看看会发生什么:

avimanyu@localhost:~/ghost$ docker-compose start
Starting ghost ... done
avimanyu@localhost:~/ghost$ 

现在发生的情况是停止的容器再次启动:

avimanyu@localhost:~/ghost$ docker-compose ps
    Name                   Command               State    Ports  
-----------------------------------------------------------------
ghost_ghost_1   docker-entrypoint.sh node  ...   Up      2368/tcp

就这样吧。状态现在从 Exit 0 返回到 Up。您还可以使用命令的 docker 版本重新检查:

avimanyu@localhost:~/ghost$ docker ps
CONTAINER ID   IMAGE                                    COMMAND                  CREATED          STATUS              PORTS                                                                      NAMES
44d09e778a91   ghost:4.20.3                             "docker-entrypoint.s…"   22 minutes ago   Up About a minute   2368/tcp                                                                   ghost_ghost_1

现在让我们再次运行 stop 命令。

avimanyu@localhost:~/ghost$ docker-compose stop
Stopping ghost_ghost_1 ... done

您现在知道容器的状态是什么。您可以使用 up 或 start 命令使其再次运行。请注意,您也可以在此状态下运行 down 而无需再次启动服务:

avimanyu@localhost:~/ghost$ docker-compose down
Removing ghost_ghost_1 ... done
Network net is external, skipping

现在容器已被移除。如果 Docker Compose 文件中指定了内部网络,它们也会被删除。由于 net 是外部的,因此将跳过删除。如果您另外指定了 -v 标志,它也会被删除!

avimanyu@localhost:~/ghost$ docker-compose down -v
Stopping ghost_ghost_1 ... done
Removing ghost_ghost_1 ... done
Network net is external, skipping
Volume ghost is external, skipping

始终对您的数据保持双重谨慎!

额外提示:请注意,由于我使用的是外部卷(之前通过“dockervolumecreatevolume-name”创建),因此使用“-v”不会删除它。它仅适用于根据 Docker Compose 规范创建的卷。但相反,“docker volume prune”会删除外部卷,即使它正在被容器使用!

此外,您在此阶段无法使用 start。它仅适用于 stopped 配置:

avimanyu@localhost:~/ghost$ docker-compose start
Starting ghost ... failed
ERROR: No containers to start

在这种情况下,您必须再次使用 docker-compose up 或 docker-compose up -d 。

avimanyu@localhost:~/ghost$ docker-compose up -d
Creating ghost_ghost_1 ... done
avimanyu@localhost:~/ghost$

总结

我希望这篇文章能让您更好地理解 up 与 up -d 与 start 以及 stop 与用于 Docker Compose 的 down 和 down -v。

从现在开始,这份内容广泛的解释指南应该会让您的日常 Docker 管理变得更加容易,并且不会那么令人难以承受。根据不同的场景,尤其是在生产系统中,您选择哪个特定命令来解决情况,当然,您的实现方式也会有所不同。

如果您有任何想法、疑问或建议要分享,请在下面发表评论。

相关文章: