openai_api_key in docker image gives error as "openai.AuthenticationError: Error code: 401 - 'invalid_api_key&a

admin2025-04-16  5

I have a simple test code to test the OPENAI_API_KEY locally and in docker image.

The application runs fine locally but for docker image run, it gives error as:

"openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: "sk-proj****"

Here is the testcode.py

from openai import Client
from dotenv import load_dotenv
import os

load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

print(f"Key is : {OPENAI_API_KEY}")

openai_client = Client()

response = openai_client.chatpletions.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

Here is the Dockerfile

FROM python:3.11-slim

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 8000

CMD ["python", "testcode.py"]

The .env file in my project

OPENAI_API_KEY="<my_key>"

This is the run command I use to run the docker image:

docker run --env-file .env -it  -p 8000:8000 testapp_1:v1

If I run this app locally:

It prints the key value and also gives the response from the llm object.

But if I run the docker image, it prints the api key correctly, but throws an Authentication Error.

On OpenAI site I checked the access permission for the api key. It is set to "all"

What can be the issue when I use the API key from my docker image?

Thanks in adavance!

I have a simple test code to test the OPENAI_API_KEY locally and in docker image.

The application runs fine locally but for docker image run, it gives error as:

"openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: "sk-proj****"

Here is the testcode.py

from openai import Client
from dotenv import load_dotenv
import os

load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

print(f"Key is : {OPENAI_API_KEY}")

openai_client = Client()

response = openai_client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

Here is the Dockerfile

FROM python:3.11-slim

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 8000

CMD ["python", "testcode.py"]

The .env file in my project

OPENAI_API_KEY="<my_key>"

This is the run command I use to run the docker image:

docker run --env-file .env -it  -p 8000:8000 testapp_1:v1

If I run this app locally:

It prints the key value and also gives the response from the llm object.

But if I run the docker image, it prints the api key correctly, but throws an Authentication Error.

On OpenAI site I checked the access permission for the api key. It is set to "all"

What can be the issue when I use the API key from my docker image?

Thanks in adavance!

Share Improve this question edited Feb 2 at 14:05 Preeti v asked Feb 2 at 12:42 Preeti vPreeti v 431 silver badge6 bronze badges 2
  • Is it possible you already have a different value set on your local environment for OPENAI_API_KEY? If that's the case, that would mean testcode.py is using that one when executed locally rather than the one defined in .env, which would explain the differences you're seeing depending on the environment. I suggest checking that locally with echo $OPENAI_API_KEY and comparing it with the one in your .env file. – lmtaq Commented Feb 2 at 21:01
  • I have not set it in the system environment variables, so echo %OPENAI_API_KEY% does not print it. So the keys are same, locally as well as in docker, it is taking it from the .env file. Thanks for the comment! – Preeti v Commented Feb 4 at 6:19
Add a comment  | 

1 Answer 1

Reset to default 3

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!

转载请注明原文地址:http://anycun.com/QandA/1744800765a87800.html