documentation/guides/mock-server/docker.md
Run the Mock Server in a Docker container.
Pull and run the Docker image:
# Using command-line URL argument (Method 1)
docker run -p 3000:3000 \
scalarapi/mock-server --url https://api.example.com/openapi.yaml
# Using environment variable with document content (Method 2)
docker run -p 3000:3000 \
-e OPENAPI_DOCUMENT="$(cat ./openapi.yaml)" \
scalarapi/mock-server
# Using URL environment variable (Method 3)
docker run -p 3000:3000 \
-e OPENAPI_DOCUMENT_URL="https://api.example.com/openapi.yaml" \
scalarapi/mock-server
# Using volume mount (Method 4)
docker run -p 3000:3000 \
-v ./openapi.yaml:/docs/openapi.yaml:ro \
scalarapi/mock-server
Visit http://localhost:3000 to access your mock API endpoints, and http://localhost:3000/scalar for the built-in API reference documentation.
The Docker image supports four ways to provide your OpenAPI document:
Pass a URL to your OpenAPI document using the --url flag:
docker run -p 3000:3000 \
scalarapi/mock-server --url https://api.example.com/openapi.yaml
The container will download the document from the specified URL at startup.
Pass the content of your OpenAPI document directly via the OPENAPI_DOCUMENT environment variable:
docker run -p 3000:3000 \
-e OPENAPI_DOCUMENT="$(cat ./openapi.yaml)" \
scalarapi/mock-server
Fetch your OpenAPI document from a URL using the OPENAPI_DOCUMENT_URL environment variable:
docker run -p 3000:3000 \
-e OPENAPI_DOCUMENT_URL="https://api.example.com/openapi.yaml" \
scalarapi/mock-server
The container will download the document from the specified URL at startup.
Mount your OpenAPI documents directory to /docs in the container:
docker run -p 3000:3000 \
-v ./documents:/docs:ro \
scalarapi/mock-server
The container automatically:
/docs directory recursively.json, .yaml, .yml files)The container uses the following priority when loading documents:
--url <URL> command-line argument (highest priority)OPENAPI_DOCUMENT environment variableOPENAPI_DOCUMENT_URL environment variable/docs directory (lowest priority)If none of these methods provide a document, the container will exit with an error message.
The Docker image includes several built-in features:
All endpoints defined in your OpenAPI document are automatically available as mock endpoints. The server generates realistic responses based on your schemas and examples.
Access the interactive API reference documentation at /scalar:
http://localhost:3000/scalar
Your OpenAPI document is automatically served at:
/openapi.json - JSON format/openapi.yaml - YAML formatThe format is automatically determined based on your source document.
This helps with debugging and monitoring during development.
Here's a complete docker-compose.yml example:
services:
mock-server:
image: scalarapi/mock-server:latest
ports:
- "3000:3000"
volumes:
# Mount your OpenAPI documents directory
- ./documents:/docs:ro
environment:
# Optional: Use environment variable instead of volume mount
# OPENAPI_DOCUMENT: |
# openapi: 3.0.0
# info:
# title: My API
# version: 1.0.0
# Or use URL to fetch document
# OPENAPI_DOCUMENT_URL: https://api.example.com/openapi.yaml
restart: unless-stopped
| Variable | Description | Required | Default |
|---|---|---|---|
OPENAPI_DOCUMENT | OpenAPI document content (JSON or YAML string) | No* | - |
OPENAPI_DOCUMENT_URL | URL to fetch OpenAPI document from | No* | - |
* At least one method (command-line argument, environment variable, or volume mount) must be provided.
The mock server runs on port 3000 inside the container by default. Map it to any host port:
# Map to host port 8080
docker run -p 8080:3000 \
-v ./openapi.yaml:/docs/openapi.yaml:ro \
scalarapi/mock-server
# Access at http://localhost:8080
If the container exits immediately, check:
--url <URL> command-line argumentOPENAPI_DOCUMENT environment variableOPENAPI_DOCUMENT_URL environment variable/docs directoryOPENAPI_DOCUMENT_URL, verify the URL is accessible and returns a valid OpenAPI documentdocker logs <container-id> to see error messagesIf port 3000 is already in use, map to a different port:
docker run -p 8080:3000 scalarapi/mock-server