21

So, I wanted to run the local Swagger UI with respect to Local Json. And for that I am following the instructions available here:

Here is the command that is being shared in that documentation:

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

Here I understand the -p option but this -e and -v is confusing.

So let's assume I have kept a JSON file at my desktop in Mac whose path is :

/Users/abc/Desktop/lp.json so with respect to this file the command will gets changed to:

docker run -p 8081:8080 -e SWAGGER_JSON=/Users/abc/Desktop/lp.json -v /bar:/foo swaggerapi/swagger-ui

But what about the -v part of the command. What is the value that I need to put with respect to -v option, although the basic command i.e :

docker run -p 8081:8080 swaggerapi/swagger-ui

Runs and SwaggerUI is available at http:localhost:8081 but with default Json and not with my Json, i.e with http://petstore.swagger.io/v2/swagger.json

So my query is what should I make a change to the command of docker that will run the image of the SwaggerUI with my local JSON?

Please help. Thanks in advance. & Happy coding :)

Ankur Verma
  • 5,793
  • 12
  • 57
  • 93

6 Answers6

28

In the docker command -v means to mount a volume and -e means to add environment variables, so what you want is probbably this:

docker run -p 8081:8080 -e SWAGGER_JSON=/mnt/lp.json -v /Users/abc/Desktop:/mnt swaggerapi/swagger-ui
David
  • 1,636
  • 19
  • 27
  • not working for me, this is my command `docker run -p 9000:8080 -e SWAGGER_JSON=/Users/abc/Downloads/swagger/swagger.json -v /Users/abc/Desktop:/Users/faizal/Downloads/swagger swaggerapi/swagger-ui` – Fai Zal Dong Feb 12 '19 at 02:10
  • 1
    You're doing it wrong, this is what you need (if your file is located at /Users/faizal/Downloads/swagger/swagger.json ): `docker run -p 9000:8080 -e SWAGGER_JSON=/mnt/swagger.json -v /Users/faizal/Downloads/swagger:/mnt swaggerapi/swagger-ui` @FaiZalDong – David Mar 01 '19 at 12:19
  • hi @psrcek thank you for replying, I already followed your steps but the swagger still use default json `https://petstore.swagger.io/v2/swagger.json`. is it because of version of swagger? – Fai Zal Dong Mar 05 '19 at 01:33
  • @psrcek Thanks for explanation! I have a swagger.json on my desktop so running this command worked `docker run -p 9000:8080 -e SWAGGER_JSON=/mnt/swagger.json -v ~/Desktop:/mnt swaggerapi/swagger-ui` - http://localhost:9000/ is loading correct file. – R Sun Sep 24 '19 at 10:29
  • this is ok for me @David but how can I get the Json or YAML file or add these line to get that file in docker-compose.yml configuration for swagger-ui – Humayun Naseer Dec 20 '20 at 18:48
5

For me following command helped

docker run -p 8081:8080 -e SWAGGER_JSON=/tmp/swagger.json -v `pwd`/docs:/tmp swaggerapi/swagger-ui:v3.21.0

'pwd'/docs => Contains my swagger.json file.

swaggerapi/swagger-ui:v3.21.0 => This version is supported SWAGGER_JSON.Its tested.

Siby Augustine
  • 141
  • 2
  • 6
4

The way it worked for me on Windows is this:

docker run -p 80:8080 -e SWAGGER_JSON=/mnt/swagger-v1.json -v C:\Users\jimen\Downloads:/mnt swaggerapi/swagger-ui

where C:\Users\jimen\Downloads is the folder where the local swagger file is located (swagger-v1.json).

enter image description here

M.Octavio
  • 1,780
  • 2
  • 25
  • 39
1

I actually wanted swagger-editor and not swaggerapi/swagger-ui:

docker pull swaggerapi/swagger-editor
docker run -p 7999:8080 swaggerapi/swagger-editor 

Then you get a full File menu:

Before I had a pull the

rustyMagnet
  • 3,479
  • 1
  • 31
  • 41
1

Instead of specifying SWAGGER_JSON you can do the following to immediatelly get your API spec openend in Swagger UI:

docker run --name my-swagger-ui -p 80:8080 -e URL=api/openapi.yaml -v ${PWD}://usr/share/nginx/html/api swaggerapi/swagger-ui:v3.51.1

This is also tested with the latest swagger-ui image which was published one day ago.

NOTE: I ran this command from Windows, thus specifying the environment variable ${PWD} maybe a bit different in Linux.


What is happening behind the curtain

What's happening here is, that you mount a volume (-v ${PWD}://usr/share/nginx/html/api) in the nginx html folder which contains your swagger spec (in this case openapi.yaml). With -e URL=api/openapi.yaml the default URL will be overwritten and instead your swagger spec will be used.

Chris
  • 2,071
  • 4
  • 14
  • 22
0

As of 22-Aug-2022, if you are running on macbook with Apple Silicon, the current docker image in docker hub does not run on Apple Silicon (only supported for amd64 platform and not arm64 platform). A simple workaround for that is to just build the image locally by cloning swagger editor and then running the following commands from the clone repository:

$ docker buildx build --platform linux/arm64 --tag swagger-editor:latest . --load
$ docker run -p 9090:8080 -e SWAGGER_FILE=/tmp/openapi.yaml -v <local_directory_containing_openapi_spec_files>:/tmp swagger-editor:latest
nikxor
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Neeraj Aug 26 '22 at 06:43