How to containerize your Service Fabric Reliable Services and Reliable Actors (Preview)

Service Fabric supports containerizing Service Fabric microservices (Reliable Services, and Reliable Actor based services). For more information, see service fabric containers.

This feature is in preview and this article provides the various steps to get your service running inside a container.

Note

This feature is in preview and is not supported in production. Currently this feature only works for Windows.

Steps to containerize your Service Fabric Application

  1. Open your Service Fabric application in Visual Studio.

  2. Add class SFBinaryLoader.cs to your project. The code in this class is a helper to correctly load the Service Fabric runtime binaries inside your application when running inside a container.

  3. For each code package you would like to containerize, initialize the loader at the program entry point. Add the static constructor shown in the following code snippet to your program entry point file.

    namespace MyApplication
    {
       internal static class Program
       {
           static Program()
           {
               SFBinaryLoader.Initialize();
           }
    
           /// <summary>
           /// This is the entry point of the service host process.
           /// </summary>
           private static void Main()
           {
    
  4. Build and package your project. To build and create a package, right-click the application project in Solution Explorer and choose the Package command.

  5. For every code package you need to containerize, run the PowerShell script CreateDockerPackage.ps1. The usage is as follows:

     $codePackagePath = 'Path to the code package to containerize.'
     $dockerPackageOutputDirectoryPath = 'Output path for the generated docker folder.'
     $applicationExeName = 'Name of the ode package executable.'
     CreateDockerPackage.ps1 -CodePackageDirectoryPath $codePackagePath -DockerPackageOutputDirectoryPath $dockerPackageOutputDirectoryPath -ApplicationExeName $applicationExeName
    

    The script creates a folder with Docker artifacts at $dockerPackageOutputDirectoryPath. Modify the generated Dockerfile to expose any ports, run setup scripts etc. based on your needs.

  6. Next you need to build and push your Docker container package to your repository.

  7. Modify the ApplicationManifest.xml and ServiceManifest.xml to add your container image, repository information, registry authentication, and port-to-host mapping. For modifying the manifests, see Create an Azure Service Fabric container application. The code package definition in the service manifest needs to be replaced with corresponding container image. Make sure to change the EntryPoint to a ContainerHost type.

    <!-- Code package is your service executable. -->
    <CodePackage Name="Code" Version="1.0.0">
    <EntryPoint>
     <!-- Follow this link for more information about deploying Windows containers to Service Fabric: https://aka.ms/sfguestcontainers -->
     <ContainerHost>
       <ImageName>myregistry.azurecr.io/samples/helloworldapp</ImageName>
     </ContainerHost>
    </EntryPoint>
    <!-- Pass environment variables to your container: -->    
    </CodePackage>
    
  8. Add the port-to-host mapping for your replicator and service endpoint. Since both these ports are assigned at runtime by Service Fabric, the ContainerPort is set to zero to use the assigned port for mapping.

    <Policies>
    <ContainerHostPolicies CodePackageRef="Code">
     <PortBinding ContainerPort="0" EndpointRef="ServiceEndpoint"/>
     <PortBinding ContainerPort="0" EndpointRef="ReplicatorEndpoint"/>
    </ContainerHostPolicies>
    </Policies>
    
  9. To test this application, you need to deploy it to a cluster that is running version 5.7 or higher. In addition, you need to edit and update the cluster settings to enable this preview feature. Follow the steps in this article to add the setting shown next.

       {
         "name": "Hosting",
         "parameters": [
           {
             "name": "FabricContainerAppsEnabled",
             "value": "true"
           }
         ]
       }
    
  10. Next deploy the edited application package to this cluster.

You should now have a containerized Service Fabric application running your cluster.

Next steps