Skip to content

Auto-generate Dockerfile during deployment if requirements are met #19

@MichaelSchmidle

Description

@MichaelSchmidle

Problem

Currently, users must explicitly run light generate dockerfile after adding required scripts to package.json. This creates friction in the deployment workflow:

  1. User runs light up production (configures environment interactively)
  2. Error: "Dockerfile not found"
  3. User adds build and start scripts to package.json
  4. User runs light generate dockerfile
  5. User runs light up production again

This is 3 extra steps after the initial error.

Proposed Solution

Smart auto-generation in light up <env> (deployment mode):

  1. Check if Dockerfile exists → if yes, proceed
  2. Check if package.json has required scripts → if no, show error (current behavior)
  3. If scripts exist but Dockerfile missing → auto-generate and continue

Example Flow (Improved)

# User runs deployment for first time
light up production

# CLI detects valid scripts, generates Dockerfile automatically
ℹ Dockerfile not found, but package.json has required scripts
✓ Generated Dockerfile automatically
ℹ Review and customize: Dockerfile
✓ Building app container...

Implementation Details

In up.ts (deployment mode validation):

// FR-012 Auto-generation: Generate Dockerfile if scripts are valid
if (!existsSync('Dockerfile')) {
  // Check if package.json has required scripts
  if (existsSync('package.json')) {
    const packageJson = JSON.parse(readFileSync('package.json', 'utf-8'));
    const scripts = packageJson.scripts || {};

    if (scripts.build && scripts.start) {
      // Scripts exist - auto-generate Dockerfile
      console.log(chalk.blue('ℹ'), 'Dockerfile not found, but package.json has required scripts');
      
      const dockerfile = generateDockerfile({
        nodeVersion: '20-alpine',
        packageManager: 'npm',
        buildCommand: 'build',
        startCommand: 'start',
        appPort: 3000
      });
      
      writeFileSync('Dockerfile', dockerfile);
      console.log(chalk.green('✓'), 'Generated Dockerfile automatically');
      console.log(chalk.blue('ℹ'), 'Review and customize:', chalk.cyan('Dockerfile'));
      console.log();
      
      // Continue with deployment (Dockerfile now exists)
    } else {
      // Scripts missing - show current error message
      throw new Error(...);
    }
  }
}

Benefits

  1. Reduced friction: Users don't need to learn light generate dockerfile command
  2. Progressive disclosure: Dockerfile appears exactly when needed
  3. Transparency: Clear message that it was auto-generated
  4. Escape hatch: Users can still customize Dockerfile before re-running
  5. Safe: Only generates if scripts are valid (no blind generation)

Success Criteria

  • User runs light up <env> for first time → Dockerfile auto-generated if scripts valid
  • Clear console output showing auto-generation happened
  • Existing light generate dockerfile command still works (for manual regeneration)
  • Error message still shows if scripts are missing (same as now)
  • No behavior change if Dockerfile already exists

Related

Notes

This is the proper fix for the workflow introduced in Spec 002. The current light generate dockerfile command was added as a bridge solution to unblock deployment testing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    dxDeveloper experience improvementsenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions