on master help sign in
Adds pulling images from docker hub
commit5ce3fb04d00a0d04351f919e52913d1faf3f3804 authorDeni Bertovic <deni@denibertovic.com> dateMon 20 Mar 2017, 11:03 (10 years ago) parent97ddb2f
4 files changed+39−11
src/Docker/Client/Api.hs+27−11 src/Docker/Client/Http.hs+5 src/Docker/Client/Internal.hs+6 src/Docker/Client/Types.hs+1
src/Docker/Client/Api.hs+27−11
@@ -21,9 +21,10 @@ module Docker.Client.Api (
2121 , buildImageFromDockerfile
2222 -- * Other
2323 , getDockerVersion
24+ , pullImage
2425 ) where
2526
26−import Control.Monad.Catch (MonadMask(..))
27+import Control.Monad.Catch (MonadMask (..))
2728import Control.Monad.IO.Class
2829import Control.Monad.Reader (ask, lift)
2930import Data.Aeson (FromJSON, eitherDecode')
@@ -31,10 +32,11 @@ import qualified Data.ByteString as BS
3132import qualified Data.ByteString.Lazy as BSL
3233import Data.Conduit (Sink)
3334import qualified Data.Conduit.Binary as Conduit
35+import qualified Data.Text as T
3436import qualified Data.Text as Text
35−import Network.HTTP.Client (responseStatus)
36−import Network.HTTP.Types (StdMethod (..))
37−import System.Exit (ExitCode(..))
37+import Network.HTTP.Client (responseStatus)
38+import Network.HTTP.Types (StdMethod (..))
39+import System.Exit (ExitCode (..))
3840
3941import Docker.Client.Http
4042import Docker.Client.Types
@@ -54,8 +56,8 @@ requestHelper' verb endpoint sink = do
5456 return $ Left $ DockerInvalidRequest endpoint
5557 Just request -> do
5658 -- JP: Do we need runResourceT?
57− -- lift $ NHS.httpSink request $ \response ->
58− lift $ httpHandler request $ \response ->
59+ -- lift $ NHS.httpSink request $ \response ->
60+ lift $ httpHandler request $ \response ->
5961 -- Check status code.
6062 let status = responseStatus response in
6163 case statusCodeToError endpoint status of
@@ -149,21 +151,25 @@ inspectContainer cid = requestHelper GET (InspectContainerEndpoint cid) >>= pars
149151-- See 'LogOpts' for options that you can pass and
150152-- 'defaultLogOpts' for sane defaults.
151153--
152−-- __NOTE__: Currently streaming logs is
153−-- not supported and this function will fetch the entire log that the
154+-- __NOTE__: his function will fetch the entire log that the
154155-- container produced in the json-file on disk. Depending on the logging
155156-- setup of the process in your container this can be a significant amount
156157-- which might block your application...so use with caution.
157158--
158−-- The recommended method is to use one of the other 'LogDriverType's available (like
159+-- If you want to stream the logs from the container continuosly then use
160+-- 'getContainerLogsStream'
161+--
162+-- __NOTE__: It's recommended to use one of the other 'LogDriverType's available (like
159163-- syslog) for creating your containers.
160164getContainerLogs :: forall m. (MonadIO m, MonadMask m) => LogOpts -> ContainerID -> DockerT m (Either DockerError BSL.ByteString)
161165getContainerLogs logopts cid = requestHelper GET (ContainerLogsEndpoint logopts False cid)
162166
167+-- | Continuosly gets the container's logs as a stream. Uses conduit.
163168getContainerLogsStream :: forall m b . (MonadIO m, MonadMask m) => LogOpts -> ContainerID -> Sink BS.ByteString m b -> DockerT m (Either DockerError b)
164−getContainerLogsStream logopts cid sink = requestHelper' GET (ContainerLogsEndpoint logopts True cid) sink
169+getContainerLogsStream logopts cid = requestHelper' GET (ContainerLogsEndpoint logopts True cid)
165170-- JP: Should the second (follow) argument be True? XXX
166171
172+-- | Build an Image from a Dockerfile
167173-- TODO: Add X-Registry-Config
168174-- TODO: Add support for remote URLs to a Dockerfile
169175-- TODO: Clean up temp tar.gz file after the image is built
@@ -171,5 +177,15 @@ buildImageFromDockerfile :: forall m. (MonadIO m, MonadMask m) => BuildOpts -> F
171177buildImageFromDockerfile opts base = do
172178 ctx <- makeBuildContext $ BuildContextRootDir base
173179 case ctx of
174− Left e -> return $ Left e
180+ Left e -> return $ Left e
175181 Right c -> requestUnit POST (BuildImageEndpoint opts c)
182+
183+-- | Pulls an image from Docker Hub (by default).
184+-- TODO: Add support for X-Registry-Auth and pulling from private docker
185+-- registries.
186+-- TODO: Implement importImage function that uses he same
187+-- CreateImageEndpoint but rather than pulling from docker hub it imports
188+-- the image from a tarball or a URL.
189+pullImage :: forall m b . (MonadIO m, MonadMask m) => T.Text -> Tag -> Sink BS.ByteString m b -> DockerT m (Either DockerError b)
190+pullImage name tag = requestHelper' POST (CreateImageEndpoint name tag Nothing)
191+
src/Docker/Client/Http.hs+5
@@ -249,4 +249,9 @@ statusCodeToError (BuildImageEndpoint _ _) st =
249249 Nothing
250250 else
251251 Just $ DockerInvalidStatusCode st
252+statusCodeToError (CreateImageEndpoint _ _ _) st =
253+ if st == status200 then
254+ Nothing
255+ else
256+ Just $ DockerInvalidStatusCode st
252257
src/Docker/Client/Internal.hs+6
@@ -70,6 +70,10 @@ getEndpoint (BuildImageEndpoint o _) = encodeURLWithQuery ["build"] query
7070 rm = encodeQ $ show $ buildRemoveItermediate o
7171 forcerm = encodeQ $ show $ buildForceRemoveIntermediate o
7272 pull = encodeQ $ show $ buildPullParent o
73+getEndpoint (CreateImageEndpoint name tag _) = encodeURLWithQuery ["images", "create"] query
74+ where query = [("fromImage", Just n), ("tag", Just t)]
75+ n = encodeQ $ T.unpack name
76+ t = encodeQ $ T.unpack tag
7377
7478getEndpointRequestBody :: Endpoint -> Maybe HTTP.RequestBody
7579getEndpointRequestBody VersionEndpoint = Nothing
@@ -86,7 +90,9 @@ getEndpointRequestBody (UnpauseContainerEndpoint _) = Nothing
8690getEndpointRequestBody (ContainerLogsEndpoint _ _ _) = Nothing
8791getEndpointRequestBody (DeleteContainerEndpoint _ _) = Nothing
8892getEndpointRequestBody (InspectContainerEndpoint _) = Nothing
93+
8994getEndpointRequestBody (BuildImageEndpoint _ fp) = Just $ requestBodySourceChunked $ CB.sourceFile fp
95+getEndpointRequestBody (CreateImageEndpoint _ _) = Nothing
9096
9197getEndpointContentType :: Endpoint -> BSC.ByteString
9298getEndpointContentType (BuildImageEndpoint _ _) = BSC.pack "application/tar"
src/Docker/Client/Types.hs+1
@@ -124,6 +124,7 @@ data Endpoint =
124124 | DeleteContainerEndpoint DeleteOpts ContainerID
125125 | InspectContainerEndpoint ContainerID
126126 | BuildImageEndpoint BuildOpts FilePath
127+ | CreateImageEndpoint T.Text Tag (Maybe T.Text) -- ^ Either pull an image from docker hub or imports an image from a tarball (or URL)
127128 deriving (Eq, Show)
128129
129130-- | We should newtype this