How-To: Mount a volume to a container

Learn how to mount a volume to a container

This how-to guide will provide an overview of how to:

  • Mount an ephemeral (short-lived) volume to a container

Prerequisites

Step 1: Define an app and a container

Begin by creating a file named app.bicep with a Radius Application and container:

import radius as rad

param environment string

resource app 'Applications.Core/applications@2023-10-01-preview' = {
  name: 'myapp'
  properties: {
    environment: environment
  }
}

resource container 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'mycontainer'
  properties: {
    application: app.id
    container: {
      image: 'ghcr.io/radius-project/samples/volumes:latest'
    }
  }
}

The samples/volumes container will display the status and contents of the /tmpdir directory within the container.

Step 2: Deploy the app and container

  1. Deploy your app with the command:

    rad deploy ./app.bicep
    
  2. Once complete, port forward to your container with rad resource expose:

    rad resource expose containers mycontainer -a myapp --port 5000
    
  3. Visit localhost:5000 in your browser. You should see a message warning that the directory /tmpdir does not exist:

Step 3: Add an ephemeral volume

Within the container.volume property, add a new volume named temp and configure it as a memory-backed ephemeral volume:

resource container 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'mycontainer'
  properties: {
    application: app.id
    container: {
      image: 'ghcr.io/radius-project/samples/volumes:latest'
      volumes: {
        tmp: {
          kind: 'ephemeral'
          managedStore: 'memory'
          mountPath: '/tmpdir'
        }
      }
    }
  }
}

Redeploy your app and container

  1. Redeploy your application to apply the new definition of your container:

    rad deploy ./app.bicep
    
  2. Once complete, port forward to your container with rad resource expose:

    rad resource expose containers mycontainer -a myapp --port 5000
    
  3. Visit localhost:5000 in your browser. You should see the contents of /tmpdir, showing an empty directory.

  4. Press the Create file button to generate a new file in the directory, such as test.txt:

  5. Done! You’ve now learned how to mount an ephemeral volume

Cleanup

  1. Run the following command to delete your app and container:

    rad app delete myapp