The issue was that double quotes were used to specify OPEANAI_API_KEY in the .env file. If the key is written in double quotes, e.g.
OPENAI_API_KEY="sk-proj-...................."
it works in the desktop app, but it results in an INVALID_API_KEY error in a docker image.
I removed the double quotes from the .env file and specified the key as follows:
OPENAI_API_KEY=sk-proj-....................
Now it works both in desktop application as well as in docker image.
Thank you!