skip to content

In Bitbucket Pipelines, how do integration tests reach a database service container, and why can adding one exhaust the step's memory?

level: middleimportance: should knowfreq 20%

answer

  1. sidecar declared under definitions
  2. shared network, no port mapping
  3. 127.0.0.1 and a wait loop
  4. one memory pool per step
  5. 1024 MB default per service

basics

~20 s

A Bitbucket Pipelines service is a sidecar container defined under definitions services and listed on the step; it shares the step's network, so tests connect on 127.0.0.1. Each service takes 1024 MB by default out of the step's single memory pool.

solid answer

~50 s

You define the database once under `definitions: services` (an `image`, optional `variables` and `memory`) and list it under the step's `services`. Pipelines starts it as a separate Docker container that shares the build container's network adapter, so its ports are open on localhost; the documentation recommends `127.0.0.1` over `localhost` to avoid IPv6 problems, and there is no built-in readiness check, so the script should wait until the database answers. The memory is the catch: a step has one pool, 4096 MB at `1x` and 8192 MB at `2x`, shared by the script and every service. Each service reserves 1024 MB unless you set `memory`, the build container keeps at least 1024 MB, and the `docker` service, which pipes also use, counts as a service too. Lower a service's `memory` or raise the step `size`, remembering that `2x` also uses twice the build minutes.

code

yaml · 16 lines
yaml
definitions:
  services:
    test-db:
      image: acme/integration-db:16
      memory: 1536
      variables:
        DB_PASSWORD: $TEST_DB_PASSWORD
pipelines:
  default:
    - step:
        name: Integration tests
        services:
          - test-db
        script:
          - ./wait-for-db.sh 127.0.0.1
          - ./run-integration-tests.sh

go deeper

for a junior

Recall that services are extra containers defined under definitions and listed on a step, reached on 127.0.0.1.

for a middle

Explain the single memory pool per step, the 1024 MB default per service, the docker service's share, and why a wait loop is needed before tests.

for a senior

Show that you diagnose an out-of-memory step by doing the memory arithmetic, right-size services first, and raise step size only where it pays for its doubled minutes.

for a principal

Weigh realistic integration environments inside CI against their memory and minute cost, and where a shared external test environment would be cheaper or riskier.

## What a service container is In Bitbucket Pipelines, every step runs in a Docker container (the **build container**). A **service** is an additional container that runs beside it for the life of that step: a database, a cache server, a stub of a web service your integration tests call. Services are how a web service's database-backed integration tests get a real database without installing one inside the build image. Services are declared once under `definitions: services` and attached by name to each step that needs them. A step can attach up to **five** service containers. ## Defining and attaching a service ```yaml definitions: services: test-db: image: acme/integration-db:16 memory: 1536 variables: DB_PASSWORD: $TEST_DB_PASSWORD pipelines: default: - step: name: Integration tests services: - test-db script: - ./wait-for-db.sh 127.0.0.1 - ./run-integration-tests.sh ``` The service's `variables` configure the container itself (here a password taken from a secured repository variable, so it never appears in the YAML). The step's `services` list is what actually starts it; defining a service that no step lists starts nothing. ## Networking and readiness - Services **share a network adapter** with the build container and open their ports on localhost. No port mapping and no hostnames are needed: the tests connect to the database's usual port on the local interface. - The documentation recommends **`127.0.0.1` rather than `localhost`**, to avoid IPv6 resolution problems. - There is **no built-in readiness check**. The step's script starts as soon as the containers are up, which is often before the database accepts connections, so the documented pattern is a loop that retries a trivial query until it succeeds. - Port 29418 is reserved and cannot be used by a service, and service logs are not available through the REST API. ## How the step's memory is split This is where most service problems come from. A step does not get memory per container; it gets **one pool**, sized by the step's `size`: | Step `size` | Total memory for the step | Build minutes used | |---|---|---| | `1x` (default) | 4096 MB | 1x | | `2x` | 8192 MB | 2x | | `4x` | 16384 MB | 4x | | `8x` | 32768 MB | 8x | The pool is divided like this: 1. Each attached service reserves **1024 MB by default**, or whatever its `memory` setting says (as low as 128 MB on a `1x` step). 2. The **`docker` service** counts as a service and also takes 1024 MB by default. Pipes run on it, which Bitbucket starts transparently, so a step that uses a pipe pays for it even though the YAML lists no service. 3. The build container, where your `script` runs, gets **what is left**, and must keep at least 1024 MB. On a `1x` step, one database at the default and the `docker` service leave 2048 MB for the build itself. With the `memory: 1536` setting above and no `docker` service, the build container gets 2560 MB. A test runner or compiler that needs 3 GB fails with an out-of-memory error in both cases, even though "the step has 4 GB". ## Fixing an out-of-memory step - **Right-size each service.** The documentation's guidance for databases is 512 MB to 2 GB; set `memory` to what the service really needs instead of accepting the 1024 MB default. - **Drop services the step does not use.** Each one is reserved memory, and each must start before the script can do useful work. - **Raise the step to `2x`.** It doubles the pool, still shared between script and services, and it also **doubles the build minutes** the step consumes. Apply it to the one step that needs it, not with `options: size` for the whole pipeline. - **Know what larger sizes add.** Sizes of `4x` and above are only available on paid plans (Standard or Premium) and add dedicated CPUs and more disk as well as memory, at four times the minutes or more. ## Services live and die with the step A service container belongs to one step. The next step that lists the same service gets a **new, empty** container, so a schema created or test data loaded in one step is gone in the next. Seed the database in the same step that runs the tests, or keep seeding fast enough to repeat. Credentials a service needs, such as a database password, belong in secured variables referenced from the service's `variables`, never written into the YAML in plain text; the database is disposable, but a password reused elsewhere is not. Because build minutes are metered, the cheapest fix is almost always to give the service the memory it really needs rather than to double the size of every step that touches the database.

  • Why can a step that uses a pipe run out of memory when it lists no services at all?
    A pipe runs on the Docker service, which Bitbucket Pipelines starts transparently. That service counts towards the step's service limit and takes 1 GB of the step's memory by default, so the script is left with less than the full pool. If the pipe needs more, or the script does, adjust the `docker` service's memory under `definitions` or give that step a larger `size`.
  • The first integration test fails with connection refused, and a rerun passes. What is happening?
    Service containers have no built-in readiness check, so the script can start before the database accepts connections. Add a wait loop at the start of the script that retries a trivial query or health endpoint on `127.0.0.1` until it answers, instead of relying on reruns.

  • Each service container gets its own memory on top of the step's 4 GB.
  • A service needs a port mapping or a hostname before tests can reach it.
  • A service is ready to accept connections as soon as the script starts.
  • A 2x step only adds memory and uses no extra build minutes.
  • A step can attach as many service containers as its tests need.