dockerfile

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package dockerfile contains the logic for creating a Dockerfile

Index

Constants

This section is empty.

Variables

View Source
var (

	// Build is the command that builds a Dockerfile
	Build = &cobra.Command{
		Use:   "build",
		Short: "Builds a dockerfile based on the config file",
		RunE: func(cmd *cobra.Command, args []string) error {
			cmd.SilenceUsage = true

			var dckFile []string
			gitignore, _ := ignore.CompileIgnoreFile(".gitignore")

			cfg, err := config.Read()
			if err != nil {
				return err
			}

			dckFile = append(dckFile, fmt.Sprintf("FROM %s", cfg.Image.URI))

			dckFile = append(dckFile, getEnvVars(cfg.Image.Variables)...)

			dckFile = append(dckFile, appendRun(cfg.Image.OnCreation)...)

			pkgUpdate := pkgm.NewOperation("update", []string{}, []string{}, true)
			update, _ := pkgUpdate.StringCommand(&cfg.Image.PkgManager)
			dckFile = append(dckFile, fmt.Sprintf("RUN %s", update))

			if len(cfg.Packages)+len(cfg.DevPackages) > 0 {
				pkgInstall := pkgm.NewOperation("add", cfg.Packages, []string{}, true)
				if devBuild {
					pkgInstall.Packages = append(pkgInstall.Packages, cfg.DevPackages...)
				}
				packages, _ := pkgInstall.StringCommand(&cfg.Image.PkgManager)
				clean := pkgm.NewOperation("clean", []string{}, []string{}, true)
				cleanCmd, _ := clean.StringCommand(&cfg.Image.PkgManager)
				dckFile = append(dckFile, fmt.Sprintf("RUN %s && %s", packages, cleanCmd))
			}

			dckFile = append(dckFile, appendRun(cfg.Image.OnFinish)...)

			dckFile = append(dckFile, exposePorts(cfg.Container.Ports)...)

			for _, mount := range cfg.Container.Mounts {
				regex := regexp.MustCompile(`([a-zA-Z0-9\.\-\/]+):([a-zA-Z0-9\.\-\/]+):?([a-zA-Z0-9\.\-\/]+)?`)
				match := regex.FindStringSubmatch(mount)
				if len(match) > 0 {
					dckFile = append(dckFile, fmt.Sprintf("COPY %s %s", match[1], match[2]))
				}
			}

			if includeFiles {
				dckIgnore := selectDck()
				switch dckIgnore {
				case true:
					dckFile = append(dckFile, fmt.Sprintf("COPY . %s", cfg.Container.WorkDir))
				case false:
					dckFile = append(dckFile, mountWorkspace(cfg.Container.WorkDir, gitignore)...)
				}
			}

			if command != "" {
				if _, ok := cfg.Commands[command]; !ok {
					return glg.Errorf("command %s not found in config file", command)
				}

				dckFile = append(dckFile, fmt.Sprintf("ENTRYPOINT [\"%s\", \"-c\", \"%s\"]", cfg.Container.Shell, cfg.Commands[command]))
			} else {
				dckFile = append(dckFile, fmt.Sprintf("ENTRYPOINT [\"%s\"]", cfg.Container.Shell))
			}

			if config.FileExists("Dockerfile") {
				glg.Warnf("Dockerfile already exists, overwriting...")
			}
			return writeList("Dockerfile", dckFile)
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL