Docker-compose,条件语句?(例如,仅在条件下添加音量) [英] Docker-compose, conditional statements? (e.g. add volume only if condition)
问题描述
我想向我的服务添加一个卷,但前提是最终用户为其提供了一个文件夹.否则,不应挂载任何卷,因为已准备好的映像在默认文件夹中具有有效数据.
I want to add a volume to my service, but only if the final user gave a folder for it. Otherwise, no volume should be mounted, for the already-prepared image has valid data in a default folder.
也就是说,我想做一些类似(伪代码)的事情:
That is, I want to do something like (pseudocode):
services:
my_awesome_service:
volumes:
if ${VARIABLE} => ${VARIABLE}:/app/folder
这样的条件语句是否可以在 docker-compose 文件中定义?
我认为使这成为可能的唯一方法是首先定义一个没有卷挂载的基本 docker-compose 文件,并且仅当 $VARIABLE<时才调用第二个 docker-compose 文件/code> 已定义.这对于单个或几个条件没有问题,但如果有多个条件,则会变得令人讨厌.
The only way I see to make this possible is to first define a base docker-compose file, which does not have the volume mount, and the call on a second docker-compose file only if the $VARIABLE
is defined. This is fine for a single or few conditions, but gets nasty if there are many.
有什么解决办法吗?
推荐答案
穷人的解决方案:
volumes:
${VARIABLE:-/dev/null}:/app/folder
或者:
volumes:
${VARIABLE:-/dev/null}:${VARIABLE:-/tmp}/app/folder
这篇关于Docker-compose,条件语句?(例如,仅在条件下添加音量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!