United Kingdom: +44 (0)208 088 8978

Building a Dockerfile with F#

This week Akash builds a Dockerfile using F#! 🐋

We're hiring Software Developers

Click here to find out more

In the last few weeks both Matt and Ryan have written awesome blogs on how you can start making the most out of Docker and what that can look like if you're using the SAFE Stack.

Matt showed us how you can use commands to build and run images.
Ryan showed us how you can use docker compose to declaratively set up your environment.
Today I'll be showing you a step in between - the Dockerfile.

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image

Rather than show you a plain old Dockerfile we can put an F# spin on it thanks to another awesome library by Dave Curylo!

FSharp.Text.Docker 🐋

The library lets you "interact with docker with the type safety of the F# language". So let's take the command that Matt ran in his previous blog and see what that translates to.

Before

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=yourStrong(!)Password" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest

After

#r "nuget: FSharp.Text.Docker"
open FSharp.Text.Docker.Builders

let dockerBuilder = dockerfile {
    from "mcr.microsoft.com/mssql/server:2019-latest"
    env_vars [
        "ACCEPT_EULA", "Y"
        "SA_PASSWORD", "yourStrong(!)Password"
    ]
    expose 1433
}

System.IO.File.WriteAllText("Dockerfile", dockerBuilder.Build())

Output

FROM mcr.microsoft.com/mssql/server:2019-latest
ENV ACCEPT_EULA=Y SA_PASSWORD=yourStrong(!)Password
EXPOSE 1433

It's pretty quick and easy to get started. The dockerfile computation expression contains a set of instructions and once built returns a string which we can use to create our Dockerfile. For more examples take a look at the tests in the repo.

Closing Thoughts 💭

There is a lot potential with this library and thanks to Dave's work on Farmer it has a familiar feel.
Looking at the README, upcoming features include the ability to:

  • Build images
  • Run containers

With the addition of these two features I can see potential to integrate with the SAFE Stack as a FAKE target, building custom images and using docker-compose to orchestrate it all! 🎻