我正在尝试将我的 swagger UI 的 URL 从
0.0.0.0
更改为 localhost
(见下图)
我正在使用带有 swagger 图像的 docker compose。
version: '3'
services:
api-postgrest:
image: another-url.com/postgrest:1.0.0
ports:
- 3000:3000
depends_on:
- database-postgres
environment:
- PGRST_DB_URI=postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@database-postgres:5432/${DATABASE_NAME}
- PGRST_DB_SCHEMA=${PGRST_DB_SCHEMA} # public
- PGRST_DB_ANON_ROLE=${PGRST_DB_ANON_ROLE} # web_anon
database-postgres:
image: postgres:16
ports:
- '5432:5432'
environment:
- POSTGRES_USER=${DATABASE_USER} # postgres
- POSTGRES_PASSWORD=${DATABASE_PASSWORD} # postgres
- POSTGRES_DB=${DATABASE_NAME} # postgres
volumes:
- pg_data:/var/lib/postgresql/data
swagger:
image: swaggerapi/swagger-ui
ports:
- "8080:8080"
expose:
- "8080"
environment:
API_URL: http://localhost:3000/
volumes:
pg_data:
经过一些研究,我当前的解决方案是添加一个 json 配置文件,如下所示,带有
SWAGGER_JSON
。
swagger:
image: swaggerapi/swagger-ui
ports:
- "8080:8080"
expose:
- "8080"
environment:
API_URL: http://localhost:3000/
SWAGGER_JSON: swagger.json
json 文件看起来像这样
{
"swagger": "2.0",
"info": {
"title": "The YOLO Json",
"description": "I'm losing my stuff",
"version": "0.0.1"
},
"host": "127.0.0.1:3000",
"schemes": [
"https"
]
}
然而它不起作用。我的问题如下,
SWAGGER_JSON
的路径是否正确以及其内容中的json是否正确?
如果指定
SWAGGER_JSON
,则无需指定 API_URL
,因为这似乎是从中加载 swagger.json 的 URL。另外,从问题中的 swagger
服务定义来看,swagger.json
文件似乎没有安装到容器中。由于问题中的swagger.json
没有任何操作,所以我使用了petstore swagger json代替,将URL更改为问题中的127.0.0.1:3000
。有了这些东西,我最终得到了如下所示的 docker-compose.yml
文件:
services:
swagger:
image: swaggerapi/swagger-ui:v5.11.6
ports:
- "8080:8080"
environment:
SWAGGER_JSON: /specs/swagger.json
volumes:
- ./swagger.json:/specs/swagger.json
使用上面的撰写文件,出站请求确实显示了预期的主机:
curl -X 'GET' \
'https://127.0.0.1:3000/v2/pet/1234' \
-H 'accept: application/json'
您可以在 github 上找到所有更改,包括使用的 swagger.json。
您需要使用其中之一:-
我更喜欢 swagger JSON,如下所示
swagger:
image: swaggerapi/swagger-ui
ports:
- "8080:8080"
environment:
SWAGGER_JSON: /specs/swagger.json
volumes:
- ./swagger.json:/specs/swagger.json
您的
swagger.json
中的条目看起来不错