testcontainers

Docker containers for your integration tests.

Stackage Nightly 2026-07-23:0.5.4.0
Latest on Hackage:0.5.4.0

See all snapshots testcontainers appears in

MIT licensed by Alex Biehl
Maintained by [email protected]
This version can be pinned in stack with:testcontainers-0.5.4.0@sha256:21e544a274eb2b8ff6a4522571711c605637e51d320f4ce4f1c12cc5d3100280,2535

About

Testcontainers is a Haskell library that provides a friendly API to run Docker containers. It is designed to create a runtime environment to use during your integration tests

Example

{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
module Main where

import qualified Test.Tasty           as Tasty
import qualified Test.Tasty.HUnit     as Tasty
import qualified TestContainers.Tasty as TC


data Endpoints = Endpoints
  {
    redisHost :: String
  , redisPort :: Int
  }


-- | Sets up and runs the containers required for this test suite.
setupContainers :: TC.MonadDocker m => m Endpoints
setupContainers = do

  -- Launch the container based on the redis:5.0.0 image.
  redisContainer <- TC.run $ TC.containerRequest (TC.fromTag "redis:5.0.0")
    -- Expose the port 6379 from within the container. The respective port
    -- on the host machine can be looked up using `containerPort` (see below).
    TC.& TC.setExpose [ 6379 ]
    -- Wait until the container is ready to accept requests. `run` blocks until
    -- readiness can be established.
    TC.& TC.setWaitingFor (TC.waitUntilMappedPortReachable 6379)

  pure $ Endpoints
    {
      redisHost = "0.0.0.0"
    , redisPort =
        -- Look up the corresponding port on the host machine for the exposed
        -- port 6379.
        TC.containerPort redisContainer 6379
    }


main :: IO ()
main =
  Tasty.defaultMain $
  -- Use `withContainers` to make the containers available in the closed over
  -- tests. Due to how Tasty handles resources `withContainers` passes down
  -- an IO action `start` to actually start up the containers. `start` can be
  -- invoked multiple times, Tasty makes sure to only start up the containrs
  -- once.
  --
  -- `withContainers` ensures the started containers are shut down correctly
  -- once execution leaves its scope.
  TC.withContainers setupContainers $ \start ->
    Tasty.testGroup "Some test group"
      [
        Tasty.testCase "Redis test" $ do
          -- Actually start the containers!!
          Endpoints {..} <- start
          -- ... assert some properties
          pure ()

      , Tasty.testCase "Another Redis test" $ do
          -- Invoking `start` twice gives the same Endpoints!
          Endpoints {..} <- start
          -- ... assert some properties
          pure ()
      ]

Changes

Revision history for testcontainer-hs

0.5.4.0 – 2026-07-06

0.5.3.0 – 2026-03-31

0.5.2.0 – 2025-08-28

0.5.1.0 – 2025-01-14

  • Introduce withWorkingDirectory to set the working directory inside a container (@alexbiehl, https://github.com/testcontainers/testcontainers-hs/pull/37)

  • Add fromExistingNetwork to allow connecting containers to existing, unmanaged networks (@alexbiehl, https://github.com/testcontainers/testcontainers-hs/pull/41)

  • Add setMemory to limit the memory of a Docker container;

  • Add setCpus to limit the CPUs limit of a docker container;

  • Add fromImageId to get an Image from an image id;

0.5.0.0 – 2023-02-20

  • BREAKING: Refined lifecycle management. testcontainers is now using testcontainers/ryuk resource reaper to cleanup containers, networks and volumes. Release keys for containers are deprecated. (@alexbiehl, https://github.com/testcontainers/testcontainers-hs/pull/33)

  • BREAKING: Ports can be passed as both numbers as well as strings ala “80/tcp” or “9423/udp” (@alexbiehl, https://github.com/testcontainers/testcontainers-hs/pull/31)

  • BREAKING: Introduce TestContainer monad (@alexbiehl, https://github.com/testcontainers/testcontainers-hs/pull/29)

  • Control container naming (@blackheaven, https://github.com/testcontainers/testcontainers-hs/pull/18)

  • Ability to use Docker networks (@alexbiehl, https://github.com/testcontainers/testcontainers-hs/pull/32)

  • Better handling of running testcontainers from within Docker (@michivi, https://github.com/testcontainers/testcontainers-hs/pull/22)

  • Ability to wait for particular HTTP routes to become available with waitforHttp (@michivi, https://github.com/testcontainers/testcontainers-hs/pull/24)

  • Some internal module reorganization (@alexbiehl, https://github.com/testcontainers/testcontainers-hs/pull/32)

  • Refined timeout handling for WaitUntilReady (@alexbiehl, https://github.com/testcontainers/testcontainers-hs/pull/30)

  • Moved repository under the testcontainers organization

0.2.0.0 – 2020-08-07

  • Dependency rework (@blackheaven, https://github.com/alexbiehl/testcontainers-hs/pull/3)

0.1.0.0 – 2020-08-06

  • First version. Released on an unsuspecting world.