Home > Back-end >  docker CMD array syntax for double quotes inside double quotes for gunicorn
docker CMD array syntax for double quotes inside double quotes for gunicorn

Time:11-17

I am using docker and gunicorn for my python application. I am starting gunicorn as below:

CMD ["gunicorn", "--workers 2", "--threads 2", "--bind 0.0.0.0:8000", "--preload", ""main:create_app()""]

But getting error as wrong syntax, because of last element i.e. (""main:create_app()""). As given in gunicorn documentation, I am trying to use below form:

def create_app():
    app = FrameworkApp()
    ...
    return app
$ gunicorn --workers=2 'test:create_app()'

I Also tried single quotes as "'main:create_app()'", But this also failed.

What I am missing?

CodePudding user response:

Correct usage is:

RUN ["gunicorn", "--workers", "2", "--threads", "2", "--bind", "0.0.0.0:8000", "--preload", "main:create_app()"]
  • Everywhere you have an unquoted space between two shell arguments in the original/working shell command, you need to split into separate elements in your JSON list. That means --workers and 2 are two different strings, instead of being one string --workers 2; the same goes for everywhere else you have an argument and an argument-option paired.
  • Syntactic shell quotes, like the quotes around 'main:create_app()', are instructions to the shell that symbols like () should not be treated as shell syntax. Because there is no shell here, those instructions are unnecessary. Just use "main:create_app()" as a simple JSON string, with only JSON quotes; no literal quotes are necessary or appropriate.

If you have questions in the future about how to convert a simple command to a JSON string, you can ask jq to do it for you:

$ jq -cn --args '$ARGS.positional' -- gunicorn --workers 2 --threads 2 --bind 0.0.0.0:8000 --preload 'main:create_app()'
["gunicorn","--workers","2","--threads","2","--bind","0.0.0.0:8000","--preload","main:create_app()"]
  • Related