Display API definition in Swagger UI using Docker + Rails6 + apipie


  1. Automatically generate a file with API definitions
  2. Manage Swagger UI containers with docker-compose
  3. View API definition & request / response with Swagger UI in local environment


Ruby: 2.7.1
Rails: 6.0.3

1. Automatically generate a file with API definitions


It seemed that the API definition file could be generated with the gem apipie \ -rails, so I used this.

Follow Getting started to add to Gemfile and initialize apipie.


gem "apipie-rails"
$ bundle install
$ rails g apipie:install

apipie.rb is generated and apipie is added to routes.rb.

Document filling

Based on DSL Reference, write the API definition at the top of the method in each Controller. (This is my own work ...)

The following is an implementation example


class Api::V1::UsersController < ApiController

  api :POST, "/api/v1/users/token", "get access token"
  description "Authenticate login and return token"
  formats ["json"]
  param :email, String, desc: "mail address", required: true
  param :password, String, desc: "password", required: true
  returns code: 200, desc: "return user token"
  error code: 401, desc: "Unauthorized"
  example <<-JSON
      detail: "Token Example",

  def token
    #Implementation abbreviation

Generate API definition file

$ rails apipie:static_swagger_json

schema_swagger_form_data.json is generated in doc / apidoc.

2. Manage Swagger UI containers with docker-compose

docker-compose settings


    image: swaggerapi/swagger-ui
      - "8081:8080"
      - doc/apidoc/schema_swagger_form_data.json:/swagger.json
      SWAGGER_JSON: /swagger.json

When you start docker and access localhost: 8081, you will be able to read the API definition.

スクリーンショット 2020-10-17 17.41.34.png

Reference: Run Swagger Editor and Swagger UI with Docker

Adjust apipie.rb

Since the directory structure and port differ depending on the project, adjust apipie.rb if necessary. You can also find more details in the reference Swagger -Specific Configuration Parameters.


-  config.api_base_url            = "/api"
+  config.api_base_url            = ""
+  config.swagger_api_host = "localhost:3100"

Don't forget to generate the document after the adjustment.

3. View API definition & request / response in Swagger UI of local environment

With the current settings, the API and Swagger UI containers are different, so API requests / responses cannot be made. Set CORS to solve it.

CORS settings

Add the gem of rack \ -cors and add the settings as below.


gem "rack-cors"


module App
  class Application < Rails::Application

    #Add the following
    config.x.request = ActiveSupport::InheritableOptions.new(config_for(:request))

    # Permit cross origin
    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins Rails.application.config.x.request["domain"]
        resource "*",
        headers: :any,
        methods: [:get, :post, :put, :patch, :delete, :options, :head]


default: &default
   - localhost:3100 #app port
   - localhost:8081 # swagger-ui port

  <<: *default

  <<: *default


After rewriting, restart the app container and you will be able to make requests from the Swagger UI.

reference: -TypeError: Failed to fetch in Swagger \ -UI and die -[Separate CORS settings for each environment](https://ti-tomo-knowledge.hatenablog.com/entry/2019/07/25/132408#CORS%E3%81%AE%E8%A8%AD% E5% AE% 9A% E3% 82% 92% E7% 92% B0% E5% A2% 83% E3% 81% 94% E3% 81% A8% E3% 81% AB% E5% 88% 86% E3% 81% 91% E3% 82% 8B)

Recommended Posts

Display API definition in Swagger UI using Docker + Rails6 + apipie
Build an environment of "API development + API verification using Swagger UI" with Docker
How to launch Swagger UI and Swagger Editor in Docker
[Rails API + Vue] Upload and display images using Active Storage
Display weather forecast using OpenWeatherMap, an external API in Ruby
Display Flash messages in Rails
[Rails 6] API development using GraphQL (Query)
Hit the Docker API in Rust
I tried using Elasticsearch API in Java
Deployed using Docker + Rails + AWS (EC2 + RDS)
Try using the Stream API in Java
Try using the Rails API (zip code)
Rails API server environment construction using docker-compose
Try using JSON format API in Java
Show Better Errors in Rails + Docker environment
Display API specifications with Laravel + SwaggerUI + Docker
Implement button transitions using link_to in Rails