liveBook · Manning

Bind mount

As explained in Building Distributed Applications that Work

A bind mount maps a location in the host filesystem to a location in a container filesystem.

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

How Building Distributed Applications that Work covers it

How deep it goes
Defines the mapping and shows its Aspire configuration, startup argument, and build-output requirement without exploring Docker internals.
Examples
Maps a local Keycloak folder to /opt/keycloak/data/import through an ImportRealms extension method.
What it assumes
Assumes an Aspire host and Keycloak container setup while defining the bind mount itself from first principles.
What you can do after
Apply it via Aspire’s WithBindMount to supply realm files to Keycloak at startup.
How it connects
Connects Docker containers, Aspire resources, Keycloak realm import, and build output configuration.

Also explained in

Compare how 2 books explain bind mount →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Distributed Applications that Work. The section links point to the parts of the book it draws on.