Docker编写的条件语句?(例如,仅在有条件的情况下添加音量) [英] Docker-compose, conditional statements? (e.g. add volume only if condition)

查看:66
本文介绍了Docker编写的条件语句?(例如,仅在有条件的情况下添加音量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将卷添加到我的服务中,但前提是最终用户为其提供了一个文件夹.否则,不应安装任何卷,因为已准备好的映像在默认文件夹中具有有效数据.

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 已定义.对于单个或几个条件,这都很好,但是如果有很多条件,这会令人讨厌.

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编写的条件语句?(例如,仅在有条件的情况下添加音量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆