dockerdockerfiledocker-cmd

docker CMD array syntax for double quotes inside double quotes for gunicorn


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?


Solution

  • Correct usage is:

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

    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()"]