Prism(API Mock Server) With Docker

yurukuma
2 min readNov 29, 2021

Install

npx @stoplight/prism-cli

docker-compose.yml

version: '3.9'services:  prism:    image: stoplight/prism:4    command: 'mock -h 0.0.0.0 /tmp/test-api.yaml'    volumes:    - ./test-api.yaml:/tmp/test-api.yaml:ro    ports:    # Serve the mocked API locally as available on port 4010     - '4010:4010'

test-api.yaml

openapi: 3.0.1
info:
title: anisAPI
version: '1.0'
paths:
'/getName/{userId}':
parameters:
- schema:
type: integer
name: userId
in: path
required: true
description: Id of an existing user.
get:
summary: Get User Info by User ID
tags: []
responses:
'200':
description: User Found
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'404':
description: User Not Found
operationId: get-users-userId
description: Retrieve the information of the user with the matching user ID.
'/getName':
parameters:
- schema:
type: string
name: emailId
in: query
required: true
description: Email of an existing user.
get:
summary: Get User Info by User Email
tags: []
responses:
'200':
description: User Found
content:
application/json:
schema:
$ref: '#/components/schemas/User'
'404':
description: User Not Found
operationId: get-users-email
description: Retrieve the information of the user with the matching user email.
components:
schemas:
User:
title: User
type: object
description: ''
properties:
id:
type: integer
description: Unique identifier for the given user.
firstName:
type: string
lastName:
type: string
email:
type: string
format: email
required:
- id
- firstName
- lastName
- email

準備好swagger yaml檔後

docker-compose up

> Info(from stoplightio/prism github)

Cannot access mock server when using Docker?

Prism uses localhost by default, which usually means 127.0.0.1. When using docker the mock server will be unreachable outside of the container unless you run the mock command with -h 0.0.0.0.

github專案

Reference

[1][stoplightio/prism github]

[2][API Mocking Using PRISM Mock API Server and Docker]

--

--

yurukuma

I’m a front-end developer. Let’s learning by doing:)