How to run swagger-ui with local code changes AND my own swagger.json?
I found this topic because I wanted to see a visual representation of my local swagger file, but could not seem to get swagger-ui (running in docker) to display anything other than the petstore.
Ultimately, my issue was with understanding the -e SWAGGER_JSON and -v flags, so I wanted to explain them here.
-v <path1>:<path2>
This option says "Mount the path <path1> from my local file system within the swagger-ui docker container on path <path2>"
-e SWAGGER_JSON=<filepath>
This option says "By default, show the swagger for the file at <filepath> using the docker container's file system." The important part here, is that this filepath should take into account how you set <path2> above
Putting it all together, I ended up with the following:
docker run -p 8085:8080 -e SWAGGER_JSON=/foo/swagger.json -v `pwd`:/foo swaggerapi/swagger-ui
This says in english: "Run my swagger-ui instance on port 8085. Mount my current working directory as '/foo' in the docker container. By default, show the swagger file at '/foo/swagger.json'."
The important thing to note is that I have a file called swagger.json in my current working directory. This command mounts my current working directory as /foo in the docker container. Then, swagger UI can pick up my swagger.json as /foo/swagger.json.
Make sure you are volume mounting the correct local directory.
Locally, I had my swagger config in $PWD/src/app/swagger/swagger.yaml
. Running the following worked fine:
docker run -p 80:8080 -e SWAGGER_JSON=/tmp/swagger.yaml -v `pwd`/src/app/swagger:/tmp swaggerapi/swagger-ui
Simply refreshing the Swagger-UI page or clicking the "Explore" button in the header triggered a refresh of the data from my YAML file.
You can also specify BASE_URL
excerpt from swagger-installation
docker run -p 80:8080 -e BASE_URL=/swagger -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui
Here's how I ended up solving this, it also allows you to have multiple YML files:
docker run -p 80:8080 \
-e URLS_PRIMARY_NAME=FIRST \
-e URLS="[ \
{ url: 'docs/first.yml', name: 'FIRST' } \
, { url: 'docs/second.yml', name: 'SECOND' } \
]" \
-v `pwd`:/usr/share/nginx/html/docs/ \
swaggerapi/swagger-ui