Essay - Published: 2022.10.19 | docker | dotnet | fsharp |
DISCLOSURE: If you buy through affiliate links, I may earn a small commission. (disclosures)
Containers are useful for building projects that can run anywhere. F# is my favorite programming language.
In this post I'll show you how to setup an F# console application and run it via Docker container.
Docker - You'll need Docker installed to run Docker containers. More info at: Install Docker Enginedotnet installed on your machine - if you don't have it, download / install .NET SDK from MicrosoftNote: While you can run dotnet from inside a container without ever installing it on your machine, it's a bit more complicated and out of scope for this post.
First let's create a console application. We'll use the dotnet command (installed with the .NET SDK) to do this.
dotnet new console -lang f#This should create a boilerplate repo that prints to console.
We'll be using Docker to containerize our F# console application.
Here's the code and we'll walkthrough what it does after:
Dockerfile
# **Build Project**
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
EXPOSE 80
WORKDIR /source
# Copy fsproj and restore all dependencies
COPY ./*.fsproj ./
RUN dotnet restore
# Copy source code and build / publish app and libraries
COPY . .
RUN dotnet publish -c release -o /app
# **Run project**
# Create new layer with runtime, copy app / libraries, then run dotnet
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "YOUR_APP_NAME.dll"]
mcr.microsoft.com/dotnet/sdk:6.0).fsproj (how we track dependencies, kind of like a package.json in JS world) and install with dotnet restoredotnet publish.dll and is smaller than the full SDKdotnet to run our .dllNote: You'll need to replace YOUR_APP_NAME.dll with the name of your created app. Typically the output .dll will match the name of your .fsproj file.
With this we should have a fully working console app. Now let's run it to make sure it works.
To run your container, use command:
docker run --rm -it $(docker build -q .)
This command:
docker builddocker run --rmIf this works correctly, you should see a console log originating from your program.fs.
In the dotnet new boilerplate I got, this was "Hello from F#"
The best way to support my work is to like / comment / share for the algorithm and subscribe for future updates.