0

I have an entry point like so:

ENTRYPOINT [ "python" ,"/usr/local/bin/script.py" ]

I want to be able to add multiple arguments to script.py if a particular container has "MY_ENV_VAR" set to TRUE.

So for instance if:

MY_ENV_VAR = true
MY_ENV_VAR2 = true
MY_ENV_VAR3 = false (or anything other than true)

I want to run /usr/local/bin/script.py --my-env-var --my-env-var2

I can't find a good working example of how to accomplish this

Paolo
  • 21,270
  • 6
  • 38
  • 69
Sputnikk23
  • 63
  • 9
  • You can't configure the ENTRYPOINT to change according to whether an env var is present or not, but you can make it so that the `script.py` prompts you with the right options by using argparse properly – Paolo Mar 16 '22 at 16:39
  • Another approach would be to-- use the ENV inside your script.py and do conditional stmt based on ENV. – Gupta Mar 16 '22 at 16:54
  • [Setting options from environment variables when using argparse](https://stackoverflow.com/questions/10551117/setting-options-from-environment-variables-when-using-argparse) describes a generic approach for doing this (setting `default=os.environ['MY_ENV_VAR']` for each option in your `argparse` configuration); this will translate just fine to Docker without changing anything. Does that setup work for you? – David Maze Mar 16 '22 at 16:58
  • Did my solution work for you? – Paolo Mar 31 '22 at 13:53

1 Answers1

0

You will not be able to conditionally do this within the Dockerfile, but you can achieve the desired behavior by configuring the Python script script.py as follows:

import argparse
import os

def main():
    parser = argparse.ArgumentParser("Arguments")

    if os.environ.get("MY_ENV_VAR") is not None:
        parser.add_argument("--my-env-var", required=True, action="store_true")
    
    if os.environ.get("MY_ENV_VAR2") is not None:
        parser.add_argument("--my-env-var2", required=True, action="store_true")
    
    parser.parse_args()

if __name__ == "__main__":
    main()

The logic is that if MY_ENV_VAR or MY_ENV_VAR2, then a required argparse argument is set.

Paolo
  • 21,270
  • 6
  • 38
  • 69