How to Use Swagger with Docker
How to Use Swagger with Docker
1 Prerequisites
To follow this guide, you need:
- Docker installed on your machine.
- Docker Compose installed on your machine.
2 Docker Compose File
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
:
|
|
3 Start / Stop container using docker compose command
-
Open a terminal and navigate to the directory where you saved the
docker-compose.yml
file. -
Run the following command to start the services:
1 2
docker-compose up -d docker-compose down
4 Access Swagger Editor and Swagger UI
- Open your web browser:
- Swagger Editor:
http://localhost:8080
- Swagger UI:
http://localhost:8081
- Swagger Editor:
Now you can edit your API definition in Swagger Editor and view it in Swagger UI.
5 Generate Code with Swagger Codegen
To generate code for client, server, or documentation, follow these steps:
-
Ensure your
docker-compose.yml
file includes theswagger-codegen
service as shown above. -
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 rightvalue
. -
The generated code will be saved in the folder
./data/output
.
6 Conclusion
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.