Running Starcounter 3.0 applications under Docker
With Starcounter 3.0 it is possible to run Starcounter applications in Linux Docker containers.

Sample Starcounter 3.0 Docker configuration

Sample files structure:
1
πŸ“ App
2
|--πŸ“‘ App.csproj
3
|--πŸ“‘ nuget.config
4
|--πŸ“‘ Program.cs
5
πŸ“‘ Dockerfile
Copied!
  • The App folder contains a sample Starcounter Console application files.
  • The Dockerfile file contains Docker container definition for the app.

App.csproj

1
<Project Sdk="Microsoft.NET.Sdk">
2
​
3
<PropertyGroup>
4
<OutputType>Exe</OutputType>
5
<TargetFramework>netcoreapp3.0</TargetFramework>
6
</PropertyGroup>
7
​
8
<ItemGroup>
9
<PackageReference Include="Starcounter.Database" Version="3.0.0-*" />
10
</ItemGroup>
11
</Project>
Copied!

nuget.config

1
<?xml version="1.0" encoding="utf-8"?>
2
<configuration>
3
<packageSources>
4
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
5
<clear />
6
<add key="local" value="../artifacts" />
7
<add
8
key="Starcounter"
9
value="https://www.myget.org/F/starcounter/api/v3/index.json"
10
/>
11
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
12
</packageSources>
13
</configuration>
Copied!

Program.cs

1
using System.Linq;
2
using Microsoft.Extensions.DependencyInjection;
3
using System;
4
using Starcounter.Database;
5
​
6
namespace App
7
{
8
/// <summary>
9
/// Here is our database table. It contains people. With names.
10
/// </summary>
11
[Database]
12
public abstract class Person
13
{
14
public abstract string Name { get; set; }
15
}
16
​
17
/// <summary>
18
/// This simple console application demonstrates how to build a service provider
19
/// for the Starcounter services, to fetch service instances from it,
20
/// and then how to use the services to make basic database transactions
21
/// with basic database interactions like SQL queries and inserts.
22
/// </summary>
23
public class Program
24
{
25
public static void Main(string[] args)
26
{
27
string name = args.FirstOrDefault() ?? "Noname";
28
string connectionString = "Database=./.database/ConsoleApp;"
29
+ "OpenMode=CreateIfNotExists;"
30
+ "StartMode=StartIfNotRunning;"
31
+ "StopMode=IfWeStarted";
32
​
33
// Here we create a service collection
34
// that we add the Starcounter services to.
35
// When we call BuildServiceProvider(),
36
// we get an instance that we can use to
37
// fetch service instances, for example ITransactor,
38
// which we then can use to to make database transactions.
39
using var services = new ServiceCollection()
40
.AddStarcounter(connectionString)
41
.BuildServiceProvider();
42
​
43
// Here we fetch our ITransactor instance from the service provider.
44
var transactor = services.GetRequiredService<ITransactor>();
45
​
46
// And here we use it to make a database transaction.
47
ulong oid = transactor.Transact(db =>
48
{
49
// Here inside the transaction,
50
// we can use the IDatabaseContext instance to
51
// interact with the Starcounter database.
52
​
53
// We can query it using SQL
54
// (which returns an IEnumerable<Person>
55
// that we can use with LINQ).
56
var p = db.Sql<Person>
57
(
58
"SELECT p FROM App.Person p WHERE Name = ?",
59
name
60
).FirstOrDefault();
61
​
62
if (p == null)
63
{
64
// We can insert new rows in the database using Insert().
65
p = db.Insert<Person>();
66
​
67
// We write to the database row using
68
// standard C# property accessors.
69
p.Name = name;
70
}
71
​
72
// Let's return the object id as result of the transaction.
73
return db.GetOid(p);
74
});
75
​
76
// And let's print it in the console.
77
Console.WriteLine(quot;{oid}: {name}");
78
}
79
}
80
}
Copied!

Dockerfile

1
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-bionic AS build
2
​
3
WORKDIR /source
4
​
5
# Download & Unpack latest Starcounter 3.0.0
6
RUN apt-get update \
7
&& apt-get install -y wget unzip
8
​
9
RUN mkdir artifacts
10
RUN wget https://starcounter.io/Starcounter/Starcounter.3.0.0-alpha-20190930.zip -O ./artifacts/Starcounter.zip
11
RUN unzip ./artifacts/Starcounter.zip -d ./artifacts
12
​
13
# Copy source files
14
RUN mkdir App
15
COPY ./App/nuget.config ./App/nuget.config
16
COPY ./App/Program.cs ./App/Program.cs
17
COPY ./App/App.csproj ./App/App.csproj
18
​
19
WORKDIR /source/App
20
RUN dotnet publish -c Release -o out
21
​
22
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-bionic AS runtime
23
​
24
# Install Starcounter dependencies
25
RUN apt-get update \
26
&& apt-get install -y wget unzip \
27
&& apt-get install -y software-properties-common \
28
&& apt-get install -y libaio1 \
29
&& add-apt-repository -y ppa:swi-prolog/stable \
30
&& apt-get update \
31
&& apt-get install -y swi-prolog-nox=7.\*
32
​
33
ENV ASPNETCORE_URLS http://+:8080
34
WORKDIR /source/publish
35
COPY --from=build /source/App/out ./
36
​
37
ENTRYPOINT ["dotnet", "App.dll"]
Copied!