Cloud Native Buildpacks (CNB) Samples

repository·main·Indexed 42 days ago

https://github.com/buildpacks/samples

A collection of sample implementations for Cloud Native Buildpacks (CNB) components. This repository provides developers with examples for testing and experimenting with apps, buildpacks, builders, base images, and packages, including guides for using the pack CLI, project descriptors (project.toml), and creating custom builders like alpine and resolute.

Tokens
11.4K
Snippets
51
Records
66
Agent score
52%

What's inside Cloud Native Buildpacks Samples

  1. Use Sample Buildpacks

    main
    This repository contains various sample buildpacks designed to demonstrate how to turn source code into container images. Each buildpack is self-contained; to understand the specific implementation, requirements, or usage of a particular buildpack, you must refer to the README.md file located within its specific directory.
  2. Explore Sample Builders

    main

    This repository contains sample builders that are built using the base images provided in this repository. These samples can be used to understand how to compose builders or to test buildpack behavior.

    Available sample builders:

    • Alpine: Located in the alpine/ directory.
    • resolute: Located in the resolute/ directory.
  3. Configure the project using project.toml

    main

    The project.toml file in this sample acts as a project descriptor for Cloud Native Buildpacks. It allows you to define how the project is described to the buildpacks and supports the following features:

    • Include/Exclude files: Control which files are included in the build context.
    • Specify buildpacks: Explicitly define which buildpacks should be used for the build.
  4. Use project.toml for project descriptors

    main

    The project.toml file in this sample acts as a project descriptor for Cloud Native Buildpacks. It allows you to define project-specific metadata and build instructions, including:

    • Include/Exclude files: Control which files are included in the build context.
    • Specify buildpacks: Explicitly define which buildpacks should be used during the build process.
  5. Configure Ruby version using .ruby-version

    main

    To specify a specific version of Ruby for the sample buildpack to install, create a .ruby-version file in the root of your application directory. The ruby-bundler sample buildpack uses this file to determine which Ruby version should be provisioned during the build process.

    # Example .ruby-version file content
    3.2.2
  6. Build and run the Hello World sample buildpack

    main

    The hello-world sample is a no-op buildpack designed to demonstrate the minimal requirements of a buildpack. You can build an application using this buildpack with the pack CLI by pointing to the current directory as the buildpack source and specifying a builder (either cnbs/sample-builder:alpine or cnbs/sample-builder:resolute). After building, you can verify the result by running the resulting Docker image and executing an echo command.

    # Build using the alpine builder
    pack build sample-hello-world-app --builder cnbs/sample-builder:alpine --buildpack .
    
    # OR build using the resolute builder
    pack build sample-hello-world-app --builder cnbs/sample-builder:resolute --buildpack .
    
    # Run the resulting image
    docker run sample-hello-world-app echo hello
  7. Build the Hello Moon sample app

    main

    The hello-moon buildpack is a no-op sample designed to demonstrate how buildpack dependencies function. It depends on the hello-world buildpack. You can build an application using this sample by specifying both the hello-world buildpack and the hello-moon buildpack (the current directory) in your pack build command.

    # Using the alpine builder
    pack build sample-hello-moon-app --builder cnbs/sample-builder:alpine --buildpack ../hello-world/ --buildpack .
    
    # OR using the resolute builder
    pack build sample-hello-moon-app --builder cnbs/sample-builder:resolute --buildpack ../hello-world/ --buildpack .
  8. Build a Ruby app using the Ruby Bundler Buildpack

    main

    To build a Ruby application that uses Bundler, use the pack CLI with the Ruby Bundler buildpack. This buildpack is compatible with Ruby applications that rely on Bundler for dependency management.

    Ensure you point the --buildpack flag to the directory containing the buildpack and the --path flag to the directory containing your Ruby application source code.

    pack build sample-ruby-bundler-app --builder cnbs/sample-builder:resolute --buildpack . --path ../../apps/ruby-bundler
  9. Build a meta-buildpack using the hello-universe sample

    main

    The hello-universe sample is a no-op meta-buildpack designed to demonstrate how to construct meta-buildpacks. You can use the pack CLI to build an application by specifying multiple buildpacks, including the current directory (the meta-buildpack itself) and other local buildpack directories.

    pack build sample-hello-universe-app \
        --builder cnbs/sample-builder:alpine \
        --buildpack . \
        --buildpack ../hello-world/ \
        --buildpack ../hello-moon/
  10. Build a Java Maven application using the sample buildpack

    main

    Use the pack CLI to build a Java application that uses Maven. This sample buildpack is compatible with Java apps that utilize Maven for dependency management and builds.

    To build the application, you must specify a builder (e.g., cnbs/sample-builder:alpine or cnbs/sample-builder:resolute), point the --buildpack flag to the current directory (.), and provide the path to the application source code using --path.

    # Using the alpine builder
    pack build sample-java-maven-app --builder cnbs/sample-builder:alpine --buildpack . --path ../../apps/java-maven
    
    # OR using the resolute builder
    pack build sample-java-maven-app --builder cnbs/sample-builder:resolute --buildpack . --path ../../apps/java-maven
  11. Build a Kotlin Gradle application using the Kotlin Gradle Buildpack

    main

    Use this buildpack to build Kotlin applications that utilize the Gradle build system. You can build the application using the pack CLI by specifying the current directory as the buildpack and pointing to the application source path.

    Note that you must provide a builder image (e.g., cnbs/sample-builder:alpine or cnbs/sample-builder:resolute) to execute the build.

    # Using the alpine builder
    pack build sample-kotlin-gradle-app --builder cnbs/sample-builder:alpine --buildpack . --path ../../apps/kotlin-gradle
    
    # OR using the resolute builder
    pack build sample-kotlin-gradle-app --builder cnbs/sample-builder:resolute --buildpack . --path ../../apps/kotlin-gradle