How to specify environment variables for services in Service Fabric

This article shows you how to specify environment variables for a service or container in Service Fabric.

Procedure for specifying environment variables for services

In this example, you set an environment variable for a container. The article assumes you already have an application and service manifest.

  1. Open the ServiceManifest.xml file.

  2. In the CodePackage element, add a new EnvironmentVariables element and an EnvironmentVariable element for each environment variable.

    <CodePackage Name="MyCode" Version="CodeVersion1">
            ...
            <EnvironmentVariables>
                  <EnvironmentVariable Name="MyEnvVariable" Value="DefaultValue"/>
                  <EnvironmentVariable Name="HttpGatewayPort" Value="19080"/>
            </EnvironmentVariables>
    </CodePackage>
    

    Environment variables can be overridden in the application manifest.

  3. To override the environment variables in the application manifest, use the EnvironmentOverrides element.

      <ServiceManifestImport>
        <ServiceManifestRef ServiceManifestName="Guest1Pkg" ServiceManifestVersion="1.0.0" />
        <EnvironmentOverrides CodePackageRef="MyCode">
          <EnvironmentVariable Name="MyEnvVariable" Value="OverrideValue"/>
        </EnvironmentOverrides>
      </ServiceManifestImport>
    

Specifying environment variables dynamically using Docker Compose

Service Fabric supports the ability to Use Docker Compose for Deployment. Compose files can source environment variables from the shell. This behavior can be used to substitute desired environment values dynamically:

environment:
  - "hostname:${hostname}"

Next steps

To learn more about some of the core concepts that are discussed in this article, see the Manage applications for multiple environments.

For information about other app management capabilities that are available in Visual Studio, see Manage your Service Fabric applications in Visual Studio.