Tanzu Platform 10.0

Build an application from a Dockerfile

Last Updated March 03, 2025

This topic tells you how to build a Kubernetes application from a Dockerfile on Tanzu Platform.

Before you begin

Before you can deploy an app in a Space, you must:

  • Install Tanzu CLI v1.5.1 or later. See Installing the Tanzu CLI.

  • Install the latest version of the Tanzu CLI app-developer plug-in group:

    • If the app-developer plug-in group is already installed, verify that you’re using the latest version of the build plug-in by running:
    tanzu plugin upgrade build
    
    • If the app-developer plug-in group is not present, install it by running:
    tanzu plugin install --group vmware-tanzu/app-developer
    
  • Log in to Tanzu Platform using the Tanzu CLI:

    tanzu login
    

Initialize a ContainerApp

Before you build your application, your must initialize a ContainerApp for it. The ContainerApp defines the default build and runtime configuration for your application and is used as an input to tanzu build. For more information about ContainerApps, see What is a ContainerApp.

To initialize a ContainerApp:

  1. Navigate to the root of your source code:

    cd YOUR-REPO-LOCATION
    
  2. Initialize the ContainerApp by running:

    tanzu app init
    

    Accept the suggested defaults by pressing Enter.

  3. From your source code repository, open file .tanzu/config/YOUR-APPLICATION-NAME.yml. Replace buildpacks: {} from spec.build with dockerfile: {} as shown in the following example:

    spec:
      build:
        dockerfile: {}
        path: ../..
    

    In this example, path: ../.. denotes the relative location of the directory that serves as the workspace for the build from the ContainerApp YAML file. ../.. is the root of the repository.

  4. (Optional) If your Dockerfile has a name other than Dockerfile or is not in the root of the build workspace, you can use the spec.build.dockerfile.path field to provide the path to the Dockerfile relative to the build workspace root.

    The following example searches for a Dockerfile named Dockerfile.test in a directory called sub-directory, which is at the root of the Project:

    spec:
      build:
        dockerfile:
          path: sub-directory/Dockerfile.test
        path: ../..
    
  5. (Optional) If you have a multi-stage Dockerfile where you only want a single stage to run, use the spec.build.dockerfile.multiStageTarget field to specify the name of the target to build. You can use this in conjunction with the path field.

    The following example builds stage foo in the Dockerfile called Dockerfile, which is at the root of the Project:

    spec:
      build:
        dockerfile:
          multiStageTarget: foo
        path: ../..
    

    For more information about multi-stage Dockerfiles, see the Docker documentation.

  6. To deploy your app, follow the procedure in Deploy your application.