Run F# / .NET in Docker (Console App)
Date: 2022-10-19 | fsharp | dotnet | docker |
Overview
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.
Pre-requisites
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 Microsoft
Note: 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.
Create a console application
First let's create a console application. We'll use the dotnet
command (installed with the .NET SDK) to do this.
- Create a repo
- Run
dotnet new console -lang f#
This should create a boilerplate repo that prints to console.
Dockerize F#
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"]
- Build Project - In the first section we are building / packaging our code to be run
- We download the .NET SDK to build our code (
mcr.microsoft.com/dotnet/sdk:6.0
) - We copy our
.fsproj
(how we track dependencies, kind of like apackage.json
in JS world) and install withdotnet restore
- We then create a runnable code package using
dotnet publish
- We download the .NET SDK to build our code (
- Run Project - In this section, we create a new container layer just for running code - we do this to get a more space efficient output image
- Download ASP.NET which is needed to run our
.dll
and is smaller than the full SDK - Copy the packaged code we built earlier
- Use
dotnet
to run our.dll
- Download ASP.NET which is needed to run our
Note: 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.
Running your F# Docker container
To run your container, use command:
docker run --rm -it $(docker build -q .)
This command:
- Builds the container in this folder with
docker build
- Runs the container (and cleans up any existing container) with
docker run --rm
If 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#"
Next Steps
- The full source code is available in this GitHub repo: fsharp-docker-console-example
- Full Stack F# project boilerplate: CloudSeed
Want more like this?
The best / easiest way to support my work is by subscribing for future updates and sharing with your network.