How to make swagger generate my own api using docker?

I have spring doc in java that generates swagger using the url http://localhost:8080/swagger-ui/index.html with base url /v3/api-docs but when using docker it doesn’t work.

So I decided to use the image version of swagger-ui in docker compose as shown below

 services:
  subscription-manager_app:
    container_name: subscription-manager_app
     image: hizam/subscription-manager_app:1.0.0
     build: .
     ports:
       - '8080:8080'
     environment:
       DATABASE_URL: jdbc:postgresql://db:5432/subscription-manager
       DATABASE_USERNAME: postgres
       DATABASE_PASSWORD: 1
     depends_on:
       - db
       - swagger
   swagger:
     container_name: swagger_con
     image: swaggerapi/swagger-ui:v5.19.0
     ports:
       - '80:8080'
   db:
     container_name: db
     image: postgres
     environment:
       POSTGRES_USER: postgres
       POSTGRES_PASSWORD: 1
       POSTGRES_DB: subscription-manager
     ports:
       - '5333:5432'
     volumes:
       - pgdata:/var/lib/postgresql/data
 volumes:
   pgdata: { }

The swagger swagger-ui:v5.19.0 generates json with the url localhost
with base url https://petstore.swagger.io/v2/swagger.json.

I have red the documentation about swagger-ui image it but I couldn’t find info that explains this case.
If you faced the same issue let me know.


Please, format your post according to the following guide: How to format your forum posts
In short: please, use </> button to share codes, terminal outputs, error messages or anything that can contain special characters which would be interpreted by the MarkDown filter. Use the preview feature to make sure your text is formatted as you would expect it and check your post after you have sent it so you can still fix it.

Example code block:

```
echo "I am a code."
echo "An athletic one, and I wanna run."
```

After fixing your post, please send a new comment so people are notified about the fixed content.


1 Like

commenting here so people will be notified about this post

I read your post twice and have no idea what you want to achieve. What is the expected result?

Furthermore, the docker hub image description contains links to the documentation.
You are probably looking for this one: https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/configuration.md

I also got confused when I red the question. Now I improved it.
I checked this site
swagger-ui/docs/usage/configuration.md at f2c454a65227388889994fd80d53fa28bd2831d9 · swagger-api/swagger-ui · GitHub which shows how to add URL but it didn’t help
The problem is that swagger-ui:v5.19.0 generates default url which https://petstore.swagger.io/v2/swagger.json.

I have to somehow figure out on how to generate my own url and then configure it in swagger-ui by adding url in the environment variable of swagger-ui.

I guess ill keep doing my research on this case.

What does that mean? What is not working? You will have to be more specific.

Another example, when you refer to a documentation and you try a specific config parameter from it, instead of

you could share the config parameter, maybe even how you used it so people who want to help you don’t need to find everything in case they are not familiar with the software.

by “using docker it doesn’t work”
I mean when running the command docker compose the generated swagger url that is comming from spricdoc doesn’t work
http://localhost:8080/swagger-ui/index.html

here is my docker compose

services:
  subscription-manager_app:
    container_name: subscription-manager_app
     image: hizam/subscription-manager_app:1.0.0
     build: .
     ports:
       - '8080:8080'
     environment:
       DATABASE_URL: jdbc:postgresql://db:5432/subscription-manager
       DATABASE_USERNAME: postgres
       DATABASE_PASSWORD: 1
     depends_on:
       - db
       - swagger
   swagger:
     container_name: swagger_con
     image: swaggerapi/swagger-ui:v5.19.0
     ports:
       - '80:8080'
   db:
     container_name: db
     image: postgres
     environment:
       POSTGRES_USER: postgres
       POSTGRES_PASSWORD: 1
       POSTGRES_DB: subscription-manager
     ports:
       - '5333:5432'

swagger-ui has docker a docker variable URL
which uses it to generate the swagger UI for the json file based on that URL.

Example

  swagger:
    container_name: swagger_con
    image: swaggerapi/swagger-ui:v5.19.0
    environment:
      URL: /openapi.json 

Note URL here is set otherwise if not set swagger will generate it’s default url which is https://petstore.swagger.io/v2/swagger.json.

So what did I try to do(solution) is to generate the json file sprindoc in maven like this

<plugin>
				<groupId>org.springdoc</groupId>
				<artifactId>springdoc-openapi-maven-plugin</artifactId>
				<executions>
					<execution>
						<phase>compile</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

and once it’s generated the file gets added inside the target directory of the java application.
After that using volumes that paths to the json and setting the jsonfile in the URL as shown below

    volumes:
      - ./target/openapi.json:/usr/share/nginx/html/openapi.json
    environment:
      URL: /openapi.json

and then typing in the url browser http://localhost/
will create and display the swagger UI for json created by springdoc which is openapi.json.

I’m sorry, but you are still trying to explain “doesn’t work” with “doesn’t work”. Which doesn’t work. It could mean so many things. It shows a white page, it shows a broken HTML, it shows a json but not what you expect, it shows an error message, and so on.

Thw swagger UI can read a local file or a URL. If you have your own URL, you can use that even without Docker, just with the swagger UI, but unless you set the required response headers, it the swagger UI will show an error message mentioning the missing header. HEre is a simple python code that can run a server that returns the header and this is what I used for my test

But it needs a json, not an html, that’s why it was confusing to me:

Later yoi set the URL to /openapi.json without making it available in the container, but that will just mean that the swagger UI will try to access it from the browser. Just like any other file is loaded in the browser when referred to in a link. So it would expect it to work on the same port like: https://localhost/openapi.json.

If this is what happened, you had to get an error message that says “not found”. And it could be seen from the web browser’s developer console.

But you don’t have to mount your json into the webserver’s doc root if you only want to load one file. The documentation that @meyay linked, doesn’t mention it on the same page, but there is an “installation” page in the same fodler which does. If you start from the image description on Docker Hub, that also links the already rendered “installation” page:

https://swagger.io/docs/open-source-tools/swagger-ui/usage/installation/#docker

and it shows examples like this

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

If you start a shell in the swagger ui container and run “env”, you can see that the default value is /app/swagger.json, so you either set a custom path in that variable, or mount your json to the default path.

Then when you open the swagger UI, you will see ./swagger.json in input field in the browser. That ./ probably means it is not a URL, but a local path in /app, although the json is also set as a symbolic link in the document root

/usr/share/nginx/html # ls -la /usr/share/nginx/html/swagger.json
lrwxrwxrwx    1 root     root            17 Jun 16 19:09 /usr/share/nginx/html/swagger.json -> /app/swagger.json

Does it mean you eventually solved your issue? If it does, your solution seems to be fine as well. If you want to follow the recommended way by the documentation, you can try what I wrote above.

1 Like