如何从容器内部检查另一个容器是否正在端口上运行

发布于 2025-01-21 02:25:29 字数 310 浏览 1 评论 0 原文

我同时运行2个容器(通过docker-compose连接在设置链接上& amp; depters_on)。 依赖性还不够,因此我希望在一个容器的入口手机上运行的脚本检查其他容器是否已经在某些端口上运行。

我尝试了:

#!bin/bash
until nc -z w10 <container_name> 3306
do
echo waiting for db to be ready...
sleep 2
done
echo code is ready

但这是不起作用的..

有人有一个主意吗?

I am running 2 containers at the same time (connected via docker-compose on setting links && depends_on).
The depends on is not enough, so I want the script that run on entryphone of one of the container to check if the other container is running already on some port.

I tried:

#!bin/bash
until nc -z w10 <container_name> 3306
do
echo waiting for db to be ready...
sleep 2
done
echo code is ready

But this is not working..

Anyone got an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2025-01-28 02:25:29

我建议使用依赖性方法。但是,您可以使用此命令的一些高级设置。 “ https://docs.docker.com/compose/startup-order/”的文档。

请阅读 wait-for-it.sh 脚本以准确实现所需的目标。从文档中提取:

version: "2"
services:
  web:
    build: .
    ports:
      - "80:8000"
    depends_on:
      - "db"
    command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
  db:
    image: postgres

I would suggest to use the depends_on approach. However, you can use some of the advanced setting of this command. Please, read the documentation of Control startup and shutdown order in Compose

You can use the wait-for-it.sh script to exactly achieve what you need. Extracted from the documentation:

version: "2"
services:
  web:
    build: .
    ports:
      - "80:8000"
    depends_on:
      - "db"
    command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
  db:
    image: postgres
眼睛会笑 2025-01-28 02:25:29

由于您已经在使用 docker-compose 来编排您的服务,更好的方法是使用 depends_on 长语法条件:service_healthy。因此,docker-compose 不会在一个容器中手动等待另一个容器变得可用,而只会在后者变得健康之后才启动前者,即可用的。

如果依赖的容器没有指定的 HEALTHCHECK 在其图像中,您已经可以在 docker-compose.yml 中使用 healthcheck 属性

使用包含的 mariadb 数据库示例>healthcheck.sh 脚本

services:
  app:
    image: myapp/image
    depends_on:
      db:
        condition: service_healthy

  db:
    image: mariadb
    environment:
      - MARIADB_ROOT_PASSWORD=password
    healthcheck:
      test: "healthcheck.sh --connect"

使用这个docker-compose up会首先启动db服务,并等待db服务变得healthy,即准备好接受连接,并且只然后将启动app服务,该服务可以立即连接到db

Since you are already using docker-compose to orchestrate your services a better way would be to use condition: service_healthy of the depends_on long syntax. So instead of manually waiting in one container for the other to become available docker-compose will start the former only after the latter became healthy, i.e. available.

If the depended-on container does not have a specified HEALTHCHECK in its image already you can manually define it in the docker-compose.yml with the healthcheck attribute.

Example with a mariadb database using the included healthcheck.sh script:

services:
  app:
    image: myapp/image
    depends_on:
      db:
        condition: service_healthy

  db:
    image: mariadb
    environment:
      - MARIADB_ROOT_PASSWORD=password
    healthcheck:
      test: "healthcheck.sh --connect"

With this docker-compose up will first start the db service and wait until the db service becomes healthy, i.e. is ready to accept connections, and only then will start the app service which can immediately connect to the db.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文