Skip to main content

Method chaining

All template methods return the template instance, allowing for fluent API usage:
const template = Template()
  .fromUbuntuImage("22.04")
  .aptInstall(["curl"]) // necessary for waitForPort
  .setWorkdir('/app')
  .copy("package.json", "/app/package.json")
  .runCmd("npm install")
  .setStartCmd("npm start", waitForPort(3000));

User and workdir

Set the working directory and user for the template:
// Set working directory
template.setWorkdir("/app");

// Set user (runs subsequent commands as this user)
template.setUser('node')
template.setUser("1000:1000"); // User ID and group ID

Copying files

Copy files from your local filesystem to the template:
// Copy a single file
template.copy("package.json", "/app/package.json");

// Copy multiple files to same destination
template.copy(["file1", "file2"], "/app/file")

// Multiple copy operations using copyItems
template.copyItems([
  { src: "src/", dest: "/app/src/" },
  { src: "package.json", dest: "/app/package.json" },
]);

// Copy with user and mode options
template.copy("config.json", "/app/config.json", {
  user: "appuser",
  mode: 0o644,
});

File operations

Perform various file operations during template build:
// Remove files or directories
template.remove("/tmp/temp-file.txt");
template.remove("/old-directory", { recursive: true });
template.remove("/file.txt", { force: true });

// Rename files or directories
template.rename("/old-name.txt", "/new-name.txt");
template.rename("/old-dir", "/new-dir", { force: true });

// Create directories
template.makeDir("/app/logs");
template.makeDir("/app/data", { mode: 0o755 });

// Create symbolic links
template.makeSymlink("/app/data", "/app/logs/data");

Installing packages

Install packages using package managers:
// Install Python packages
template.pipInstall('requests pandas numpy')
template.pipInstall(['requests', 'pandas', 'numpy'])

// Install Node.js packages
template.npmInstall('express lodash')
template.npmInstall(['express', 'lodash'])

// Install system packages (automatically runs apt update)
template.aptInstall('curl wget git')
template.aptInstall(['curl', 'wget', 'git'])

Git operations

Clone Git repositories during template build (requires git to be installed):
// Clone a repository
template.gitClone('https://github.com/user/repo.git')

// Clone a repository to a specific path
template.gitClone('https://github.com/user/repo.git', '/app/repo')

// Clone a specific branch
template.gitClone('https://github.com/user/repo.git', '/app/repo', {
  branch: 'main',
})

// Shallow clone with depth limit
template.gitClone('https://github.com/user/repo.git', '/app/repo', {
  depth: 1,
})

Environment variables

Set environment variables in the template:
template.setEnvs({
  NODE_ENV: 'production',
  API_KEY: 'your-api-key',
  DEBUG: 'true',
})

Running commands

Execute shell commands during template build:
// Run a single command
template.runCmd('apt-get update && apt-get install -y curl')

// Run multiple commands
template.runCmd(['apt-get update', 'apt-get install -y curl', 'curl --version'])

// Run commands as a specific user
template.runCmd('npm install', { user: 'node' })
I