A bind mount maps a host filesystem location to a location inside a Docker container. Use one when a container needs files maintained alongside the host application, such as Keycloak realm configuration. section 2.4
To import Keycloak realms in an Aspire host:
- Create a local Keycloak folder and put the realm export JSON file in it. section 2.4
- Call WithBindMount to map that folder to /opt/keycloak/data/import, and launch Keycloak with --import-realm. Keycloak checks the mapped directory at startup. section 2.4
- Configure the host project to copy the JSON file to its output directory so it remains available with the built executable. section 2.4
Listing 2.2 Importing the Keycloak realms
public static IResourceBuilder<KeycloakResource>
ImportRealms(this IResourceBuilder<KeycloakResource>
builder,
string source)
{
builder
.WithBindMount(source,
"/opt/keycloak/data/import")
.WithAnnotation(
new CommandLineArgsCallbackAnnotation(
args =>
{
args.Clear();
if (builder.ApplicationBuilder
.ExecutionContext.IsRunMode)
{
args.Add("start-dev");
}
else
{
args.Add("start");
}
args.Add("--import-realm");
}));
return builder;
}
The ImportRealms extension combines the bind mount with Keycloak’s import argument; it also selects start-dev for run mode and start otherwise. section 2.4
The host project is a convenient location for this example. In a production codebase, the realm file might instead live in a configuration library or an environment-specific directory. section 2.4

