# Docker Compose preview environments for every pull request

Gregale · Updated 2026-12-01

Set up GitHub PR previews for a Docker Compose API and its private service, then verify each preview gets the right data without calling production.

A Docker Compose preview environment should run the services from the pull request together. If a PR changes your API and its pricing service, the API preview should call that PR's pricing service, not production and not another PR. On Gregale, declare that dependency in Compose, connect the repository to GitHub, and keep preview-to-production service calls set to `deny`.

[Download the runnable example](https://gregale.dev/examples/docker-compose-pr-preview-environments.zip)

## 1. Declare a public-facing API and a private dependency

The API gets a routable app URL through `ports` (it can still require authentication). The pricing service uses `expose` without `ports`, so it stays internal while serving HTTP. `depends_on` declares the dependency Gregale should include in the preview. The `declared` service policy keeps unrelated services out.

```yaml
x-gregale-service-policy: declared
services:
  api:
    build: ./api
    ports: ['8080:8080']
    depends_on: [pricing]
    environment:
      PRICING_URL: http://pricing:8080
  pricing:
    build: ./pricing
    expose: ['8080']
    x-gregale-allow-callers: [api]
    x-gregale-preview-calls: deny
```

Locally, the API uses the Compose hostname at `PRICING_URL`. On Gregale, it prefers the injected `GREGALE_SERVICE_PRICING_URL`, which lets the platform route the internal request for the caller's project and PR. The app does not retry a different URL when that upstream fails. See [how internal service calls are scoped](https://gregale.dev/docs/internal-services.md).

## 2. Deploy the baseline and bind the GitHub repository

Put the example at the root of a new repository. Commit the baseline price of `1000` cents to `main`, then [connect the Gregale GitHub App](https://gregale.dev/docs/deploy-from-github.md) to that repository. Run these commands from the sample root with an authenticated CLI:

```bash
PROJECT='replace-with-a-new-demo-slug'
gregale deploy --project --project-slug "$PROJECT" --prefix "$PROJECT-" --dry-run
gregale deploy --project --project-slug "$PROJECT" --prefix "$PROJECT-"
gregale project get "$PROJECT"
```

Review the plan before applying it: only the API should have an app URL; the pricing workload should be internal HTTP. Take the API app slug from the project output and bind it to the default branch:

```bash
API_APP='replace-with-api-app-slug'
gregale github link "$API_APP" --repo 'OWNER/REPOSITORY' --branch main
gregale github status "$API_APP"
```

In the [project GitHub deployment policy](https://gregale.dev/docs/preview-environments.md#project-github-deployment-policy), confirm previews are enabled and the preview service policy is `deny`. Keep production credentials out of previews. If a preview needs a datastore, use separate test credentials and verify the connection explicitly; this example has none.

## 3. Prove two open PRs stay separate

Create two branches separately from `main`. Change only `pricing/price.txt` to `1100` in one and `1200` in the other. Open both PRs into `main` from the connected repository, not from forks. Wait for the `gregale-preview` check on each PR head, then use the API URL in each PR comment. Send an ordinary GET to `/price` on both preview URLs and the baseline app URL, authenticating as each app requires.

| URL                   | Expected response      |
| --------------------- | ---------------------- |
| Stable production API | `{"price_cents":1000}` |
| API preview for PR A  | `{"price_cents":1100}` |
| API preview for PR B  | `{"price_cents":1200}` |

All three requests should return HTTP 200. If a preview returns the production price, or the other PR's price, treat that as a failure. If it returns 502, inspect the PR check and pricing logs; the app returns 502 when its configured upstream is unavailable. Don't fix it by pointing the preview at your live pricing API.

Close one PR **without merging**. Gregale marks the preview closed, but the URL can still serve during the documented 24-hour grace period. The other PR and production should keep answering independently. Check the first preview's teardown after that window, not immediately after closing.

## What we verified

On a disposable Gregale Scale project, the dry-run classified the pricing service as internal HTTP with no public URL. After deploying with an explicit app prefix, an authenticated GET to the baseline API returned HTTP 200 with `{"price_cents":1000}` through that service.

The local process harness returned the exact three expected prices and produced HTTP 502 when one configured pricing service stopped, while the other two kept serving. That exercises the sample's HTTP behavior, not Gregale's preview isolation. The Scale test account had no Gregale GitHub App installation, so we did not verify the GitHub PR routing or teardown end to end.

For scope, setup, and troubleshooting, see the [PR preview docs](https://gregale.dev/docs/preview-environments.md) and [multi-service apps guide](https://gregale.dev/docs/multi-service.md).

## Questions

### How do I create Docker Compose preview environments for GitHub pull requests?

Deploy the Compose project, connect its repository through the Gregale GitHub App, bind the API workload to the target branch, and open a pull request from a branch in that repository. Declare callable dependencies with depends_on so they join the preview.

### Can a Docker Compose PR preview call the production version of a private service?

Gregale first looks for the target preview in the same account, project, and PR. With the project preview service policy set to deny, a missing preview dependency cannot fall back to its production service.

### Does closing a pull request immediately remove its Gregale preview?

No. Gregale marks the preview closed and allows it to serve during the documented 24-hour grace period before teardown. Other PR previews are separate.

Canonical URL: https://gregale.dev/articles/docker-compose-pr-preview-environments

