compose

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package compose provides Docker Compose file parsing and generation utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectComposeVersion

func DetectComposeVersion() (string, error)

DetectComposeVersion detects whether docker compose v2 or v1 is available. Returns "v2" for "docker compose", "v1" for "docker-compose", or error if neither.

Types

type ComposeFile

type ComposeFile struct {
	Version  string             `yaml:"version,omitempty"`
	Services map[string]Service `yaml:"services"`
	Networks map[string]any     `yaml:"networks,omitempty"`
	Volumes  map[string]any     `yaml:"volumes,omitempty"`
}

ComposeFile represents a Docker Compose file structure.

func BuildComposeFile

func BuildComposeFile(services map[string]Service) *ComposeFile

BuildComposeFile creates a complete Docker Compose file from services.

func Parse

func Parse(path string) (*ComposeFile, error)

Parse reads and parses a Docker Compose file from the given path.

func ParseFromString

func ParseFromString(content string) (*ComposeFile, error)

ParseFromString parses a Docker Compose file from a string.

func (*ComposeFile) GetService

func (c *ComposeFile) GetService(name string) (Service, bool)

GetService returns a service by name.

func (*ComposeFile) String

func (c *ComposeFile) String() (string, error)

String returns the YAML representation of the compose file.

func (*ComposeFile) WriteToFile

func (c *ComposeFile) WriteToFile(path string) error

WriteToFile writes the compose file to disk.

type HealthCheck

type HealthCheck struct {
	Test        []string `yaml:"test,omitempty"`
	Interval    string   `yaml:"interval,omitempty"`
	Timeout     string   `yaml:"timeout,omitempty"`
	Retries     int      `yaml:"retries,omitempty"`
	StartPeriod string   `yaml:"start_period,omitempty"`
}

HealthCheck represents a Docker health check configuration.

type Service

type Service struct {
	Image         string            `yaml:"image,omitempty"`
	ContainerName string            `yaml:"container_name,omitempty"`
	Environment   map[string]string `yaml:"environment,omitempty"`
	Ports         []string          `yaml:"ports,omitempty"`
	Volumes       []string          `yaml:"volumes,omitempty"`
	Command       string            `yaml:"command,omitempty"`
	Networks      []string          `yaml:"networks,omitempty"`
	HealthCheck   *HealthCheck      `yaml:"healthcheck,omitempty"`
	DependsOn     map[string]any    `yaml:"depends_on,omitempty"`
	Restart       string            `yaml:"restart,omitempty"`
}

Service represents a single service in a Docker Compose file.

func GenerateMySQLCompose

func GenerateMySQLCompose(serviceName, image, username, password, database string, port string) Service

GenerateMySQLCompose generates a Docker Compose snippet for MySQL.

func GeneratePostgresCompose

func GeneratePostgresCompose(serviceName, image, username, password, database string, port string) Service

GeneratePostgresCompose generates a Docker Compose snippet for PostgreSQL.

func (*Service) AddDependsOn

func (s *Service) AddDependsOn(serviceName string, condition string)

AddDependsOn adds a dependency on another service.

func (*Service) AddHealthCheck

func (s *Service) AddHealthCheck(interval, timeout string, retries int, testCmd ...string)

AddHealthCheck adds a healthcheck to the service.

func (*Service) GetEnv

func (s *Service) GetEnv(key string) (string, bool)

GetEnv returns an environment variable value from a service.

func (*Service) GetPort

func (s *Service) GetPort(containerPort string) (string, error)

GetPort returns the host port for a container port mapping. Format expected: "host:container" or just "container" if not mapped.

func (*Service) SetRestart

func (s *Service) SetRestart(policy string)

SetRestart sets the restart policy.

Jump to

Keyboard shortcuts

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