swagger-ui
swagger-ui copied to clipboard
How to serve local file with docker
I am following the instructions to set up a docker container with a local API docs file, with an api.yaml file in the local repo in the folder $HOME/my-app, and launching the container as follows:
docker run -p 80:8080 -e SWAGGER_JSON=/docs/api.yaml -v $HOME/my-app:/docs swaggerapi/swagger-ui
However, it returns in a 404 ("GET /docs/api.yaml HTTP/1.1" 404 126).
It's not clear what I'm doing wrong and any help would be appreciated.
Thanks for reading.
I used this and it works :
version: "3.3" services: swagger-ui: image: swaggerapi/swagger-ui container_name: "swagger-ui" ports: - "80:8080" volumes: - ./docs/api.yaml:/api.yaml environment: SWAGGER_JSON: /api.yaml
To be honest, the doc is unclear about SWAGGER_JSON environment variable.
thanks @AirDur here is docker run command
docker run -p 80:8080 -e SWAGGER_JSON=/api.yaml -v /<absolute_path>/api.yaml:/api.yaml swaggerapi/swagger-ui
This page (installation.md) memtioned the SWAGGER_JSON but not in this page (configuration.md), really confusing.
Using the above still results in showing the default Swagger Petstore example :(
If setting
volumes:
- ./docs/api.yaml:/api.yaml
environment:
SWAGGER_JSON: /api.yaml
what URL will the file be served on? I assumed just localhost, which that doesn't work, but localhost/api.yaml doesn't work either.
I tried @AirDur solution and it works, thanks! @mandreyel I can see the file under localhost/api.yaml. Be sure that the path ./docs/api.yaml exists, it's relative to the docker-compose.yaml file location.
Problem is the swagger UI always shows up with the Petstore URL in the text box. Is there a way to change it? It's annoying to change it manually every time.
@mandreyel I had this same problem and was using the "tag latest" just like you, so I decided to test the most stable version, the v3.24.3 and the problem was solved
Using the above still results in showing the default Swagger Petstore example :(
As for me I keep forgetting that my browser can have a cache. [SHIFT]+[F5] solves this for Google Chrome.
It looks like Swagger will silently fall back to the petstore whenever the defined swagger.json can not be found. Make sure your paths and mounts are correct
Holy Moly. This was a real blood boiler due to me thinking it would be a quick solution using their docker image...
After getting the path setup correctly as mentioned by previous commentators, I still could not view my custom.yaml.
After reading this article, I was finally able to it view.
Apparently it looks in the /usr/share/nginx/html/ for the custom swagger doc.
The full CLI command looked like this:
docker run -p 80:8080 -e SWAGGER_JSON=/custom.yaml -v {AbsolutePath}/custom.yaml:/usr/share/nginx/html/custom.yaml swaggerapi/swagger-ui

On the shoulders of giants...
Holy Moly. This was a real blood boiler due to me thinking it would be a quick solution using their docker image...
After getting the path setup correctly as mentioned by previous commentators, I still could not view my
custom.yaml.After reading this article, I was finally able to it view.
Apparently it looks in the
/usr/share/nginx/html/for the custom swagger doc.The full CLI command looked like this:
docker run -p 80:8080 -e SWAGGER_JSON=/custom.yaml -v {AbsolutePath}/custom.yaml:/usr/share/nginx/html/custom.yaml swaggerapi/swagger-ui
On the shoulders of giants...
Thanks a lot, this finally works!
On the shoulders of giants...
What a view from up here, thank you @escarborEleven!
I found this worked slightly better as it loaded directly to my custom.yaml instead of starting with the Pet Store:
docker run -p 80:8080 -d --rm -e URL=/custom.yaml -v {AbsolutePath}/custom.yaml:/usr/share/nginx/html/custom.yaml swaggerapi/swagger-ui