Dockerizer by Easypanel

repository·main·Indexed 42 days ago

https://github.com/easypanel-io/dockerizer

A tool for automating the generation of Docker-based environments from templates and configuration files. It includes pre-defined dockerizers for Angular, NestJS, Next.js, Node.js, Nuxt.js, PHP, and React, using Zod for schema validation and Nunjucks for template rendering.

Tokens
5.4K
Snippets
29
Records
30
Agent score
54%

What's inside Dockerizer

  1. Compile Dockerizer files to JSON

    main

    Dockerizer files and templates located in the files/ directory must be compiled into a files.json file for use. Use the following scripts to manage this process:

    • npm run compile-files: Compiles all files from a dockerizer into files.json once.
    • npm run compile-files-watch: Watches for changes in the files/ directory and automatically runs the compilation.
    npm run compile-files
    npm run compile-files-watch
  2. Create a new Dockerizer

    main

    To create a new Dockerizer, create a new directory under src/dockerizers/[name]. You can use the existing sample dockerizer as a template to get started.

    Each Dockerizer directory follows this structure:

    • index.ts: Contains the dockerizer schema and generation logic.
    • files/: A directory containing the raw files, templates, and configuration files used by the dockerizer.
    • files.json: The compiled version of the files/ directory (generated via scripts).
    • tests/: Contains test suites for the dockerizer, including the application code and input.json used for testing.
    src/dockerizers/[name]/
    ├── index.ts          # dockerizer schema & generation logic
    ├── files/            # files, templates, config files
    ├── files.json        # compiled from the files folder
    └── tests/
        └── [test-name]/
            ├── code/     # test application codebase
            └── input.json # test input for the dockerizer
  3. Generate tests and lint Dockerfiles

    main

    When developing or testing Dockerizers, use these scripts to manage test generation and linting:

    • npm run compile-tests: Generates the dockerizer folder for each test application based on the configuration provided in input.json.
    • npm run hadolint: Lints all Dockerfiles. Note: You must run npm run compile-tests before running this command to ensure the Dockerfiles exist.
    npm run compile-tests
    npm run hadolint
  4. Configure the Next.js dockerizer schema

    main

    The Next.js dockerizer uses a Zod schema to define configuration options for generating a Dockerfile. You can provide the following options to customize the build environment:

    • nodeImage (string): The base Node.js image to use. Defaults to "node:20".
    • telemetry (boolean): Whether to enable/disable telemetry. Defaults to false.
    • host (string): The host address the application binds to. Defaults to "0.0.0.0".
    • port (string): The port the application listens on. Defaults to "3000".
    • user (string, advanced): The user inside the container. Defaults to "nextjs".
    • group (string, advanced): The group inside the container. Defaults to "nodejs".
    • alpinePackages (string, advanced): Additional Alpine packages to install. Defaults to "libc6-compat".

    Available nodeImage options include:

    • node:16 / node:16-alpine
    • node:18 / node:18-alpine
    • node:20 / node:20-alpine
    import { schema } from './src/dockerizers/nextjs/index';
    
    const config = {
      nodeImage: 'node:20-alpine',
      port: '3001',
      telemetry: true
    };
    
    // Validates and provides defaults
    const validated = schema.parse(config);
  5. Configure Node.js Dockerizer options

    main

    The Node.js dockerizer uses a schema to define configuration for generating Dockerfiles. You can specify the base Node.js image and the application port.

    Available nodeImage options include:

    • node:16
    • node:16-alpine
    • node:18
    • node:18-alpine
    • node:20
    • node:20-alpine

    Default values are:

    • nodeImage: "node:18"
    • port: 3000
    {
      "nodeImage": "node:20-alpine",
      "port": 8080
    }
  6. Configure the Angular dockerizer schema

    main

    The Angular dockerizer uses a Zod schema to define the configuration required to generate Dockerfiles and Nginx configurations. You can provide the following keys in your input object:

    • projectName: The name of the project (defaults to "myApp").
    • nodeImage: The Docker image used for the Node.js build stage (defaults to "node:18-alpine").
    • nginxImage: The Docker image used for the Nginx serving stage (defaults to "nginx:stable").
    • port: The port the Nginx server will listen on (defaults to "80").
    {
      "projectName": "my-angular-app",
      "nodeImage": "node:20-alpine",
      "nginxImage": "nginx:latest",
      "port": "80"
    }
  7. Configure PHP Dockerizer options

    main

    The PHP dockerizer uses a schema to define configuration for generating Dockerfiles, web server virtual host configurations, and php.ini files. You can provide a configuration object to the generate function to customize the environment.

    Configuration Schema

    KeyTypeDefaultDescription
    phpVersionstring"8.2"Supported: "7.2", "7.3", "7.4", "8.0", "8.1", "8.2"
    alpinebooleantrueWhether to use an Alpine-based image
    documentRootstring"/"The web server document root (relative to /app)
    webServerstring"nginx"Supported: "apache", "nginx"
    composerInstallbooleanfalseWhether to run composer install
    phpDateTimezonestring"UTC"PHP timezone setting
    phpDisplayErrorsstring"Off"PHP error display setting
    phpMemoryLimitstring"128M"PHP memory limit
    phpMaxExecutionTimenumber60PHP maximum execution time in seconds
    phpPostMaxSizestring"32M"PHP post max size
    phpUploadMaxFilesizestring"16M"PHP upload max filesize
    // Example configuration object
    const config = {
      phpVersion: "8.1",
      webServer: "nginx",
      documentRoot: "public",
      phpMemoryLimit: "256M",
      composerInstall: true
    };
  8. Configure the Strapi dockerizer schema

    main

    The Strapi dockerizer uses a Zod schema to define configuration options for generating Dockerfiles. You can customize the Node.js version, build-stage system packages, production-stage system packages, the execution user, and the application port.

    Available configuration keys:

    • nodeVersion: The Node.js version to use (defaults to "18"). Supported options include "16", "18", and "20".
    • buildStagePackages: A space-separated string of system packages to install during the build stage (defaults to "build-base gcc autoconf automake zlib-dev libpng-dev vips-dev git").
    • productionStagePackages: A space-separated string of system packages to install during the production stage (defaults to "vips-dev").
    • user: The Linux user to run the application (defaults to "node").
    • port: The application port (defaults to "1337").
    const config = {
      nodeVersion: "20",
      port: "1337",
      user: "node",
      buildStagePackages: "gcc git",
      productionStagePackages: "vips-dev"
    };
  9. Generate Dockerfiles for Next.js using generate()

    main

    The generate function takes a configuration object matching the Next.js schema and returns a Files object containing the rendered Dockerfile. It uses Nunjucks templates internally to inject your configuration into the Dockerfile structure.

    Input must satisfy the schema requirements. If values are missing, the defaults defined in the schema will be used.

    import { generate } from './src/dockerizers/nextjs/index';
    
    const files = generate({
      nodeImage: 'node:18-alpine',
      port: '3000',
      host: '0.0.0.0'
    });
    
    console.log(files.Dockerfile); // Contains the rendered Dockerfile string
  10. Generate Strapi Dockerfiles with generate()

    main

    The generate function takes a configuration object matching the Strapi schema and returns a Files object containing the rendered Dockerfile.

    It uses Nunjucks templates internally to inject your configuration into the Dockerfile structure.

    import { generate } from './src/dockerizers/strapi/index';
    
    const files = generate({
      nodeVersion: "20",
      port: "1337"
    });
    
    console.log(files.Dockerfile);
  11. Generate Angular Docker files with generate()

    main

    The generate function takes a configuration object matching the schema and returns a Files object containing the rendered Dockerfile and the nginx.conf file. It uses Nunjucks templates internally to inject your configuration into the Dockerfile.

    import { generate } from "./src/dockerizers/angular/index";
    
    const files = generate({
      projectName: "my-app",
      nodeImage: "node:20-alpine",
      nginxImage: "nginx:stable",
      port: "80"
    });
    
    // files contains:
    // files.Dockerfile
    // files['nginx.conf']