• Stars
    star
    111
  • Rank 314,510 (Top 7 %)
  • Language Makefile
  • License
    BSD 3-Clause "New...
  • Created over 5 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Demonstrating how GHC, Alpine, Stack, and Docker can be used together

GHC, Alpine, Stack, and Docker

Table of Contents

This repository is a small demonstration of the steps required to build an Alpine Linux Docker image with all the tools necessary to compile Haskell programs linked against musl libc.

Such an environment are extremely useful for producing small Docker images for the deployment of services and for creating portable Haskell executables (either statically linked, or bundled with the musl linker and their dynamic dependencies).

At the time of writing, this repository is primarily geared towards showing off a workflow by which one can use stack's Docker integration to seamlessly develop inside an Alpine Linux container. This gives the benefit of stack's build caching and also integrates nicely with development tools like ghcid.

In the future I hope to either update this repository with explicit instructions and/or examples that show how one can create minimal (i.e. tens of MB) Docker images to deploy Haskell applications in production.

Prerequisites

Ensure Docker is installed on your computer and the Docker daemon is running.

Install the Haskell Tool Stack.

Building the Docker Images

At the time of writing (1 Apr. 2019) GHC-HQ doesn't provide a version of GHC that is compatible with musl libc, and the ghc package provided by Alpine is out of date.

This means that the first step in this process is going to be compiling GHC itself and packaging up all the necessary tools and dependencies in an Alpine Linux container.

For convenience, I've combined all of these steps into a single, multi-stage Dockerfile in this repository, and written a Makefile that caches the intermediate build layers so changes to the final environment don't trigger a full rebuild of GHC and the associated tooling.

"Quick" Start

To build the Docker images, navigate to the project root directory and run:

make docker-build-gmp

Keep in mind that this stage compiles GHC, which can take anywhere from 30 mins to upwards of an hour depending on how fast your computer is.

Overview

I've tried to keep the Dockerfile relatively well commented, but the build process can be roughly understood as an impl

  • base

    • Base layer used for all the intermediate build images to follow
    • Contains all OS dependencies required to run ghcup and stack
  • build-ghc

    • Builds GHC via ghcup
    • Contains all dependencies required to build GHC
    • Contains some (currently unused) logic for selecting between integer-gmp and integer-simple
      • integer-gmp depends on libgmp, which is licensed under the LGPL, so if one wants to distribute a statically linked, closed source binary they will need to build against a version of GHC that uses integer-simple
  • build-tooling

    • Downloads stack and verifies the hash
  • alpine-haskell

    • Assembles artifacts from the previous layers
    • Copies GHC and stack
    • Installs bash and shadow
      • stack requires usermod/groupmod from shadow as well as bash to run

Developing Locally with stack

Once the images have finished building, this project can be compiled to demonstrate what local development looks like with stack.

For convenience, I've added some targets to the Makefile that run some common development tasks.

First, ensure that ghcid is installed (for example, by running stack build --copy-compiler-tool ghcid).

Then, compile this project in Docker by running make build-fast.

Finally, start a ghcid development loop with make ghcid; this loop uses stack --docker to spawn a container in which GHC recompiles everything whenever it detects changes in your code.

Try changing something in executables/Main.hs and see the changes reflected in ghcid!

TODO

  • Demonstrate integer-simple support
  • Demonstrate static linking
  • Create separate Dockerfiles for GHC and combined tooling
    • Automate the creation of these images in CI and host them on Dockerhub

Related work

More Repositories

1

easy-hls-nix

Easy Haskell Language Server tooling with Nix!
Nix
54
star
2

dotnix

Shackled within an elaborate prison of my own design.
Nix
39
star
3

servant-persistent-realworld

Overengineered attempt at implementing the RealWorld API spec in Haskell using Servant and Persistent
Haskell
16
star
4

purescript-validation-experiment

Experiment with, and provide examples for, PureScript's validation library
PureScript
14
star
5

termina

wandering aimlessly throughout a vast wilderness of calamitous intent
Nix
13
star
6

scale-by-the-bay-2018

Haskell
13
star
7

composite-realworld

Overengineered attempt at implementing the RealWorld API spec in Haskell using Servant and Composite
Haskell
8
star
8

pauvre

An example Servant application that builds into a minimal Docker image
Haskell
5
star
9

conscient

An example of "code-first" API specification derivation using Haskell and Servant
Haskell
5
star
10

example-cabal-project

A simple example project using cabal-install, Nix, and direnv
Haskell
4
star
11

viaduct

Haskell
3
star
12

purescript-validationtown

Let's validate some stuff with Variant, simple-json, and Semigroups
PureScript
3
star
13

lc2018

Input Validation in PureScript
PureScript
2
star
14

semantic-stack-yaml

2
star
15

mmmtl

Mistakenly Mocking MTL
Haskell
2
star
16

optics-by-example

Working through Chris Penner's "Optics by Example"
Haskell
2
star
17

purescript-lambda-demo

Test out calling Lambda functions directly from PureScript
PureScript
2
star
18

extensible-validation

PureScript
2
star
19

hodor

Overengineering some simple API calls
Haskell
2
star
20

transforming-transformers

Haskell
1
star
21

bazel_haskell_nix_sandbox

Python
1
star
22

dry-it-up

Demonstration of sharing common dependency/extension/options information in a Haskell project
Haskell
1
star
23

levity-polymorphism-berlinfp-2020

Nix
1
star
24

purescript-mqttjs

Incomplete bindings to the MQTT.js library.
PureScript
1
star