testcontainers

Version, currently 0.1.01 version
  • 0.1.0latestMar 7, 2026

github.com/dragosv/testcontainers-crystal

Testcontainers for Crystal — a library that provides lightweight, throwaway Docker containers for integration testing.

0 stars
0 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  testcontainers:
    github: dragosv/testcontainers-crystal
    version: ~> 0.1.0

Then run:

shards install

shard.yml

Crystal
>= 1.10.0
License
MIT
Author
Testcontainers Crystal Contributors

Dependencies

Runtime Dependencies

  • docr~> 0.1.4github: marghidanu/docr
  • http_client*github: jgaskins/http_client

Development Dependencies

  • ameba~> 1.6.4github: crystal-ameba/amebadev

README

Crystal Docker License: MIT PRs Welcome

Testcontainers for Crystal

A lightweight Crystal library for writing tests with throwaway Docker containers, inspired by testcontainers-ruby. Uses docr for Docker Engine API communication.

Requirements

  • Crystal >= 1.10.0
  • Docker or Docker Desktop running

Installation

Add to your shard.yml:

dependencies:
  testcontainers:
    github: dragosv/testcontainers-crystal
    version: "~> 0.1.0"

Then run:

shards install

Quick Start

See QUICKSTART.md for a step-by-step guide.

require "testcontainers"

container = Testcontainers::DockerContainer.new("nginx:latest")
  .with_exposed_port(80)
  .with_wait_for(:http, port: 80)
  .start

port = container.mapped_port(80)
puts "Nginx running at http://localhost:#{port}"

container.stop
container.remove

Modules

Pre-configured containers for common services — each exposes a connection_url convenience method:

ModuleImageDefault Port
PostgresContainerpostgres5432
MySQLContainermysql3306
MariaDBContainermariadb3306
RedisContainerredis6379
MongoContainermongo27017
NginxContainernginx80
RabbitMQContainerrabbitmq5672 / 15672
ElasticsearchContainerelasticsearch9200
pg = Testcontainers::PostgresContainer.new
  .with_database("testdb")
  .start

url = pg.connection_url

Wait Strategies

# Wait for log message matching a regex
.with_wait_for(:logs, message: /ready to accept connections/)

# Wait for a TCP port to be reachable
.with_wait_for(:tcp, port: 5432)

# Wait for an HTTP endpoint to return 200
.with_wait_for(:http, port: 8080, path: "/health")

# Wait for Docker HEALTHCHECK to report healthy
.with_wait_for(:healthcheck)

Crystal Spec Integration

require "spec"
require "testcontainers"

describe "Database" do
  it "connects to PostgreSQL" do
    pg = Testcontainers::PostgresContainer.new
      .with_database("testdb")
      .start

    begin
      url = pg.connection_url
      url.should contain("postgres://")
    ensure
      pg.stop
      pg.remove
    end
  end
end

Networking

Testcontainers::Network.create("app-net") do |network|
  db = Testcontainers::PostgresContainer.new
    .with_network(network)
    .with_network_alias("database")
    .start

  app = Testcontainers::DockerContainer.new("myapp:latest")
    .with_network(network)
    .with_env("DB_HOST", "database")
    .start

  # app can reach db at hostname "database"

  app.stop; app.remove
  db.stop; db.remove
end

Documentation

DocumentDescription
QUICKSTART.md5-minute getting-started guide
ARCHITECTURE.mdDesign decisions and component overview
IMPLEMENTATION_GUIDE.mdDetailed implementation guide
PROJECT_SUMMARY.mdFull feature inventory and stats
CONTRIBUTING.mdHow to contribute

Contributing

Contributions are welcome — please read CONTRIBUTING.md first.

Acknowledgments

License

MIT — see LICENSE.

Support

GitHub Issues · Testcontainers Slack