Swagger-ui: How to run swagger-ui with local code changes AND my own swagger.json?

Created on 16 Sep 2017  路  7Comments  路  Source: swagger-api/swagger-ui

The Readme specifies that Swagger-UI can be run with your own file like this

docker run -p 80:8080 -e SWAGGER_JSON=/foo/swagger.json -v /bar:/foo swaggerapi/swagger-ui

which works I if I translate it to

docker build . -t swagger-ui-local && \
  docker run -p 80:8080 -e SWAGGER_JSON=/foo/my-file.json -v $PWD:/foo swagger-ui-local

This, however, ignores my local changes.

I can run my local changes with

npm run dev

but I can't figure out how to get this dev server to run anything else than the Petstore example.

Can anyone help me combine the two, so I can run swagger-ui with local code changes AND my own swagger.json?

lock-bot

Most helpful comment

@HaraldNordgren What I do is open http://localhost:3200?url=myfile.json

All 7 comments

I think I figured it out for npm run dev:

Place my-file.json in the dev-helpers folder. Then it's available from the search bar in on http://localhost:3200/.

To load it automatically when opening the server, alter dev-helpers/index.html by changing

url: "http://petstore.swagger.io/v2/swagger.json"

to

url: "my-file.json"

@HaraldNordgren What I do is open http://localhost:3200?url=myfile.json

None of things which I've seen so far worked for me so I've made this bash script for starting docker swagger-ui container with my host file linked (and not copied) for live updates of what I'm working on. Maybe it will be helpful for other people looking for option to preview their file edited from host (eg. some text editor you like).

LOCAL_PORT=8080
LOCAL_SWAGGER_FILE=`pwd`/api.yml;

docker run \
    -p ${LOCAL_PORT}:8080 \
    -e SWAGGER_JSON=/usr/share/nginx/html/swagger.json \
    -v ${LOCAL_SWAGGER_FILE}:/usr/share/nginx/html/swagger.json \
    swaggerapi/swagger-ui \
    /bin/sh -c 'sed -i "s|cp \$SWAGGER_JSON \$NGINX_ROOT|\# cp \$SWAGGER_JSON \$NGINX_ROOT|g" /usr/share/nginx/docker-run.sh && . /usr/share/nginx/docker-run.sh'

What it does is essentially commenting out copy statement from docker-run.sh which makes the docker container fail to start because of copying the file specified in SWAGGER_JSON.

@HaraldNordgren I recently struggled with the same problem and found a simple trick to solve this situation.

Sharing it here - https://medium.com/@requestly_ext/check-swagger-api-specs-without-hosting-swagger-ui-934861c004de

Maybe it will help you and others. But this is only helpful if you want to view specs on your machine only and not share the IP of your machine with others. But its worth looking at as it minimizes some of the development cycles.

@HaraldNordgren Thanks!

Make sure you are volume mounting correctly! Check out my answer in this Stack Overflow post https://stackoverflow.com/a/50264343/3838892

Locking due to inactivity.

This is done to avoid resurrecting old issues and bumping long threads with new, possibly unrelated content.

If you think you're experiencing something similar to what you've found here: please open a new issue, follow the template, and reference this issue in your report.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fehguy picture fehguy  路  3Comments

liuya05 picture liuya05  路  3Comments

Deraen picture Deraen  路  4Comments

MartinMuzatko picture MartinMuzatko  路  4Comments

LaysDragon picture LaysDragon  路  3Comments