@@ -76,6 +76,7 @@ module Docker.Client.Types (
76 , ExposedPorts(..)
76 , ExposedPorts(..)
77 , DeviceWeight(..)
77 , DeviceWeight(..)
78 , DeviceRate(..)
78 , DeviceRate(..)
81import Data.Aeson (FromJSON, ToJSON, genericParseJSON,
82import Data.Aeson (FromJSON, ToJSON, genericParseJSON,
@@ -830,6 +831,11 @@ instance FromJSON PortType where
830newtype PortBindings = PortBindings [PortBinding]
831newtype PortBindings = PortBindings [PortBinding]
831 deriving (Eq, Show)
832 deriving (Eq, Show)
835+instance Monoid PortBindings where
836+ mempty = PortBindings []
837+ mappend (PortBindings p1) (PortBindings p2) = PortBindings (p1 ++ p2)
833-- { <port>/<protocol>: [{ "HostPort": "<port>" }] }
839-- { <port>/<protocol>: [{ "HostPort": "<port>" }] }
834data PortBinding = PortBinding {
840data PortBinding = PortBinding {
835 containerPort :: Port
841 containerPort :: Port
@@ -837,10 +843,20 @@ data PortBinding = PortBinding {
837 , hostPorts :: [HostPort]
843 , hostPorts :: [HostPort]
838 } deriving (Eq, Show)
844 } deriving (Eq, Show)
841portAndType2Text :: Port -> PortType -> Text
846portAndType2Text :: Port -> PortType -> Text
842portAndType2Text p t = (T.pack $ show p) <> "/" <> (T.pack $ show t)
847portAndType2Text p t = (T.pack $ show p) <> "/" <> (T.pack $ show t)
849+-- | A convenience function that adds PortBindings to and exiting
850+-- "CreateOpts" record. Useful with 'defaultCreateOpts'
853+-- >>> let pb = PortBinding 80 TCP [HostPort "0.0.0.0" 8000]
854+-- >>> addPortBinding (defaultCreateOpts "nginx:latest") pb
855+addPortBinding :: CreateOpts -> PortBinding -> CreateOpts
856+addPortBinding c pb = c{hostConfig=hc{portBindings=pbs <> PortBindings [pb]}}
857+ where hc = hostConfig c
858+ pbs = portBindings $ hostConfig c
844instance ToJSON PortBinding where
860instance ToJSON PortBinding where
845 toJSON (PortBinding {..}) = object [portAndType2Text containerPort portType .= hostPorts]
861 toJSON (PortBinding {..}) = object [portAndType2Text containerPort portType .= hostPorts]