language-docker

Dockerfile parser, pretty-printer and embedded DSL

https://github.com/hadolint/language-docker#readme

Version on this page:8.0.2
LTS Haskell 22.39:12.1.0
Stackage Nightly 2024-10-31:13.0.0
Latest on Hackage:13.0.0

See all snapshots language-docker appears in

GPL-3.0-only licensed by Lukas Martinelli, Pedro Tacla Yamada, José Lorenzo Rodríguez
Maintained by [email protected]
This version can be pinned in stack with:language-docker-8.0.2@sha256:3d3b5adebe576d8d5c72619117481513a3d89adf66850c6628ed6d0f2991971e,2778

Build Status Hackage GPL-3 licensed

haskell-language-docker

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

import Language.Docker
main = do
    ef <- parseFile "./Dockerfile"
    print ef

Parsing strings

import Language.Docker
main = do
    c <- readFile "./Dockerfile"
    print (parseString c)

Pretty-printing files

import Language.Docker
main = do
    Right d <- parseFile "./Dockerfile"
    putStr (prettyPrint d)

Writing Dockerfiles in Haskell

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLists #-}
import Language.Docker

main = putDockerfileStr $ do
    from "node"
    run "apt-get update"
    run ["apt-get", "install", "something"]
    -- ...

Using the QuasiQuoter

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
import Language.Docker
main = putDockerfileStr $ do
    from "node"
    run "apt-get update"
    [edockerfile|
    RUN apt-get update
    CMD node something.js
    |]
    -- ...

Templating Dockerfiles in Haskell

{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLists #-}
import Control.Monad
import Language.Docker
import Data.String (fromString)
import qualified Data.Text.Lazy.IO as L

tags = ["7.8", "7.10", "8"]
cabalSandboxBuild packageName = do
    let cabalFile = packageName ++ ".cabal"
    run "cabal sandbox init"
    run "cabal update"
    add [fromString cabalFile] (fromString $ "/app/" ++ cabalFile)
    run "cabal install --only-dep -j"
    add "." "/app/"
    run "cabal build"
main =
    forM_ tags $ \tag -> do
        let df = toDockerfileText $ do
            from ("haskell" `tagged` tag)
            cabalSandboxBuild "mypackage"
        L.writeFile ("./examples/templating-" ++ tag ++ ".dockerfile") df

Using IO in the DSL

By default the DSL runs in the Identity monad. By running in IO we can support more features like file globbing:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedLists #-}
import           Language.Docker
import qualified System.Directory     as Directory
import qualified System.FilePath      as FilePath
import qualified System.FilePath.Glob as Glob
import Data.List.NonEmpty (fromList)
import qualified Data.Text.Lazy.IO    as L

main = do
    str <- toDockerfileTextIO $ do
        fs <- liftIO $ do
            cwd <- Directory.getCurrentDirectory
            fs <- Glob.glob "./test/*.hs"
	    let relativeFiles = map (FilePath.makeRelative cwd) fs
            return (fromList relativeFiles)
        from "ubuntu"
	copy $ (toSources fs) `to` "/app/"
    L.putStr str