DEV Community

box
box

Posted on

Swagger UI + Docker: Initial Setup

Environment

  • Mac

    • CPU: Apple M1
    • OS: Sonoma 14.6.1
  • Docker Desktop

Dir Structure

dir-structure

* Drawn by mermaid.

graph TD;
    subgraph top["top"]
        subgraph docker["docker"]
            env[".env"]
            docker_compose["docker-compose.yml"]
        end
        subgraph openapi["openapi"]
            openapi_file["openapi.yml"]
        end
    end
Enter fullscreen mode Exit fullscreen mode

docker/.env

SWAGGER_UI_HOST_PORT={host-port}
Enter fullscreen mode Exit fullscreen mode

docker/docker-compose.yml

services:
  swagger-ui:
    image: swaggerapi/swagger-ui:v5.17.14
    environment:
      SWAGGER_JSON: /oas/openapi.yml
    ports:
      - "${SWAGGER_UI_HOST_PORT}:8080"
    volumes:
      - ../openapi:/oas
Enter fullscreen mode Exit fullscreen mode

openapi/openapi.yml

Define a minimal API documentation conforming to OAS 3.0.3.

openapi: 3.0.3
info:
  title: Sample API
  version: 1.0.0
paths:
  /hello:
    get:
      summary: Hello World Request
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type: string
                example: "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

Docker

Create Container and Image.

  1. Open the terminal.

  2. Move the current-dir to the location where docker-compose.yml is located.

  3. Execute docker compose create.

    docker compose create
    

Open Swagger UI.

  1. Start the container.

    docker compose start
    
  2. Open http://localhost:{SWAGGER_UI_HOST_PORT}.

  3. Stop the container once satisfied.

    docker compose stop
    

Top comments (0)