docker

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package docker provides Docker integration for running temporary ClickHouse instances for migration testing and schema validation workflows.

The package enables developers to stand up ClickHouse containers with complete project configuration including cluster settings, keeper/zookeeper setup, and custom macros - ensuring the Docker environment matches the target production environment.

Key Features

  • Temporary ClickHouse containers with project configuration volume mounting
  • Support for ReplicatedMergeTree tables and distributed operations
  • SQL execution and file processing within containers
  • Schema dumping for validation and comparison
  • Automatic container lifecycle management with cleanup
  • Configurable ports, versions, and container names

Usage Example

import (
	"context"
	"github.com/docker/docker/client"
	"github.com/pseudomuto/housekeeper/pkg/docker"
	"github.com/pseudomuto/housekeeper/pkg/clickhouse"
)

// Create Docker client
dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
	log.Fatal(err)
}
defer dockerClient.Close()

// Create and configure ClickHouse container
container, err := docker.NewWithOptions(dockerClient, docker.DockerOptions{
	Version:   "25.7",
	ConfigDir: "/path/to/clickhouse/config.d",
})
if err != nil {
	log.Fatal(err)
}

ctx := context.Background()
defer container.Stop(ctx)

if err := container.Start(ctx); err != nil {
	log.Fatal(err)
}

// Get connection details
dsn, _ := container.GetDSN()
httpDSN, _ := container.GetHTTPDSN()

// Connect using ClickHouse client
client, _ := clickhouse.NewClient(ctx, dsn)
defer client.Close()

// Execute SQL commands
err := client.ExecuteMigration(ctx, "SELECT version()")

// Dump complete schema
schema, _ := client.GetSchema(ctx)

The Docker container automatically volume mounts your ClickHouse configuration directory, ensuring the containerized ClickHouse instance has access to cluster definitions, keeper settings, and other configuration required for advanced features like ReplicatedMergeTree tables.

Index

Constants

View Source
const (
	// DefaultClickHousePort is the default port for ClickHouse server
	DefaultClickHousePort = 9000

	// DefaultClickHouseHTTPPort is the default HTTP port for ClickHouse server
	DefaultClickHouseHTTPPort = 8123
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ClickHouseContainer

type ClickHouseContainer struct {
	// contains filtered or unexported fields
}

ClickHouseContainer manages ClickHouse Docker containers for development

func New

func New(dockerClient DockerClient) (*ClickHouseContainer, error)

New creates a new ClickHouse Docker container with default options

Example:

// Create Docker client
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
	log.Fatal(err)
}
defer cli.Close()

container, err := docker.New(cli)
if err != nil {
	log.Fatal(err)
}

// Start ClickHouse container
if err := container.Start(ctx); err != nil {
	log.Fatal(err)
}
defer container.Stop(ctx)

func NewWithOptions

func NewWithOptions(dockerClient DockerClient, opts DockerOptions) (*ClickHouseContainer, error)

NewWithOptions creates a new ClickHouse Docker container with custom options

Example:

// Create Docker client
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
	log.Fatal(err)
}
defer cli.Close()

opts := docker.DockerOptions{
	Version:   "25.7",
	ConfigDir: "/path/to/project/db/config.d",
}
container, err := docker.NewWithOptions(cli, opts)
if err != nil {
	log.Fatal(err)
}

// Start ClickHouse container
if err := container.Start(ctx); err != nil {
	log.Fatal(err)
}
defer container.Stop(ctx)

func (*ClickHouseContainer) GetDSN

func (c *ClickHouseContainer) GetDSN(ctx context.Context) (string, error)

GetDSN returns the DSN for connecting to the Docker ClickHouse instance

func (*ClickHouseContainer) GetHTTPDSN

func (c *ClickHouseContainer) GetHTTPDSN(ctx context.Context) (string, error)

GetHTTPDSN returns the HTTP DSN for connecting to the Docker ClickHouse instance

func (*ClickHouseContainer) IsRunning

func (c *ClickHouseContainer) IsRunning() bool

IsRunning returns true if the container is currently running

func (*ClickHouseContainer) Start

func (c *ClickHouseContainer) Start(ctx context.Context) error

Start starts a ClickHouse Docker container with the configured version

func (*ClickHouseContainer) Stop

Stop stops and removes the ClickHouse Docker container

type Container

type Container struct {
	Names  []string
	Image  string
	State  string
	Status string
}

type ContainerOptions

type ContainerOptions struct {
	Name    string
	Image   string
	Env     map[string]string
	Ports   map[int]int
	Volumes []ContainerVolume
}

type ContainerVolume

type ContainerVolume struct {
	HostPath      string `yaml:"hostPath"`
	ContainerPath string `yaml:"containerPath"`
	ReadOnly      bool   `yaml:"readOnly"`
}

type DockerClient

DockerClient defines the interface for Docker operations used by the Engine. This interface is satisfied by *client.Client and allows for easy mocking in tests.

type DockerOptions

type DockerOptions struct {
	// Version is the ClickHouse version to run (default: latest)
	Version string

	// ConfigDir is the optional ClickHouse config directory path to mount (relative paths will be converted to absolute)
	ConfigDir string

	// Name is the container name (default: housekeeper-dev)
	Name string
}

DockerOptions represents options for running ClickHouse in Docker

Jump to

Keyboard shortcuts

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