← Blog

Claude API Docker Compose Setup Guide

2026-11-20 · 4 min read · SubToAPI Team

Claude API Docker Compose Setup Guide

If you're building a service that calls the Claude API and want it to run reliably in a container alongside a database, a cache, or a worker queue, Docker Compose is the fastest way to get a reproducible local and staging environment. This guide walks through a working docker-compose.yml for a Node.js service that talks to Claude, covering environment variables, secrets handling, networking between containers, and the small details that break deployments if you skip them.

The short answer: you don't containerize "the Claude API" itself — it's a hosted service — you containerize your application and give it network access plus an API key as an environment variable. The setup below gets that running in under ten minutes.

Project layout

A minimal setup for a Claude-backed API service looks like this:

.
├── docker-compose.yml
├── Dockerfile
├── .env
├── package.json
└── src/
    └── server.js

The Dockerfile

Keep the image small and avoid baking secrets into it:

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --omit=dev

COPY src ./src

EXPOSE 3000
CMD ["node", "src/server.js"]

docker-compose.yml

version: "3.9"

services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - CLAUDE_API_KEY=${CLAUDE_API_KEY}
      - CLAUDE_BASE_URL=${CLAUDE_BASE_URL:-https://api.anthropic.com/v1}
    env_file:
      - .env
    depends_on:
      - redis
    networks:
      - app-net
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    volumes:
      - redis-data:/data
    networks:
      - app-net

networks:
  app-net:
    driver: bridge

volumes:
  redis-data:

Redis here is optional — it's included because most production setups want a cache for rate-limit tracking, idempotency keys, or storing streamed response chunks for replay. Drop it if your service is stateless.

Handling the API key safely

Never commit .env to version control. Add it to .gitignore and provide a .env.example with placeholder values:

CLAUDE_API_KEY=sk-ant-xxxxxxxxxxxxxxxx
CLAUDE_BASE_URL=https://api.anthropic.com/v1

Compose automatically reads .env from the project root for variable substitution in the YAML (the ${CLAUDE_API_KEY} syntax), and env_file injects the same values into the container's runtime environment. You technically only need one of the two mechanisms, but keeping both explicit makes the file self-documenting for teammates who haven't used Compose before.

For staging and CI, inject the key through your pipeline's secret store instead of a checked-in file — GitHub Actions secrets, GitLab CI variables, or your cloud provider's secret manager all work fine with docker compose config picking up the environment at build time.

A minimal server calling Claude

// src/server.js
const express = require("express");
const app = express();
app.use(express.json());

app.post("/ask", async (req, res) => {
  const response = await fetch(`${process.env.CLAUDE_BASE_URL}/messages`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-api-key": process.env.CLAUDE_API_KEY,
      "anthropic-version": "2023-06-01",
    },
    body: JSON.stringify({
      model: "claude-3-5-sonnet-20241022",
      max_tokens: 1024,
      messages: [{ role: "user", content: req.body.prompt }],
    }),
  });

  const data = await response.json();
  res.json(data);
});

app.listen(3000, () => console.log("listening on 3000"));

Run it with:

docker compose up --build

Then test:

curl -X POST http://localhost:3000/ask \
  -H "Content-Type: application/json" \
  -d '{"prompt":"Say hello in one sentence"}'

Networking notes that actually matter

healthcheck:
  test: ["CMD", "wget", "-qO-", "http://localhost:3000/health"]
  interval: 30s
  timeout: 5s
  retries: 3

Avoiding provider lock-in and billing surprises

One issue teams hit after containerizing a Claude-backed service: managing per-developer API keys, usage caps, and billing across a team becomes its own ops problem once more than one person or environment is calling the API. If you'd rather not build key rotation, per-key usage limits, and team billing yourself, SubToAPI sits in front of your existing Claude access and gives you sub_live_... application keys with streaming, tool use support, and usage metadata per key — so your Compose setup above only needs one environment variable pointed at https://api.subtoapi.app/v1 instead of managing provider credentials per environment. Check the quickstart or pricing if that's useful for your team.

Production hardening checklist

questions

Do I need a special base image for Claude API calls? No. Any standard runtime image (Node, Python, Go) works since you're making plain HTTPS requests. There's no SDK-specific container requirement.

Can I run multiple services sharing one API key in Compose? Yes — define the key once in .env and reference it via environment in each service that needs it, or centralize calls through a single internal gateway service to avoid duplicating rate-limit logic.

How do I keep the API key out of my Docker image layers? Pass it at runtime through environment or env_file, never through a COPY or ARG baked into the image. Build-time ARG values can leak into image history even if unused later.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →