Stoplight Prism - Creating Docker image with API specification embeded inside

Prism is very lightweight and easy to use API mocking server which reads API specification and creates mock API endpoints for the developer. It can create dynamic values for fields and can validate request data against the specification.

Another feature is creating a proxy server that will help you debug API call from client to API server. In this mode, prism intercepts incoming requests and outgoing responses so you can validate the work done by the backend developer.

You have to start a separate instance for separate rate API specification. If you have multiple Microservices specification, you may need to run various prism mock server for each specification. To make this easy, we can create a docker image with API specification document can run multiple containers mapped on different port number.

Now, I wanted to deploy this mock server for my API on a cloud server to access the developer and rebuild this docker container if anything changes in the specification document. I automated this process using CI/CD pipeline in which docker image having prism and updated API spec gets to rebuild and redeployed automatically on the cloud server.

Here is the simple Dockerfile, which helps me to create a docker image.

 

FROM stoplight/prism:4
ADD api-spec.json /usr/src/prism/
EXPOSE 4010
CMD ["mock","-h","'0.0.0.0'","/usr/src/prism/api-spec.json"]

here, api-spec.json is my API document in OpenAPI-v3 format.

Now, by running the following command, I can create multiple container from this image.

Create Image:

 docker build -t my-api-spec/api-mock-server . 

Create container:

docker run -it -p 4010:4010 --name api-mockserver my-api-spec/api-mock-server

 

 

Tags