首页 > 解决方案 > 如何在 Docker Compose 中运行多个 bash 命令(使用管道)

问题描述

我在 Docker Compose 中有一个冗长的 bash 命令:

  command: bash -c 'wait-for-it emul:4000 && (firebase-prime ... | grep -v -E "Do not use with production credentials") && npx vite --port 3000'

如果firebase-prime没有找到,或者它会失败,整个命令仍然会通过并且 Docker Compose 会继续运行。如何让它失败,以及如何让 Docker Compose 关闭所有依赖服务?

标签: bashdocker-compose

解决方案


实验:

$ bash -c 'false | cat' && echo a
a
$ bash -c 'set -o pipefail; false | cat' && echo a
$ bash -o pipefail -c 'false | cat' && echo a

添加-o pipefail似乎是最简单的语法。

  command: bash -o pipefail -c 'wait-for-it emul:4000 && (firebase-prime ... | grep ...)`

推荐阅读