[BUG] Error when running with Docker
morley92 opened this issue · comments
Checklist
- I checked the FAQ section of the documentation
- I looked for similar issues in the issue tracker
- I am using the latest version of Schemathesis
Describe the bug
Schemathesis run via Docker won't connect to my locally running application.
To Reproduce
- Create a dummy server file -
app.js
const http = require("http")
const server = http.createServer((req, res) => {
if (req.method === "GET") {
if (req.url === "/cats") {
res.writeHead(200, { "Content-Type": "application/json" })
res.end(JSON.stringify({ message: "Hello" }))
}
}
})
const PORT = 3003
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`)
})
- Create a dummy OpenAPI specification -
cats-swagger.yml
openapi: 3.0.0
info:
title: Simple JSON API
version: 1.0.0
paths:
/cats:
get:
summary: Get a friendly greeting
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: Hello
-
Run the server -
node app.js
-
Run schemathesis via Docker
docker run --platform linux/amd64 --network host -v $(pwd)/cats-swagger.yml:/mnt/cats-swagger.yml schemathesis/schemathesis:latest run --hypothesis-max-examples=1 --base-url http://0.0.0.0:3003 /mnt/cats-swagger.yml
(I've tried against localhost
and 127.0.0.1
also)
- Observe the error
======================= Schemathesis test session starts =======================
Schema location: file:///mnt/cats-swagger.yml
Base URL: http://0.0.0.0:3003
Specification version: Open API 3.0.0
Workers: 1
Collected API operations: 1
GET /cats E [100%]
==================================== ERRORS ====================================
__________________________________ GET /cats ___________________________________
Network Error
Connection failed
Failed to establish a new connection: [Errno 111] Connection refused
Need more help?
Join our Discord server: https://discord.gg/R9ASRAmHnA
=================================== SUMMARY ====================================
No checks were performed.
Hint: You can visualize test results in Schemathesis.io by using `--report` in your CLI command.
Expected behavior
No network errors
Environment
- OS: MacOS (Apple M1 Pro)
- Python version: N/A
- Schemathesis version: Latest
- Spec version: Open API 3.0.0
Tested on both Work and Home laptop (same specs)
Additional context
Include any other relevant details, like logs or screenshots.
Hi @morley92
Hm, seems to be MacOS-specific, as it seems to work on my Linux machine but fails on my laptop. I'll take a closer look in the next following days.
Using host.docker.internal
:
docker run --platform linux/amd64 -v $(pwd)/cats-swagger.yml:/mnt/cats-swagger.yml schemathesis/schem
athesis:latest run --hypothesis-max-examples=1 --base-url http://host.docker.internal:3003 /mnt/cats-swagger.yml
======================= Schemathesis test session starts =======================
Schema location: file:///mnt/cats-swagger.yml
Base URL: http://host.docker.internal:3003
Specification version: Open API 3.0.0
Workers: 1
Collected API operations: 1
GET /cats . [100%]
=================================== SUMMARY ====================================
Performed checks:
not_a_server_error 1 / 1 passed PASSED
Tip: Use the `--report` CLI option to visualize test results via Schemathesis.io.
We run additional conformance checks on reports from public repos.
============================== 1 passed in 0.02s ===============================
Here is the relevant section from Docker for Mac docs:
The host has a changing IP address, or none if you have no network access. We recommend that you connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host.
I'll update the docs to mention this.
thankyou!