Docker-compose, conditional statements? (e.g. add volume only if condition)

后端 未结 6 1680
野趣味
野趣味 2021-02-12 02:25

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

6条回答
  •  情深已故
    2021-02-12 03:19

    Using conditional stmts in docker-compose is somewhat possible. Checkout variable substitution. The documentation is available for just the simplest of if-else. And since I have not tried with complex expressions involving strings, I cannot be sure. But following are the points you might want to keep in mind when trying out conditional variables:

    • Environment variables in docker-compose file (with only a key) are resolved to their values on the machine Compose is running on. So when using ${CHEESE} in docker-compose, one should have CHEESE="cheddar" set in .env file or exported manually in host machine.
    • Alternatively .env file can be set with env_file option. Variables in this file are exported in the docker container before the variables under environment option. That means variables in environment will override variables in env_file .

提交回复
热议问题