How to Use Swagger with Docker

How to Use Swagger with Docker

To follow this guide, you need:

  • Docker installed on your machine.
  • Docker Compose installed on your machine.

This is an example of a Docker Compose file for Swagger Editor, Swagger UI, and Swagger Codegen. Copy this file and save it as docker-compose.yml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
services:
  swagger-editor:
    image: swaggerapi/swagger-editor
    ports:
      - "8080:8080"
    environment:
      SWAGGER_FILE: /data/swagger.yml
    volumes:
      - ./data:/data

  swagger-ui:
    image: swaggerapi/swagger-ui
    ports:
      - "8081:8080"
    environment:
      SWAGGER_JSON: /data/swagger.yml
    volumes:
      - ./data:/data

  swagger-codegen:
    image: openapitools/openapi-generator-cli
    user: "1000:1000"
    volumes:
      - ./data:/data
    command: ["help"]
  1. Open a terminal and navigate to the directory where you saved the docker-compose.yml file.

  2. Run the following command to start the services:

    1
    2
    
    docker-compose up -d
    docker-compose down
    
  1. Open your web browser:
    • Swagger Editor: http://localhost:8080
    • Swagger UI: http://localhost:8081

Now you can edit your API definition in Swagger Editor and view it in Swagger UI.

To generate code for client, server, or documentation, follow these steps:

  1. Ensure your docker-compose.yml file includes the swagger-codegen service as shown above.

  2. Open a terminal and run the following command to generate code:

    1
    
    docker compose run swagger-codegen generate --api-package 3.0.0 -i /data/swagger.yml --generator-name html2 -o /data/output
    

    Replace language, file name with the right value.

  3. The generated code will be saved in the folder ./data/output.

Using Docker and Docker Compose, you can easily set up Swagger Editor, Swagger UI, and Swagger Codegen. This allows you to edit, view, and generate code from your API definitions quickly and efficiently.