Skip to content

Postgres container does not return from start()Β #1003

@gbarton

Description

@gbarton

Expected Behaviour
I expect when I call start() to eventually be returned back to. I've managed to track it down to the healthcheck. If I override the default with a log based one it works.

Actual Behaviour
await hangs forever.

Testcontainer Logs

$ DEBUG=testcontainers* TESTCONTAINERS_RYUK_DISABLED=true  bun run src/lib/trying.ts | pino-pretty 
  testcontainers [DEBUG] Checking container runtime strategy "TestcontainersHostStrategy"... +0ms
[01:03:37.073] INFO (225606): log level set to debug
  testcontainers [DEBUG] Container runtime strategy "TestcontainersHostStrategy" is not applicable +6ms
  testcontainers [DEBUG] Checking container runtime strategy "ConfigurationStrategy"... +1ms
  testcontainers [DEBUG] Container runtime strategy "ConfigurationStrategy" is not applicable +1ms
  testcontainers [DEBUG] Checking container runtime strategy "UnixSocketStrategy"... +0ms
  testcontainers [TRACE] Fetching Docker info... +3ms
[01:03:37.075] INFO (225606): setting up pg container
  testcontainers [TRACE] Fetching remote container runtime socket path... +62ms
  testcontainers [TRACE] Resolving host... +1ms
  testcontainers [DEBUG] Checking gateway for Docker host... +1ms
  testcontainers [TRACE] Fetching Compose info... +10ms
  testcontainers [TRACE] Looking up host IPs... +661ms
  testcontainers [TRACE] Initialising clients... +1ms
  testcontainers [TRACE] Container runtime info:
  testcontainers {
  testcontainers   "node": {
  testcontainers     "version": "v22.6.0",
  testcontainers     "architecture": "arm64",
  testcontainers     "platform": "linux"
  testcontainers   },
  testcontainers   "containerRuntime": {
  testcontainers     "host": "172.17.0.1",
  testcontainers     "hostIps": [
  testcontainers       {
  testcontainers         "address": "172.17.0.1",
  testcontainers         "family": 4
  testcontainers       }
  testcontainers     ],
  testcontainers     "remoteSocketPath": "/var/run/docker.sock",
  testcontainers     "indexServerAddress": "https://index.docker.io/v1/",
  testcontainers     "serverVersion": "27.4.1",
  testcontainers     "operatingSystem": "Debian GNU/Linux 12 (bookworm)",
  testcontainers     "operatingSystemType": "linux",
  testcontainers     "architecture": "aarch64",
  testcontainers     "cpus": 4,
  testcontainers     "memory": 3975135232,
  testcontainers     "runtimes": [
  testcontainers       "io.containerd.runc.v2",
  testcontainers       "runc"
  testcontainers     ],
  testcontainers     "labels": []
  testcontainers   },
  testcontainers   "compose": {
  testcontainers     "version": "2.36.0",
  testcontainers     "compatability": "v2"
  testcontainers   }
  testcontainers } +1ms
  testcontainers [DEBUG] Container runtime strategy "UnixSocketStrategy" works +1ms
  testcontainers [DEBUG] Checking if image exists "postgres:17"... +3ms
  testcontainers [DEBUG] Checked if image exists "postgres:17" +8ms
  testcontainers [DEBUG] Image "postgres:17" already exists +1ms
  testcontainers [DEBUG] Acquiring lock file "/tmp/testcontainers-node.lock"... +8ms
  testcontainers [DEBUG] Acquired lock file "/tmp/testcontainers-node.lock" +12ms
  testcontainers [DEBUG] Listing containers... +2ms
  testcontainers [DEBUG] Listed containers +15ms
  testcontainers [DEBUG] Releasing lock file "/tmp/testcontainers-node.lock"... +2ms
  testcontainers [DEBUG] Released lock file "/tmp/testcontainers-node.lock" +2ms
  testcontainers [DEBUG] Creating container for image "postgres:17"... +2ms
  testcontainers [DEBUG] [042e0031550b] Created container for image "postgres:17" +164ms
  testcontainers [INFO] [042e0031550b] Starting container for image "postgres:17"... +1ms
  testcontainers [DEBUG] [042e0031550b] Starting container... +0ms
  testcontainers [DEBUG] [042e0031550b] Started container +526ms
  testcontainers [INFO] [042e0031550b] Started container for image "postgres:17" +0ms
  testcontainers [DEBUG] [042e0031550b] Fetching container logs... +15ms
  testcontainers [DEBUG] [042e0031550b] Waiting for container to be ready... +5ms
  testcontainers [DEBUG] [042e0031550b] Waiting for composite... +1ms
  testcontainers [DEBUG] [042e0031550b] Waiting for health check... +1ms
  testcontainers [DEBUG] [042e0031550b] Waiting for host port 32806... +5ms
  testcontainers [DEBUG] [042e0031550b] Waiting for internal port 5432... +10ms
  testcontainers [DEBUG] [042e0031550b] Demuxing stream... +16ms
  testcontainers [DEBUG] [042e0031550b] Demuxed stream +1ms
  testcontainers [DEBUG] [042e0031550b] Host port 32806 ready +32ms
  testcontainers [DEBUG] [042e0031550b] Host port wait strategy complete +0ms
  testcontainers:containers [042e0031550b] The files belonging to this database system will be owned by user "postgres". +0ms
  testcontainers:containers [042e0031550b] This user must also own the server process. +4ms
  testcontainers:containers [042e0031550b]  +0ms
  testcontainers:containers [042e0031550b] The database cluster will be initialized with locale "en_US.utf8". +1ms
  testcontainers:containers [042e0031550b] The default database encoding has accordingly been set to "UTF8". +1ms
  testcontainers:containers [042e0031550b] The default text search configuration will be set to "english". +0ms
  testcontainers:containers [042e0031550b]  +1ms
  testcontainers:containers [042e0031550b] Data page checksums are disabled. +1ms
  testcontainers:containers [042e0031550b]  +1ms
  testcontainers:containers [042e0031550b] fixing permissions on existing directory /var/lib/postgresql/data ... ok +0ms
  testcontainers:containers [042e0031550b] creating subdirectories ... ok +1ms
  testcontainers:containers [042e0031550b] selecting dynamic shared memory implementation ... posix +2ms
  testcontainers:containers [042e0031550b] selecting default "max_connections" ... 100 +40ms
  testcontainers:containers [042e0031550b] selecting default "shared_buffers" ... 128MB +65ms
  testcontainers:containers [042e0031550b] selecting default time zone ... Etc/UTC +43ms
  testcontainers:containers [042e0031550b] creating configuration files ... ok +2ms
  testcontainers:containers [042e0031550b] running bootstrap script ... ok +413ms
  testcontainers:containers [042e0031550b] performing post-bootstrap initialization ... ok +1s
  testcontainers:containers [042e0031550b] syncing data to disk ... ok +5s
  testcontainers:containers [042e0031550b]  +0ms
  testcontainers:containers [042e0031550b]  +1ms
  testcontainers:containers [042e0031550b] Success. You can now start the database server using: +0ms
  testcontainers:containers [042e0031550b]  +0ms
  testcontainers:containers [042e0031550b] pg_ctl -D /var/lib/postgresql/data -l logfile start +0ms
  testcontainers:containers [042e0031550b]  +1ms
  testcontainers:containers [042e0031550b] initdb: warning: enabling "trust" authentication for local connections +0ms
  testcontainers:containers [042e0031550b] initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. +0ms
  testcontainers:containers [042e0031550b] waiting for server to start....2025-05-17 01:03:45.325 UTC [147] LOG:  starting PostgreSQL 17.2 (Debian 17.2-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit +148ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:45.335 UTC [147] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +9ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:45.368 UTC [150] LOG:  database system was shut down at 2025-05-17 01:03:40 UTC +32ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:45.390 UTC [147] LOG:  database system is ready to accept connections +23ms
  testcontainers:containers [042e0031550b] done +62ms
  testcontainers:containers [042e0031550b] server started +2ms
  testcontainers:containers [042e0031550b] CREATE DATABASE +488ms
  testcontainers:containers [042e0031550b]  +6ms
  testcontainers:containers [042e0031550b]  +2ms
  testcontainers:containers [042e0031550b] /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* +1ms
  testcontainers:containers [042e0031550b]  +0ms
  testcontainers:containers [042e0031550b] waiting for server to shut down....2025-05-17 01:03:45.951 UTC [147] LOG:  received fast shutdown request +2ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:45.964 UTC [147] LOG:  aborting any active transactions +17ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:45.971 UTC [147] LOG:  background worker "logical replication launcher" (PID 153) exited with exit code 1 +5ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:45.976 UTC [148] LOG:  shutting down +1ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:45.986 UTC [148] LOG:  checkpoint starting: shutdown immediate +9ms
  testcontainers:containers [042e0031550b] .2025-05-17 01:03:47.324 UTC [148] LOG:  checkpoint complete: wrote 921 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.086 s, sync=1.206 s, total=1.348 s; sync files=301, longest=0.167 s, average=0.005 s; distance=4238 kB, estimate=4238 kB; lsn=0/1908948, redo lsn=0/1908948 +1s
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:47.344 UTC [147] LOG:  database system is shut down +17ms
  testcontainers:containers [042e0031550b] done +9ms
  testcontainers:containers [042e0031550b] server stopped +1ms
  testcontainers:containers [042e0031550b]  +2ms
  testcontainers:containers [042e0031550b] PostgreSQL init process complete; ready for start up. +0ms
  testcontainers:containers [042e0031550b]  +2ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:47.478 UTC [1] LOG:  starting PostgreSQL 17.2 (Debian 17.2-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit +120ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:47.478 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432 +0ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:47.478 UTC [1] LOG:  listening on IPv6 address "::", port 5432 +1ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:47.496 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" +16ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:47.521 UTC [198] LOG:  database system was shut down at 2025-05-17 01:03:47 UTC +25ms
  testcontainers:containers [042e0031550b] 2025-05-17 01:03:47.542 UTC [1] LOG:  database system is ready to accept connections +22ms
  testcontainers [DEBUG] [042e0031550b] Health check wait strategy complete +9s

Steps to Reproduce

  1. Using bun
  2. use this code:
import { PostgreSqlContainer } from '@testcontainers/postgresql'
import { GenericContainer, Wait } from 'testcontainers';


export async function setupDockerTestDb() {
  // @see https://github.com/testcontainers/testcontainers-node/issues/807
  // @see https://www.answeroverflow.com/m/1128519076952682517
  console.log('setting up pg container');
  try {
    // this fails
    const container = await new PostgreSqlContainer('postgres:17').start();

    // but this will succeed
    // const container = await new PostgreSqlContainer('postgres:17').withWaitStrategy(Wait.forLogMessage('ready to accept connections',2)).start();
    
    console.log(`container online: ${container.getHostname()}`);

    return { async stop() { await container.stop({timeout: 10000});} } //, db }
  } catch (err) {
    console.log(err);
    console.log('error setting up pg container, ignoring');
    return { async stop() { Logger.info('nothing to stop')}}
  }
}

const { stop } = await setupDockerTestDb();

process.on("SIGINT", async () => {
  await stop();
});

process.on("SIGKILL", async () => {
  await stop();
});
  1. Run command DEBUG=testcontainers* TESTCONTAINERS_RYUK_DISABLED=true bun run src/lib/trying.ts or wherever you put it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions