I want to add swagger-ui to my docker-compose setup. I'm using postgres and postgrest. My setup looks roughly like:
version: '3'
services:
postgrest:
image: postgrest/postgrest
ports:
- 3000:3000
links:
- postgres
postgres:
image: postgres
ports:
- 5432:5432
volumes:
- "./pgdata:/var/lib/postgresql/data"
swagger:
image: swaggerapi/swagger-ui
expose:
- 8080
ports:
- 8080:8080
links:
- postgrest:postgrest
environment:
API_URL: http://localhost:3000
This shows me the proper API docs UI when I'm testing locally. When I deploy, http://localhost:3000 isn't serving an OpenAPI definition anymore, and this breaks. I can change API_URL to the remote URL, but then it won't update locally if I'm testing some changes, and that generally seems to defeat the point anyway.
Is there a way to point swagger at "the postgrest running in the same docker compose setup"? Something like:
swagger:
...
links:
- postgrest:postgrest
environment:
API_URL: http://postgrest:3000
Sometimes docker compose can do magic like this, e.g. in nginx.
Thanks in advance!
Though you set the
API_URLin yourdocker-compose.yml, the actual request to get the spec file is done by the browser I believe.So, your browser should be able to resolve that URL, not the swagger-ui container itself.
Also, since this is the case, you don't really need a remote Swagger UI hosted at all. Just have a separate local container of swagger-ui running and change the URL to the swagger file when needed, in the UI itself.
UPDATE: Using
SWAGGER_JSONDo note that using
sleepisn't the best approach. You can check out better options like using wait-on / wait-for / wait-for-itPS: I have tried
wait-for&wait-for-it, but sincepostgresthas its endpoint available even though the connection to the DB wasn't successful, it was responding with a503and both these utils just check for the TCP socket availability, so don't work as expected here.wait-onwould work since it checks for2xxon HEAD requests but you would need a container with nodejs, so I stuck withsleepas the simplest example of what has to be done. :)