on master help sign in
Adds example with data containers
commit5bd2a89c07a4833e2dc1167c1bc86726a55e6768 authorDeni Bertovic <deni@denibertovic.com> dateFri 2 Sep 2016, 00:45 (10 years ago) parent8466f04
1 file changed+24−1
examples/Example.hs+24−1
examples/Example.hs+24−1
@@ -15,7 +15,7 @@ stopNginxContainer :: ContainerID -> IO ()
1515stopNginxContainer cid = runDockerT (defaultClientOpts, defaultHttpHandler) $ do
1616 r <- stopContainer DefaultTimeout cid
1717 case r of
18− Left err -> error "I failed to stop the container"
18+ Left _ -> error "I failed to stop the container"
1919 Right _ -> return ()
2020
2121runPostgresContainer :: IO ContainerID
@@ -30,3 +30,26 @@ runPostgresContainer = runDockerT (defaultClientOpts, defaultHttpHandler) $ do
3030 _ <- startContainer defaultStartOpts i
3131 return i
3232
33+-- | Example of how we can start a database container while hosting it's
34+-- data in a data volume living inside another container.
35+runPostgresWithDataContainer :: IO ContainerID
36+runPostgresWithDataContainer = runDockerT (defaultClientOpts, defaultHttpHandler) $ do
37+ -- We use a dummy command like /bin/true because we don't want to start
38+ -- the postgres database in the data container we just want it to
39+ -- create a container filesystem for us.
40+ let dataOpts = setCmd "/bin/true" $ defaultCreateOpts "postgres:9.5"
41+ cid <- createContainer dataOpts (Just "myDataContainer")
42+ _ <- case cid of
43+ Left err -> error $ show err
44+ Right i -> startContainer defaultStartOpts i
45+ let pb = PortBinding 5432 TCP [HostPort "0.0.0.0" 5432]
46+ -- Default permission is read-write if we don't specify anything
47+ let vf = VolumeFrom "myDataContainer" Nothing
48+ let myCreateOpts = addVolumeFrom vf $ addPortBinding pb $ defaultCreateOpts "postgres:9.5"
49+ ccid <- createContainer myCreateOpts (Just "pgContainer")
50+ case ccid of
51+ Left err -> error $ show err
52+ Right i -> do
53+ _ <- startContainer defaultStartOpts i
54+ return i
55+