skip to Main Content

I’m trying to change the URL of my swagger UI from 0.0.0.0 to localhost (see image below)

enter image description here

I’m using a docker compose with a swagger image.

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:

My current solution after some researching was to add a json configuration file as you can see below, with the SWAGGER_JSON.

  swagger:
    image: swaggerapi/swagger-ui
    ports:
      - "8080:8080"
    expose:
      - "8080"
    environment:
      API_URL: http://localhost:3000/
      SWAGGER_JSON: swagger.json

The json file looks like this

{
    "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"
    ]
}

Yet it is not working. My question would be the following, is the path for SWAGGER_JSON correct and also the json correct in its content?

2

Answers


  1. If you specify SWAGGER_JSON, you don’t need to specify API_URL as that seems to be the URL from which to load the swagger.json. Also from the swagger service definition in the question, it doesn’t look like the swagger.json file has been mounted into the container. And as the swagger.json in the question didn’t have any operations, I used the petstore swagger json instead, changing the URL to 127.0.0.1:3000 like in the question. With those things I ended up with a docker-compose.yml file like the below:

    services:
      swagger:
        image: swaggerapi/swagger-ui:v5.11.6
        ports:
          - "8080:8080"
        environment:
          SWAGGER_JSON: /specs/swagger.json
        volumes:
          - ./swagger.json:/specs/swagger.json
    

    With the above compose file, the outbound requests do show the expected host:

    curl -X 'GET' 
      'https://127.0.0.1:3000/v2/pet/1234' 
      -H 'accept: application/json'
    

    You can find all the changes including the swagger.json used on github.

    Login or Signup to reply.
  2. You need to use either one of them:-

    I would prefer swagger JSON, as below

    swagger:
        image: swaggerapi/swagger-ui
        ports:
          - "8080:8080"
        environment:
          SWAGGER_JSON: /specs/swagger.json
        volumes:
          - ./swagger.json:/specs/swagger.json
    
    

    The entries in your swagger.json looks good

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search