Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use host if there is no alias #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/TestContainers/Docker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

Expand Down Expand Up @@ -178,6 +176,7 @@ import qualified Data.Aeson.Optics as Optics
import qualified Data.ByteString.Lazy.Char8 as LazyByteString
import Data.Function ((&))
import Data.List (find)
import Data.Maybe (fromMaybe)
import Data.String (IsString (..))
import Data.Text (Text, pack, splitOn, strip, unpack)
import Data.Text.Encoding (encodeUtf8)
Expand Down Expand Up @@ -1112,24 +1111,17 @@ internalContainerIp Container {id, inspectOutput} =
-- Takes the first alias found.
--
-- @since 0.5.0.0
containerAlias :: Container -> Text
containerAlias :: Container -> Maybe Text
containerAlias Container {id, inspectOutput} =
case inspectOutput
inspectOutput
^? pre
( Optics.key "NetworkSettings"
% Optics.key "Networks"
% Optics.members
% Optics.key "Aliases"
% Optics.values
% Optics._String
) of
Nothing ->
throw $
InspectOutputMissingNetwork
{ id
}
Just alias ->
alias
)

-- | Get the IP address for the container's gateway, i.e. the host.
-- Takes the first gateway address found.
Expand Down Expand Up @@ -1192,9 +1184,12 @@ containerPort Container {id, inspectOutput} Port {port, protocol} =
containerAddress :: Container -> Port -> (Text, Int)
containerAddress container Port {port, protocol} =
let inDocker = unsafePerformIO isRunningInDocker
in if inDocker
then (containerAlias container, port)
else ("localhost", containerPort container (Port {port, protocol}))
mappedPort = containerPort container (Port {port, protocol})
mAlias = containerAlias container
in case (inDocker, mAlias) of
(True, Just alias) -> (alias, port)
(True, Nothing) -> ("172.17.0.1", mappedPort) -- FIXME Assume default host for now
(False, _) -> ("localhost", mappedPort)

-- | Runs the `docker inspect` command. Memoizes the result.
--
Expand Down