on master help sign in
Adds basic getting started docs

Also adds a convenince function for adding port bindings more easily. In the next release we should probably add lenses for deply nested structures like this.

commit6b13d4469dd2bffa4e8884c122358c8522d5757e authorDeni Bertovic <deni@denibertovic.com> dateSat 13 Aug 2016, 22:50 (10 years ago) parentea64241
2 files changed+76−2
src/Docker/Client.hs+59−1 src/Docker/Client/Types.hs+17−1
src/Docker/Client.hs+59−1
@@ -1,13 +1,71 @@
11{-# LANGUAGE ExplicitForAll #-}
22{-# LANGUAGE ScopedTypeVariables #-}
33
4+{-|
5+Module : Docker.Client
6+Copyright : (c) Deni Bertovic, 2016
7+License : BSD3
8+Maintainer : deni@denibertovic.com
9+Stability : experimental
10+Portability : POSIX
11+
12+Getting started
13+
14+* Creating a container:
15+
16+We're going to create a nginx container and we're going to map port @80@ from within the container to port @8000@ on
17+the host.
18+
19+@
20+import Docker.Client
21+
22+runNginxContainer :: IO ContainerID
23+runNginxContainer = runDockerT (defaultClientOpts, defaultHttpHandler) $ do
24+ let pb = PortBinding 80 TCP [HostPort "0.0.0.0" 8000]
25+ let myCreateOpts = addPortBinding (defaultCreateOpts "nginx:latest") pb
26+ cid <- createContainer myCreateOpts
27+ case cid of
28+ Left err -> error $ show err
29+ Right i -> do
30+ _ <- startContainer defaultStartOpts i
31+ return i
32+@
33+
34+Let's start out nginx container:
35+
36+>>> cid <- runNginxContainer
37+
38+
39+Visit http://localhost:8000 and verify that nginx is running.
40+
41+Let's stop the container now:
42+
43+@
44+stopNginxContainer :: ContainerID -> IO ()
45+stopNginxContainer cid = runDockerT (defaultClientOpts, defaultHttpHandler) $ do
46+ r <- stopContainer DefaultTimeout cid
47+ case r of
48+ Left err -> error "I failed to stop the container"
49+ Right _ -> return ()
50+@
51+
52+>>> stopNginxContainer cid
53+
54+* Get Docker API Version:
55+
56+>>> runDockerT (defaultClientOpts, defaultHttpHandler) $ getDockerVersion
57+Right (DockerVersion {version = "1.12.0", apiVersion = "1.24", gitCommit = "8eab29e", goVersion = "go1.6.3", os = "linux", arch = "amd64", kernelVersion = "4.6.0-1-amd64", buildTime = "2016-07-28T21:46:40.664812891+00:00"})
58+
59+-}
460module Docker.Client (
61+ -- * Client functions
562 module Docker.Client.Api
63+ -- * Types
664 , module Docker.Client.Types
65+ -- * Http
766 , module Docker.Client.Http
867 ) where
968
1069import Docker.Client.Api
1170import Docker.Client.Http
1271import Docker.Client.Types
13−
src/Docker/Client/Types.hs+17−1
@@ -76,6 +76,7 @@ module Docker.Client.Types (
7676 , ExposedPorts(..)
7777 , DeviceWeight(..)
7878 , DeviceRate(..)
79+ , addPortBinding
7980 ) where
8081
8182import Data.Aeson (FromJSON, ToJSON, genericParseJSON,
@@ -830,6 +831,11 @@ instance FromJSON PortType where
830831newtype PortBindings = PortBindings [PortBinding]
831832 deriving (Eq, Show)
832833
834+
835+instance Monoid PortBindings where
836+ mempty = PortBindings []
837+ mappend (PortBindings p1) (PortBindings p2) = PortBindings (p1 ++ p2)
838+
833839-- { <port>/<protocol>: [{ "HostPort": "<port>" }] }
834840data PortBinding = PortBinding {
835841 containerPort :: Port
@@ -837,10 +843,20 @@ data PortBinding = PortBinding {
837843 , hostPorts :: [HostPort]
838844 } deriving (Eq, Show)
839845
840−
841846portAndType2Text :: Port -> PortType -> Text
842847portAndType2Text p t = (T.pack $ show p) <> "/" <> (T.pack $ show t)
843848
849+-- | A convenience function that adds PortBindings to and exiting
850+-- "CreateOpts" record. Useful with 'defaultCreateOpts'
851+-- Example:
852+--
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
859+
844860instance ToJSON PortBinding where
845861 toJSON (PortBinding {..}) = object [portAndType2Text containerPort portType .= hostPorts]
846862