Forge Fate
Backend & Infrastructure

Seven Steps to Verify API Integration Between Docker Compose Services

4 min read

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.

ItemChecked environment
HostmacOS, ARM64
Docker Desktop4.55.0
Docker Engine29.1.3
Docker Composev2.40.3-desktop.1
Container Python3.13.17
Imagepython: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.

FileRole
compose.yamlAPI, worker, network, secrets
api.pyEducational HTTP API
verify.pyAuthentication, response, create/read/delete checks
init_token.pyGenerate a disposable token
RUNBOOK.txtExecution and cleanup sequence
execution-log.txtOutput captured in this run

1. Define the caller and destination

text
worker container → http://api:8080 → api container

Services 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.

CallerAddress here
Worker → APIhttp://api:8080
API's own healthcheckhttp://127.0.0.1:8080/health
Host → APINot 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.

bash
python3 init_token.py

This creates .secrets/demo_token.txt, does not print its value, and refuses to overwrite an existing file. Choose a unique project name:

bash
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.

yaml
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.txt

Both services explicitly receive the required secret, read from /run/secrets/demo_token. Compose secrets

bash
docker compose -p "$demo_project" config --quiet

Passing configuration validation establishes that Compose can interpret it, not that the API runs or communicates.

3. Distinguish running from ready

Start only the API:

bash
docker compose -p "$demo_project" up -d api

The 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:

bash
docker compose -p "$demo_project" run --rm --no-deps worker not-ready

Observed output:

text
Python 3.13.17PASS not-ready: HTTP 503

The process accepted and answered the request but reported not ready; this differs from a network-connection failure. Now switch readiness on:

bash
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.

RequestExpected result
GET /items/seed without token401, unauthorized
Same request with wrong token401, unauthorized
Same request with correct token200, 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:

json
{  "slug": "seed",  "value": "hello"}

The worker checks status, JSON Content-Type, fields, and values. Its item validator is:

python
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:

json
{  "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:

text
PASS contract guard: rejected malformed HTTP 200 body

Connectivity, authentication, and response correctness require separate evidence.

6. Execute create, readback, and deletion as one flow

RequestExample contract
Read valid item200 and item JSON
Create item201 and created-item JSON
Read created item200 and stored value
Delete created item204
Read deleted item404 and not_found

These are this API's contract, not universal success criteria replacing a real service specification.

bash
docker compose -p "$demo_project" run --rm worker

After 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:

text
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 404

Worker 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:

text
PASS cleanup: HTTP 204PASS absent-after-delete: HTTP 404

Then remove the stack using the same project name:

bash
docker compose -p "$demo_project" down

Clean 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:

bash
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:

bash
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.

ExampleReal-service investigation
api:8080Caller, network, service name, internal port
Readiness fileActual dependencies and readiness checks
Disposable shared tokenAuthentication, permission scope, secure transport
slug/valueStatus, field, and error contracts
In-memory dataActual storage and readback consistency
verify- IDCollision-resistant test-data identification
Example DELETESupported 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.

To: [email protected]

Open email draft

If no email app opens, copy these details into your usual email service.

Contact information