Dockerfile parser, pretty-printer and embedded DSL
Provides de ability to parse docker files, a pretty-printer and EDSL for writting Dockerfiles in Haskell.
- Parsing files
- Parsing strings
- Pretty-printing files
- Writing Dockerfiles in Haskell
- Using the QuasiQuoter
- Templating Dockerfiles in Haskell
- Using IO in the DSL
import Language.Docker
main = do
ef <- parseFile "./Dockerfile"
print ef
import Language.Docker
main = do
c <- readFile "./Dockerfile"
print (parseString c)
import Language.Docker
main = do
Right d <- parseFile "./Dockerfile"
putStr (prettyPrint d)
{-# LANGUAGE OverloadedStrings #-}
import Language.Docker
main = putStr $ toDockerfileStr $ do
from "node"
run "apt-get update"
runArgs ["apt-get", "install", "something"]
-- ...
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
import Language.Docker
main = putStr $ toDockerfileStr $ do
from "node"
run "apt-get update"
[edockerfile|
RUN apt-get update
CMD node something.js
|]
-- ...
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import Language.Docker
tags = ["7.8", "7.10", "8"]
cabalSandboxBuild packageName = do
let cabalFile = packageName ++ ".cabal"
run "cabal sandbox init"
run "cabal update"
add cabalFile ("/app/" ++ cabalFile)
run "cabal install --only-dep -j"
add "." "/app/"
run "cabal build"
main =
forM_ tags $ \tag -> do
let df = toDockerfileStr $ do
from ("haskell" `tagged` tag)
cabalSandboxBuild "mypackage"
writeFile ("./examples/templating-" ++ tag ++ ".dockerfile") df
By default the DSL runs in the Identity
monad. By running in IO we can
support more features like file globbing:
{-# LANGUAGE OverloadedStrings #-}
import Language.Docker
import qualified System.Directory as Directory
import qualified System.FilePath as FilePath
import qualified System.FilePath.Glob as Glob
main = do
str <- toDockerfileStrIO $ do
fs <- liftIO $ do
cwd <- Directory.getCurrentDirectory
fs <- Glob.glob "./test/*.hs"
return (map (FilePath.makeRelative cwd) fs)
from "ubuntu"
mapM_ (\f -> add f ("/app/" ++ FilePath.takeFileName f)) fs
putStr str
GPLv3