Skip to content

PostgresContainer - wait_for breaks when using port_bindings #89

@mchwalek

Description

@mchwalek

I noticed, that when I specify port_bindings, the wait_for isn't being set properly.

Below you can find a step by step expanation why it's happening:

  1. PostgresContainer constructor calls parent constructor https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/lib/testcontainers/postgres.rb#L34
  2. DockerContainer constructor calls add_wait_for https://github.com/testcontainers/testcontainers-ruby/blob/main/core/lib/testcontainers/docker_container.rb#L59
  3. The method parameter is nil and @exposed_ports field is present, so it sets a @wait_for that tries to connect to the bound port https://github.com/testcontainers/testcontainers-ruby/blob/main/core/lib/testcontainers/docker_container.rb#L270
  4. Going back to PostgresContainer's constructor - it does a conditional assignment to @wait_for, but it has been already set to a non-nil value, even though no wait_for option was specified https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/lib/testcontainers/postgres.rb#L39

This can be worked around by specifying the wait_for option explicitly

Testcontainers::PostgresContainer.new(
  'postgres:16',
  username: test_db_config['username'],
  password: test_db_config['password'],
  port_bindings: { '5432': test_db_config['port'] },
  wait_for: :healthcheck
)

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