Django rest framework swagger missing port when run in docker

Viewed 555

I am using rest framework swagger for the first time in my Django application. When I run it locally from PyCharm it works just fine. My app runs on port 1337 and when I Try Out my restful API endpoint and click Execute, the curl command works and the URL includes the port.

The issue is when I run my Django app in a Docker. In this case, the URL in the curl command does not include the port. Do I have to add any swagger specific configuration to my docker compose file? I have not changed my Dockerfile nor my docker-compose file at all.

Running in docker

What do I need to do to get this to work properly?

2 Answers

It seems like you have set the url parameter for the get_schema_view(...)--(DRF Doc) function. The url is used to set the canonical base URL for the schema.

If you didn't set the value, DRF will use the "requested host" as the default URL.

Ref

url = self.url
if not url and request is not None:
    url = request.build_absolute_uri()

SO, you can set the url to any value or you can exclude the parameter to get the default behavior.

from rest_framework.schemas import get_schema_view

urlpatterns = [
    path('openapi', get_schema_view(
        title="Your Project",
        description="API for all things …",
        version="1.0.0"
    ), name='openapi-schema'),
]

Please check the file urls.py which is the place you are setting URL for SWAGGER. Don't set url in get_schema_view function. Also, when you are running the app by using docker, the URL in the curl command does not include the port -> That's right! I think that your file is using multiple settings for swagger by checking like this: if getattr(settings, "SETTINGS_ENV", None) in ["local", "dev", "draft", "production"]:

Related