docker-compose 在构建之前等待其他服务

huangapple go评论109阅读模式
英文:

docker-compose wait on other service before build

问题

以下是已翻译的部分:

有几种方法可以修复 docker-compose 中容器启动顺序的问题,例如:

然而,如果docker-compose文件中的某个服务包含了 build 指令,似乎docker-compose会首先尝试构建图像(基本上忽略了 depends_on,或将 depends_on 解释为启动依赖关系,而不是构建依赖关系)。

是否可能通过 build 指令来指定需要在开始构建过程之前启动另一个服务?

最小示例:

version: "3.5"
services:
  web:
    build: # 这将在 postgres 启动之前运行
      context: .
      dockerfile: Dockerfile.setup # 需要 postgres 启动
    depends_on:
      - postgres
    ...
  postgres:
    image: postgres:10
    ...

尽管通常的建议是程序应该以一种优雅地处理服务不可用(至少在一段时间内)的方式编写,是否有任何方法允许构建只在其他容器启动后才开始?

一些其他相关问题:

更新/解决方案:通过将所需的(数据库)设置推迟到引导容器的 CMD 指令中解决了潜在问题:

FROM undertest-base:latest

...

CMD ./wait && ./bootstrap.sh

其中 wait 在postgres启动后等待,bootstrap.sh 包含设置postgres数据库的固定内容,因此在执行该脚本后整个系统可以完全进行测试。

有了这个解决方案,使用数据库设置建立临时测试环境变得简单,只需运行 docker-compose up 即可。

英文:

There are a few approaches to fix container startup order in docker-compose, e.g.

However, if one of the services in a docker-compose file includes a build directive, it seems docker-compose will try to build the image first (ignoring depends_on basically - or interpreting depends_on as start dependency, not build dependency).

Is it possible for a build directive to specify that it needs another service to be up, before starting the build process?

Minimal Example:

version: "3.5"
services:
  web:
    build: # this will run before postgres is up
      context: .
      dockerfile: Dockerfile.setup # needs postgres to be up
    depends_on:
      - postgres
    ...
  postgres:
    image: postgres:10
    ...

Notwithstanding the general advice that programs should be written in a way that handles the unavailability of services (at least for some time) gracefully, are there any ways to allow builds to start only when other containers are up?


Some other related questions:


Update/Solution: Solved the underlying problem by pushing all the (database) setup required to the CMD directive of a bootstrap container:

FROM undertest-base:latest

...

CMD ./wait && ./bootstrap.sh

where wait waits for postgres and bootstrap.sh contains the code for setting up the postgres database with fixtures so the over system becomes fully testable after that script.

With that, setting up an ephemeral test environment with database setup becomes a simple docker-compose up again.

答案1

得分: 1

在Compose中没有这个选项,而且它实际上不会起作用。

图像构建的输出是一个自包含的不可变图像。您可以执行诸如docker push之类的操作,将图像推送到注册表,Docker的图层缓存将避免重新构建已构建的图像。因此,在这种假设的设置中,如果您可以在Dockerfile中访问数据库,但运行以下命令:

docker-compose build
docker-compose down -v
docker-compose up -d --build

down -v步骤将删除数据库使用的存储。而up --build选项将导致重新构建图像,但构建序列将跳过所有步骤,并产生与最初相同的图像,您对数据库所做的任何更改都不会发生。

在更机械的层面上,构建序列不使用Compose提供的网络,因此您也无法连接到数据库容器。

有时候,在build:中有一个依赖关系可能很有用,特别是如果您尝试构建其他Compose设置中的图像共享的基本图像。但既稳定的Compose文件v3 build:块,也不太受支持的Compose规范 build:都不支持图像构建依赖于其他任何内容的概念。

英文:

There is no option for this in Compose, and also it won't really work.

The output of an image build is a self-contained immutable image. You can do things like docker push an image to a registry, and Docker's layer cache will avoid rebuilding an image that it's already built. So in this hypothetical setup, if you could access the database in a Dockerfile, but you ran

docker-compose build
docker-compose down -v
docker-compose up -d --build

the down -v step will remove the storage the database uses. While the up --build option will cause the image to be rebuilt, the build sequence will skip all of the steps and produce the same image as originally, and whatever changes you might have made to the database won't have happened.

At a more mechanical layer, the build sequence doesn't use the Compose-provided network, so you also wouldn't be able to connect to the database container.

There are occasional use cases where a dependency in build: would be handy, in particular if you're trying to build a base image that other images in your Compose setup share. But neither the stable Compose file v3 build: block nor the less-widely-supported Compose specification build: supports any notion of an image build depending on anything else.

huangapple
  • 本文由 发表于 2023年2月8日 21:51:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定