Those who have not yet 1 -[Create AWS/EC2 instance] 1 Those who have not yet 2 ・ [Nginx settings] 2 Those who have not yet 3 ・ [Swagger-ui settings] 3 Those who have not yet 4 -[Settings to make swagger-ui read arbitrary files] 4
Please refer to the above and meet all the prerequisites first.
sshLogin.ec2
ssh -i keypair_hogehoge.pem ec2-user@******
If you see an image like the one above, then ** SSH connection is successful **.
Pull the docker image of [stoplight/prism] 5 after SSH connection
prismPull.docker
sudo docker pull stoplight/prism:3
If it looks like the above image, the pull is successful.
sudo vim /etc/nginx/nginx.conf
server {
listen 80;
listen [::]:80;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
//swagger-editor settings
location /swagger-editor/docker/ {
proxy_pass http://localhost:8000/;
proxy_redirect off;
}
//swagger-ui settings
location /swagger-ui/docker/ {
proxy_pass http://localhost:8001/;
proxy_redirect off;
}
//Mock server settings(This time set this)
location /swagger-mock/docker/ {
proxy_pass http://localhost:4010/;
proxy_redirect off;
}
This time, we will use the yaml file set in [here] 4. Then add a mock server under ** servers **.
servers:
- url: http://petstore.swagger.io/
//Settings for mock server
- url: http://IP address of the EC2 instance/swagger-mock/docker/
Please change the part of ** IP address of EC2 instance ** to an appropriate value.
You must specify ** any yaml file ** when starting the mock server.
This setting Container name: prism Directory where yaml files are stored:/home/ec2-user/www yaml file: pet.yaml
prism.docker
sudo docker run --name prism -it -p 4010:4010 -v /home/ec2-user/www:/tmp stoplight/prism:3 mock -h 0.0.0.0 /tmp/pet.yaml
Container name specification (optional)
containerName.docker
--name hogehoge
yaml file specification (required)
yaml.docker
mock -h 0.0.0.0 /tmp/pokemon.yaml
If it is displayed in the terminal as shown below, it is OK.
[ec2-user@ip-00-0-0-00 ~]$ sudo docker run --name prism -it -p 4010:4010 -v /home/ec2-user/www:/tmp stoplight/prism:3 mock -h 0.0.0.0 /tmp/pet.yaml
[3:37:33 AM] › [CLI] … awaiting Starting Prism…
[3:37:34 AM] › [CLI] ℹ info GET http://0.0.0.0:4010/pets?limit=60
[3:37:34 AM] › [CLI] ▶ start Prism is listening on http://0.0.0.0:4010
Go to swagger-ui and select the right server
If the success response set in the yaml file is displayed as a result as shown below, it is OK
You should also see something like the following in your terminal.
[5:55:41 AM] › [HTTP SERVER] get /pets ℹ info Request received
[5:55:41 AM] › [NEGOTIATOR] ℹ info Request contains an accept header: application/json
[5:55:41 AM] › [VALIDATOR] ✔ success The request passed the validation rules. Looking for the best response
[5:55:41 AM] › [NEGOTIATOR] ✔ success Found a compatible content for application/json
[5:55:41 AM] › [NEGOTIATOR] ✔ success Responding with the requested status code 200
If you don't need to check the result of [Try it out] → [Excute] on the terminal, you can run the mock server in the background.
Foreground ver
sudo docker run --name prism -it -p 4010:4010 -v /home/ec2-user/www:/tmp stoplight/prism:3 mock -h 0.0.0.0 /tmp/pokemon.yaml
Background ver
sudo docker run --name prism -it -d -p 4010:4010 -v /home/ec2-user/www:/tmp stoplight/prism:3 mock -h 0.0.0.0 /tmp/pet.yaml
-d
With this option, each container can be started in the background.
docker: Error response from daemon: Conflict. The container name "/prism" is already in use by container "768393267a3839c7db21028daeb8f5a39b67766f994615de3e068ea7f819c500". You have to remove (or rename) that container to be able to reuse that name.
If you get the above error after entering the ** docker run ** command, it is because the container name is covered. Enter the command below to delete the container.
sudo docker rm arbitrary container name
Don't forget to stop the container before deleting it.
sudo docker stop Arbitrary container name
The above is how to build a mock server using [stoplight/prism] 5.
If you have any questions or questions, please leave them in the comments section.
Recommended Posts