Seven Steps to Verify API Integration Between Docker Compose Services
Running containers do not establish successful service-to-service API integration. Check service-name access, readiness, authentication, and response contracts. After a write, verify stored content and deletion too.
This article runs a small API and a caller worker on the same Compose network.
Verification scope
A separate educational example ran during revision on 2026-10-24. It used neither the real blog API nor production data. The API stores data only in memory and is not a production server or authentication-system implementation.
| Item | Checked environment |
|---|---|
| Host | macOS, ARM64 |
| Docker Desktop | 4.55.0 |
| Docker Engine | 29.1.3 |
| Docker Compose | v2.40.3-desktop.1 |
| Container Python | 3.13.17 |
| Image | python:3.13-alpine, pinned to the executed image's digest |
Download the reproducible example
The archive contains no token; generate a fresh one before execution.
| File | Role |
|---|---|
compose.yaml | API, worker, network, secrets |
api.py | Educational HTTP API |
verify.py | Authentication, response, create/read/delete checks |
init_token.py | Generate a disposable token |
RUNBOOK.txt | Execution and cleanup sequence |
execution-log.txt | Output captured in this run |
1. Define the caller and destination
worker container → http://api:8080 → api containerServices on the same Compose network can use service names and container-internal ports. The worker therefore calls api:8080. Compose networking
Inside the worker, localhost means the worker itself, so localhost:8080 is not equivalent. This example publishes no host ports; calls originate from the worker, not a host browser or curl.
| Caller | Address here |
|---|---|
| Worker → API | http://api:8080 |
| API's own healthcheck | http://127.0.0.1:8080/health |
| Host → API | Not configured; no published port |
In another setup with published ports, the host uses the published port while containers use internal ports. Keep those addresses distinct.
2. Prepare configuration and a disposable token
Extract the archive and enter compose-api-demo. Run subsequent commands from the same directory and terminal.
python3 init_token.pyThis creates .secrets/demo_token.txt, does not print its value, and refuses to overwrite an existing file. Choose a unique project name:
demo_project="compose-api-demo-$(date +%Y%m%d%H%M%S)-$$"Use the same value for all Compose commands, including cleanup. The essential configuration follows; the complete file also pins the image digest used in the run.
services: api: image: python:3.13-alpine working_dir: /app command: ["python", "api.py"] volumes: - ./api.py:/app/api.py:ro secrets: [demo_token] healthcheck: test: - CMD - python - -c - "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health', timeout=2)" interval: 1s timeout: 3s retries: 30 worker: image: python:3.13-alpine working_dir: /app entrypoint: ["python", "verify.py"] volumes: - ./verify.py:/app/verify.py:ro secrets: [demo_token] depends_on: api: condition: service_healthynetworks: default: internal: truesecrets: demo_token: file: ./.secrets/demo_token.txtBoth services explicitly receive the required secret, read from /run/secrets/demo_token. Compose secrets
docker compose -p "$demo_project" config --quietPassing configuration validation establishes that Compose can interpret it, not that the API runs or communicates.
3. Distinguish running from ready
Start only the API:
docker compose -p "$demo_project" up -d apiThe educational readiness switch returns 503 from /health while /tmp/demo-ready is absent. It does not inspect real database connectivity or initialization.
Bypass dependency health waiting to inspect the not-ready response:
docker compose -p "$demo_project" run --rm --no-deps worker not-readyObserved output:
Python 3.13.17PASS not-ready: HTTP 503The process accepted and answered the request but reported not ready; this differs from a network-connection failure. Now switch readiness on:
docker compose -p "$demo_project" exec -T api python -c "from pathlib import Path; Path('/tmp/demo-ready').touch()"Ordinary worker startup uses depends_on.condition: service_healthy. Compose can wait for the dependency's healthcheck, but this does not guarantee every later API request succeeds. Startup order and readiness
4. Check health and authentication separately
/health is unauthenticated; data endpoints require a token. A passing healthcheck therefore does not establish authentication behavior.
| Request | Expected result |
|---|---|
GET /items/seed without token | 401, unauthorized |
| Same request with wrong token | 401, unauthorized |
| Same request with correct token | 200, specified item |
The program reads its secret into the Authorization header without printing it. This local, unpublished-port example uses HTTP and a disposable token. Do not carry that transport setup into production with real credentials: Bearer-token storage/transmission requires protection, and RFC 6750 requires TLS. Bearer token specification
These authentication checks run before the read check when the worker is invoked in step 6.
5. Distinguish HTTP 200 from a correct response
The /items/seed contract is:
{ "slug": "seed", "value": "hello"}The worker checks status, JSON Content-Type, fields, and values. Its item validator is:
def item_contract(body): check( isinstance(body, dict) and set(body) == {"slug", "value"} and all(isinstance(body[k], str) for k in ("slug", "value")), "item schema mismatch", )This deliberately strict contract permits only two fields. Adapt it if a real API permits additional fields.
To check the validator itself, /broken deliberately returns HTTP 200 with:
{ "unexpected": true}The HTTP request succeeds but the body violates the item contract. The worker verifies rejection. This output means the guard detected the malformed response, not that the response was valid:
PASS contract guard: rejected malformed HTTP 200 bodyConnectivity, authentication, and response correctness require separate evidence.
6. Execute create, readback, and deletion as one flow
| Request | Example contract |
|---|---|
| Read valid item | 200 and item JSON |
| Create item | 201 and created-item JSON |
| Read created item | 200 and stored value |
| Delete created item | 204 |
| Read deleted item | 404 and not_found |
These are this API's contract, not universal success criteria replacing a real service specification.
docker compose -p "$demo_project" run --rm workerAfter DNS, health, authentication, read, and contract checks pass, the worker creates one item with a verify- plus UUID identifier. It reads that identifier back and compares the exact value. The post-creation section attempts deletion in finally, then reads again to verify absence.
Observed output:
Python 3.13.17PASS DNS: apiPASS health: HTTP 200PASS auth-missing: HTTP 401PASS auth-wrong: HTTP 401PASS read: HTTP 200 + JSON contractPASS contract guard: rejected malformed HTTP 200 bodyPASS create: HTTP 201PASS read-after-write: exact value matchPASS cleanup: HTTP 204PASS absent-after-delete: HTTP 404Worker exit code was 0. The stored value existed in the running API process's memory. Database persistence, restart survival, concurrency, and duplicate creation after retries were not tested.
Even finally cannot guarantee cleanup after forced termination or lost communication. Tests against a persistent store need recorded IDs and an independent cleanup method.
7. Clean up test data and the example stack
Distinguish deletion from subsequent absence:
PASS cleanup: HTTP 204PASS absent-after-delete: HTTP 404Then remove the stack using the same project name:
docker compose -p "$demo_project" downClean up even if an earlier step failed. A new terminal must restore the original project-name value. In this run, both project-container and project-network listings were empty afterward:
docker ps -a --filter "label=com.docker.compose.project=$demo_project" --format '{{.ID}}'docker network ls --filter "label=com.docker.compose.project=$demo_project" --format '{{.ID}}'Finally remove only the generated token file:
python3 -c "from pathlib import Path; Path('.secrets/demo_token.txt').unlink()"Source and logs can remain, and the downloaded Python image remains cached. This example has no data-storage volume. In general, down does not erase external database records or every volume's data. --volumes broadens deletion and is unnecessary here. Compose down
What must change for a real service?
A passing educational example does not establish production integration.
| Example | Real-service investigation |
|---|---|
api:8080 | Caller, network, service name, internal port |
| Readiness file | Actual dependencies and readiness checks |
| Disposable shared token | Authentication, permission scope, secure transport |
slug/value | Status, field, and error contracts |
| In-memory data | Actual storage and readback consistency |
verify- ID | Collision-resistant test-data identification |
| Example DELETE | Supported deletion/recovery procedures |
A useful record includes caller/destination, environment/versions, readiness, accepted/rejected authentication, read-contract checks, create/read/delete results, cleaned resources, and unverified scope.
Different failures point to different next checks: unresolved service names to networking, 401 to authentication, and HTTP 200 with the wrong body to the API contract and implementation.
Report an error or share feedback
Open a draft with this article’s title and URL. Review the message and recipient before sending.
Open email draftIf no email app opens, copy these details into your usual email service.
Related posts
Backend & Infrastructure Three Production Problems Local Tests Missed
Flyway never ran, npm failed inside Docker, and cache writes failed on permissions. Local tests missed each problem.
Backend & Infrastructure Putting a Home Mac on Public HTTPS Without Port Forwarding: Four Checks on Cloudflare Tunnel's First Boot
The connector, container address, DNS, and certificate checks needed to get one hello line onto a public URL.