Skip to content

Commit

Permalink
Make the reaper container respect DOCKER_HOST
Browse files Browse the repository at this point in the history
Problem: The reaper container currently expects a docker socket to be
available at `/var/run/docker.sock`, but this is not always the
case. Especially with rootless docker, which usually puts the socket
at `$XDG_RUNTIME_DIR/docker.sock` and then exports it as
`DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock`. In this case, the
reaper container will not be able use docker.

Solution: Check if the `DOCKER_HOST` environment variable sets the
docker socket location to something other than the default.
  • Loading branch information
Sereja313 authored and alexbiehl committed Feb 13, 2024
1 parent ea5d27b commit 72a076c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/TestContainers/Docker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ import Data.Aeson (decode')
import qualified Data.Aeson.Optics as Optics
import qualified Data.ByteString.Lazy.Char8 as LazyByteString
import Data.Function ((&))
import Data.List (find)
import Data.List (find, stripPrefix)
import Data.Maybe (fromMaybe)
import Data.String (IsString (..))
import Data.Text (Text, pack, splitOn, strip, unpack)
import Data.Text.Encoding (encodeUtf8)
Expand All @@ -200,8 +201,9 @@ import Network.HTTP.Types (statusCode)
import qualified Network.Socket as Socket
import Optics.Fold (pre)
import Optics.Operators ((^?))
import Optics.Optic ((%))
import Optics.Optic ((%), (<&>))
import System.Directory (doesFileExist)
import System.Environment (lookupEnv)
import System.IO (Handle, hClose)
import System.IO.Unsafe (unsafePerformIO)
import qualified System.Process as Process
Expand Down Expand Up @@ -603,11 +605,16 @@ run request = do
-- @since 0.5.0.0
createRyukReaper :: TestContainer Reaper
createRyukReaper = do
dockerSocketLocation <-
liftIO $
lookupEnv "DOCKER_HOST"
<&> (>>= stripPrefix "unix://")
<&> fromMaybe "/var/run/docker.sock"
ryukContainer <-
run $
containerRequest (fromTag ryukImageTag)
& skipReaper
& setVolumeMounts [("/var/run/docker.sock", "/var/run/docker.sock")]
& setVolumeMounts [(pack dockerSocketLocation, "/var/run/docker.sock")]
& setExpose [ryukPort]
& setWaitingFor (waitUntilMappedPortReachable ryukPort)
& setRm True
Expand Down

0 comments on commit 72a076c

Please sign in to comment.