Skip to content

virt.docker #

Tools to work with docker

How to install a docker machine

TODO: make examples how to deploy a docker machine

TODO: make examples how to use docker compose, make sure we have nice lib for it

Builders

see https://github.com/threefoldtech/vbuilders/tree/development/builders for examples

How to get started see

https://github.com/threefoldtech/vbuilders/blob/development/docsrc/src/gettingstarted/ubuntu/ubuntu.md

requirements

can only use a node.executor to execute what is required

fn new #

fn new(args DockerEngineArgs) !DockerEngine

if sshkeys_allowed empty array will check the local machine for loaded sshkeys

enum BuildPlatformType #

enum BuildPlatformType {
	linux_arm64
	linux_amd64
}

enum PlatformType #

enum PlatformType {
	alpine
	ubuntu
}

only 2 supported for now

struct AddFileEmbeddedArgs #

@[params]
struct AddFileEmbeddedArgs {
pub mut:
	source          string // is the filename, needs to be embedded
	dest            string // in the container we're building
	make_executable bool
}

struct AddFileEmbeddedItem #

struct AddFileEmbeddedItem {
pub mut:
	source          string
	dest            string // in the container we're building
	recipe          &DockerBuilderRecipe @[str: skip]
	make_executable bool
	check_embed     bool = true
}

fn (AddFileEmbeddedItem) check #

fn (mut i AddFileEmbeddedItem) check() !

fn (AddFileEmbeddedItem) render #

fn (mut i AddFileEmbeddedItem) render() !string

struct BuildArgs #

@[params]
struct BuildArgs {
pub mut:
	reset  bool // resets the docker state, will build again
	strict bool // question: ?
	engine &DockerEngine
	args   Params
}

struct CmdArgs #

@[params]
struct CmdArgs {
pub mut:
	cmd string
}

struct CmdItem #

struct CmdItem {
pub mut:
	cmd    string
	recipe &DockerBuilderRecipe @[str: skip]
}

fn (CmdItem) check #

fn (mut i CmdItem) check() !

fn (CmdItem) render #

fn (mut i CmdItem) render() !string

struct CodeGetArgs #

@[params]
struct CodeGetArgs {
pub mut:
	url string // e.g.  https://github.com/vlang/v
	// other example url := 'https://github.com/threefoldfoundation/www_examplesite/tree/development/manual'
	pull  bool
	reset bool
	name  string
	dest  string // where does the directory need to be checked out to
}

struct ComposeArgs #

@[params]
struct ComposeArgs {
pub mut:
	name        string @[required]
	params      string
	composepath string // can be empty, if empty will be buildpath/compose
}

struct ComposeService #

@[heap]
struct ComposeService {
pub mut:
	name    string
	content string // optional
	params  Params
	files   []embed_file.EmbedFileData
	recipe  &DockerComposeRecipe @[str: skip]
	render  bool = true
	env     map[string]string
	ports   []PortMap
	volumes []VolumeMap
	restart bool
	image   string
}

fn (ComposeService) env_add #

fn (mut cs ComposeService) env_add(key string, val string)

add an environment variable to the service

fn (ComposeService) restart_set #

fn (mut cs ComposeService) restart_set()

make sure the service always restarts

fn (ComposeService) port_expose #

fn (mut cs ComposeService) port_expose(indocker int, host int) !

make sure the service always restarts

fn (ComposeService) volume_add #

fn (mut cs ComposeService) volume_add(hostpath string, containerpath string) !

make sure the service always restarts

struct ComposeServiceArgs #

@[params]
struct ComposeServiceArgs {
pub:
	name    string @[required]
	image   string @[required]
	params  string
	content string // optional, if this is set then will not render
}

struct ContainerGetArgs #

@[params]
struct ContainerGetArgs {
pub mut:
	name     string
	id       string
	image_id string
	// tag    string
	// digest string
}

EXISTS, GET

struct ContainerGetError #

struct ContainerGetError {
	Error
pub:
	args     ContainerGetArgs
	notfound bool
	toomany  bool
}

fn (ContainerGetError) msg #

fn (err ContainerGetError) msg() string

fn (ContainerGetError) code #

fn (err ContainerGetError) code() int

struct CopyArgs #

@[params]
struct CopyArgs {
pub mut:
	from            string
	source          string
	dest            string
	make_executable bool // if set will make the file copied executable
}

struct CopyItem #

struct CopyItem {
pub mut:
	from            string
	source          string
	dest            string
	recipe          &DockerBuilderRecipe @[str: skip]
	make_executable bool // if set will make the file copied executable
	// check_embed bool = true
}

fn (CopyItem) check #

fn (mut i CopyItem) check() !

fn (CopyItem) render #

fn (mut i CopyItem) render() !string

struct DockerBuilderRecipe #

@[heap]
struct DockerBuilderRecipe {
pub mut:
	name     string
	prefix   string
	tag      string
	params   Params
	files    []embed_file.EmbedFileData
	engine   &DockerEngine @[str: skip]
	items    []RecipeItem
	platform PlatformType
	zinit    bool
}

fn (DockerBuilderRecipe) add_cmd #

fn (mut b DockerBuilderRecipe) add_cmd(args CmdArgs) !

add run command to docker, is the cmd which is run when docker get's built

fn (DockerBuilderRecipe) add_codeget #

fn (mut r DockerBuilderRecipe) add_codeget(args_ CodeGetArgs) !

checkout a code repository on right location

fn (DockerBuilderRecipe) add_copy #

fn (mut b DockerBuilderRecipe) add_copy(args CopyArgs) !

to do something like: 'Add alpine:latest'

fn (DockerBuilderRecipe) add_download #

fn (mut r DockerBuilderRecipe) add_download(args_ DownloadArgs) !

checkout a code repository on right location

fn (DockerBuilderRecipe) add_entrypoint #

fn (mut b DockerBuilderRecipe) add_entrypoint(args EntryPointArgs) !

fn (DockerBuilderRecipe) add_env #

fn (mut b DockerBuilderRecipe) add_env(name string, val string) !

fn (DockerBuilderRecipe) add_expose #

fn (mut b DockerBuilderRecipe) add_expose(args ExposeARgs) !

to do something like: 'Expose 8080/udp'

fn (DockerBuilderRecipe) add_file_embedded #

fn (mut b DockerBuilderRecipe) add_file_embedded(args AddFileEmbeddedArgs) !

to do something like: 'Add alpine:latest'

fn (DockerBuilderRecipe) add_from #

fn (mut b DockerBuilderRecipe) add_from(args FromArgs) !

to do something like: 'FROM alpine:latest'

fn (DockerBuilderRecipe) add_go_package #

fn (mut r DockerBuilderRecipe) add_go_package(args GoPackageArgs) !

install go components

fn (DockerBuilderRecipe) add_gobuild_from_code #

fn (mut r DockerBuilderRecipe) add_gobuild_from_code(args GoBuildArgs) !

do a build of a go package . will get the code, pull and/or reset . will the pull all dependencies . will do the build DEBUG TRICK: put debug flag on, which will not execute the build cmd . you can go to /tmp/build/buildname and do shell.sh to debug

fn (DockerBuilderRecipe) add_gobuilder #

fn (mut r DockerBuilderRecipe) add_gobuilder() !

fn (DockerBuilderRecipe) add_nodejsbuilder #

fn (mut r DockerBuilderRecipe) add_nodejsbuilder() !

fn (DockerBuilderRecipe) add_package #

fn (mut b DockerBuilderRecipe) add_package(args PackageArgs) !

add one of more name (alpine packages), no need to do update, upgrade first,

fn (DockerBuilderRecipe) add_run #

fn (mut b DockerBuilderRecipe) add_run(args RunArgs) !

to do something like: 'FROM alpine:latest'

fn (DockerBuilderRecipe) add_rust_package #

fn (mut r DockerBuilderRecipe) add_rust_package(args RustPackageArgs) !

use cargo install to install rust components

fn (DockerBuilderRecipe) add_rustbuild_from_code #

fn (mut r DockerBuilderRecipe) add_rustbuild_from_code(args RustBuildArgs) !

do a build of a rust package . will get the code, pull and/or reset . will the pull all dependencies . will do the build DEBUG TRICK: put debug flag on, which will not execute the build cmd . you can go to /tmp/build/buildname and do shell.sh to debug

fn (DockerBuilderRecipe) add_sshserver #

fn (mut r DockerBuilderRecipe) add_sshserver() !

add ssh server and init scripts (note: zinit needs to be installed)

fn (DockerBuilderRecipe) add_vbuilder #

fn (mut r DockerBuilderRecipe) add_vbuilder() !

fn (DockerBuilderRecipe) add_volume #

fn (mut b DockerBuilderRecipe) add_volume(args VolumeArgs) !

to do something like: 'Volume /data'

fn (DockerBuilderRecipe) add_workdir #

fn (mut b DockerBuilderRecipe) add_workdir(args WorkDirArgs) !

to do something like: 'FROM alpine:latest'

fn (DockerBuilderRecipe) add_zinit #

fn (mut r DockerBuilderRecipe) add_zinit() !

import freeflowuniverse.crystallib.develop.gittools import freeflowuniverse.crystallib.core.pathlib

fn (DockerBuilderRecipe) add_zinit_cmd #

fn (mut b DockerBuilderRecipe) add_zinit_cmd(args ZinitAddArgs) !

add a zinit to the docker container each init will launch a process in the container the init files are rendered as yaml files in the build directory and added to the docker at the end how to use zinit see: https://github.com/threefoldtech/zinit/tree/master/docs

fn (DockerBuilderRecipe) build #

fn (mut b DockerBuilderRecipe) build(reset bool) !

fn (DockerBuilderRecipe) delete #

fn (mut b DockerBuilderRecipe) delete() !

delete the working directory

fn (DockerBuilderRecipe) execute #

fn (mut r DockerBuilderRecipe) execute(args ExecuteArgs) !

execute the file as embedded

fn (DockerBuilderRecipe) path #

fn (mut b DockerBuilderRecipe) path() string

fn (DockerBuilderRecipe) render #

fn (mut b DockerBuilderRecipe) render() !string

fn (DockerBuilderRecipe) write_file #

fn (mut r DockerBuilderRecipe) write_file(args WriteFileArgs) !

the content will be written to dest all trailing spaces will be removed (dedent)

struct DockerComposeRecipe #

@[heap]
struct DockerComposeRecipe {
pub mut:
	name    string
	content string
	params  Params
	engine  &DockerEngine @[str: skip]
	items   []&ComposeService
	path    string
}

fn (DockerComposeRecipe) delete #

fn (mut b DockerComposeRecipe) delete() !

fn (DockerComposeRecipe) service_new #

fn (mut recipe DockerComposeRecipe) service_new(args ComposeServiceArgs) !&ComposeService

fn (DockerComposeRecipe) start #

fn (mut b DockerComposeRecipe) start() !

fn (DockerComposeRecipe) stop #

fn (mut b DockerComposeRecipe) stop() !

struct DockerContainer #

@[heap]
struct DockerContainer {
pub mut:
	id              string
	name            string
	created         time.Time
	ssh_enabled     bool // if yes make sure ssh is enabled to the container
	ipaddr          IPAddress
	forwarded_ports []string
	mounts          []utils.ContainerVolume
	ssh_port        int // ssh port on node that is used to get ssh
	ports           []string
	networks        []string
	labels          map[string]string @[str: skip]
	image           &DockerImage      @[str: skip]
	engine          &DockerEngine     @[str: skip]
	status          utils.ContainerStatus
	memsize         int // in MB
	command         string
}

need to fill in what is relevant

fn (DockerContainer) start #

fn (mut container DockerContainer) start() !

create/start container (first need to get a dockercontainer before we can start)

fn (DockerContainer) halt #

fn (mut container DockerContainer) halt() !

delete docker container

fn (DockerContainer) delete #

fn (mut container DockerContainer) delete() !

delete docker container

fn (DockerContainer) save2image #

fn (mut container DockerContainer) save2image(image_repo string, image_tag string) !string

save the docker container to image

fn (DockerContainer) export #

fn (mut container DockerContainer) export(path string) !

export docker to tgz

fn (DockerContainer) shell #

fn (mut container DockerContainer) shell(args DockerShellArgs) !

open shell to the container using docker, is interactive, cannot use in script

fn (DockerContainer) execute #

fn (mut container DockerContainer) execute(cmd_ string, silent bool) !

struct DockerContainerCreateArgs #

@[params]
struct DockerContainerCreateArgs {
	name             string
	hostname         string
	forwarded_ports  []string          // ["80:9000/tcp", "1000, 10000/udp"]
	mounted_volumes  []string          // ["/root:/root", ]
	env              map[string]string // map of environment variables that will be passed to the container
	privileged       bool
	remove_when_done bool = true // remove the container when it shuts down
pub mut:
	image_repo string
	image_tag  string
	command    string = '/bin/bash'
}

struct DockerEngine #

@[heap]
struct DockerEngine {
	name string
pub mut:
	sshkeys_allowed []string // all keys here have access over ssh into the machine, when ssh enabled
	images          []DockerImage
	containers      []DockerContainer
	buildpath       string
	localonly       bool
	cache           bool = true
	push            bool
	platform        []BuildPlatformType // used to build
	registries      []DockerRegistry    // one or more supported DockerRegistries
	prefix          string
}

https://docs.docker.com/reference/

fn (DockerEngine) compose_new #

fn (mut e DockerEngine) compose_new(args_ ComposeArgs) DockerComposeRecipe

fn (DockerEngine) container_create #

fn (mut e DockerEngine) container_create(args DockerContainerCreateArgs) !&DockerContainer

fn (DockerEngine) container_delete #

fn (mut e DockerEngine) container_delete(args ContainerGetArgs) !

fn (DockerEngine) container_exists #

fn (mut e DockerEngine) container_exists(args ContainerGetArgs) !bool

fn (DockerEngine) container_get #

fn (mut e DockerEngine) container_get(args_ ContainerGetArgs) !&DockerContainer

get container from memory, can use match_glob see https://modules.vlang.io/index.html#string.match_glob

fn (DockerEngine) container_import #

fn (mut e DockerEngine) container_import(path string, mut args DockerContainerCreateArgs) !&DockerContainer

import a container into an image, run docker container with it image_repo examples ['myimage', 'myimage:latest'] if DockerContainerCreateArgs contains a name, container will be created and restarted

fn (DockerEngine) containers_delete #

fn (mut e DockerEngine) containers_delete(args ContainerGetArgs) !

remove one or more container

fn (DockerEngine) containers_get #

fn (mut e DockerEngine) containers_get(args_ ContainerGetArgs) ![]&DockerContainer

get containers from memory params: name string (can also be a glob e.g. use *,? and []) id string image_id string

fn (DockerEngine) containers_load #

fn (mut e DockerEngine) containers_load() !

load all containers, they can be consulted in e.containers see obj: DockerContainer as result in e.containers

fn (DockerEngine) get_free_port #

fn (mut e DockerEngine) get_free_port() ?int

Get free port

fn (DockerEngine) image_exists #

fn (mut e DockerEngine) image_exists(args ImageGetArgs) !bool

fn (DockerEngine) image_get #

fn (mut e DockerEngine) image_get(args ImageGetArgs) !&DockerImage

find image based on repo and optional tag args: repo string tag string digest string id string

fn (DockerEngine) init #

fn (mut e DockerEngine) init() !

check docker has been installed & enabled on node

fn (DockerEngine) load #

fn (mut e DockerEngine) load() !

reload the state from system

fn (DockerEngine) recipe_new #

fn (mut e DockerEngine) recipe_new(args RecipeArgs) DockerBuilderRecipe

params

name     string
prefix 	 string (e.g. despiegk/ or myimage registry-host:5000/despiegk/) is added to the name when pushing
                    also exists on docker engine level, if not used here will come from the docker engine level
tag		 string (default is 'latest' but can be a version nr)
platform PlatformType
zinit    bool = true

fn (DockerEngine) registry_add #

fn (mut e DockerEngine) registry_add(args DockerRegistryArgs) !

check docker has been installed & enabled on node

fn (DockerEngine) reset_all #

fn (mut e DockerEngine) reset_all() !

reset all images & containers, CAREFUL!

struct DockerEngineArgs #

@[params]
struct DockerEngineArgs {
pub mut:
	sshkeys_allowed []string // ssh keys which will be inserted when a docker gets deployed (is not implemented yet)
	name            string = 'default'
	localonly       bool   // do you build for local utilization only
	prefix          string // e.g. despiegk/ or myimage registry-host:5000/despiegk/) is added to the name when pushing	
}

struct DockerImage #

@[heap]
struct DockerImage {
pub mut:
	repo    string
	id      string
	tag     string
	digest  string
	size    int // size in MB
	created time.Time
	engine  &DockerEngine @[str: skip]
}

fn (DockerImage) delete #

fn (mut image DockerImage) delete(force bool) !

delete docker image

fn (DockerImage) export #

fn (mut image DockerImage) export(path string) !string

export docker image to tar.gz

fn (DockerImage) load #

fn (mut image DockerImage) load(path string) !

import docker image back into the local env

struct DockerRegistry #

@[heap]
struct DockerRegistry {
pub mut:
	name     string = 'default'
	datapath string
	ssl      bool
}

struct DockerRegistryArgs #

@[params]
struct DockerRegistryArgs {
pub mut:
	name      string = 'default' @[required]
	datapath  string
	ssl       bool
	reset     bool // if reset will reset existing running one
	reset_ssl bool // if reset will reset the sslkey
	secret    string = '1234' @[required]
}

struct DockerShellArgs #

@[params]
struct DockerShellArgs {
pub mut:
	cmd string
}

// open ssh shell to the cobtainer pub fn (mut container DockerContainer) ssh_shell(cmd string) ! { container.engine.node.shell(cmd)! }

struct DownloadArgs #

@[params]
struct DownloadArgs {
pub mut:
	name       string // unique name which will be used in the cache
	url        string
	reset      bool // will remove
	minsize_kb u32 = 10 // is always in kb
	maxsize_kb u32
	hash       string // if hash is known, will verify what hash is
	dest       string // if specified will copy to that destination	
	timeout    int = 180
	retry      int = 3
}

struct EntryPointArgs #

@[params]
struct EntryPointArgs {
pub mut:
	cmd string
}

import freeflowuniverse.crystallib.builder

struct EntryPointItem #

struct EntryPointItem {
pub mut:
	cmd    string
	recipe &DockerBuilderRecipe @[str: skip]
}

fn (EntryPointItem) check #

fn (mut i EntryPointItem) check() !

fn (EntryPointItem) render #

fn (mut i EntryPointItem) render() !string

struct EnvItem #

struct EnvItem {
pub mut:
	name  string
	value string
}

fn (EnvItem) check #

fn (mut i EnvItem) check() !

fn (EnvItem) render #

fn (mut i EnvItem) render() !string

struct ExecuteArgs #

@[params]
struct ExecuteArgs {
pub mut:
	source string // is the filename, needs to be embedded
	debug  bool
}

struct ExposeARgs #

@[params]
struct ExposeARgs {
pub mut:
	ports []string
}

struct ExposeItem #

struct ExposeItem {
pub mut:
	ports  []string
	recipe &DockerBuilderRecipe @[str: skip]
}

fn (ExposeItem) check #

fn (mut i ExposeItem) check() !

fn (ExposeItem) render #

fn (mut i ExposeItem) render() !string

struct FromArgs #

@[params]
struct FromArgs {
pub mut:
	image string
	tag   string
	alias string
}

struct FromItem #

struct FromItem {
pub mut:
	image  string
	tag    string
	recipe &DockerBuilderRecipe @[str: skip]
	alias  string
}

fn (FromItem) check #

fn (mut i FromItem) check() !

fn (FromItem) render #

fn (mut i FromItem) render() !string

struct GoBuildArgs #

@[params]
struct GoBuildArgs {
pub mut:
	url      string // e.g.  https://github.com/valeriansaliou/sonic
	pull     bool
	reset    bool
	buildcmd string = 'go run build.go '
	copycmd  string
	name     string
}

struct GoPackageArgs #

@[params]
struct GoPackageArgs {
pub mut:
	name    string // can be comma separated, can also be url e.g. github.com/caddyserver/xcaddy/cmd/xcaddy@latest
	postcmd string // normally empty
}

struct ImageGetArgs #

@[params]
struct ImageGetArgs {
pub:
	repo   string
	tag    string
	digest string
	id     string
}

struct ImageGetError #

struct ImageGetError {
	Error
pub:
	args     ImageGetArgs
	notfound bool
	toomany  bool
}

fn (ImageGetError) msg #

fn (err ImageGetError) msg() string

fn (ImageGetError) code #

fn (err ImageGetError) code() int

struct PackageArgs #

@[params]
struct PackageArgs {
pub mut:
	name  string
	names []string
}

struct PackageItem #

struct PackageItem {
pub mut:
	names    []string
	recipe   &DockerBuilderRecipe @[str: skip]
	platform PlatformType
}

fn (PackageItem) check #

fn (mut i PackageItem) check() !

fn (PackageItem) render #

fn (mut i PackageItem) render() !string

struct PortMap #

struct PortMap {
pub:
	indocker int
	host     int
}

struct RecipeArgs #

struct RecipeArgs {
pub:
	name   string
	prefix string // string (e.g. despiegk/ or myimage registry-host:5000/despiegk/) is added to the name when pushing
	// also exists on docker engine level, if not used here will come from the docker engine level
	tag      string //(default is 'latest' but can be a version nr)
	platform PlatformType
	zinit    bool = true
}

struct RunArgs #

@[params]
struct RunArgs {
pub mut:
	cmd string
}

struct RunItem #

struct RunItem {
pub mut:
	cmd    string
	recipe &DockerBuilderRecipe @[str: skip]
}

fn (RunItem) check #

fn (mut i RunItem) check() !

fn (RunItem) render #

fn (mut i RunItem) render() !string

struct RustBuildArgs #

@[params]
struct RustBuildArgs {
pub mut:
	url      string // e.g.  https://github.com/valeriansaliou/sonic
	pull     bool
	reset    bool
	buildcmd string = 'cargo build --release'
	copycmd  string
	debug    bool // to be able to easily debug the intermediate step
	name     string
}

import freeflowuniverse.crystallib.develop.gittools import freeflowuniverse.crystallib.core.pathlib

struct RustPackageArgs #

@[params]
struct RustPackageArgs {
pub mut:
	name    string // can be comma separated
	copycmd string // normally empty
}

struct VolumeArgs #

@[params]
struct VolumeArgs {
pub mut:
	mount_points []string
}

struct VolumeItem #

struct VolumeItem {
pub mut:
	mount_points []string
	recipe       &DockerBuilderRecipe @[str: skip]
}

fn (VolumeItem) check #

fn (mut i VolumeItem) check() !

fn (VolumeItem) render #

fn (mut i VolumeItem) render() !string

struct VolumeMap #

struct VolumeMap {
pub:
	hostpath      string
	containerpath string
}

fn (VolumeMap) check #

fn (mut vm VolumeMap) check() !

struct WorkDirArgs #

@[params]
struct WorkDirArgs {
pub mut:
	workdir string
}

struct WorkDirItem #

struct WorkDirItem {
pub mut:
	workdir string
	recipe  &DockerBuilderRecipe @[str: skip]
}

fn (WorkDirItem) check #

fn (mut i WorkDirItem) check() !

fn (WorkDirItem) render #

fn (mut i WorkDirItem) render() !string

struct WriteFileArgs #

@[params]
struct WriteFileArgs {
pub mut:
	name            string // such a file needs to have a name
	content         string
	dest            string // where the content will be saved
	make_executable bool   // if true then will be made executable
}

struct ZinitAddArgs #

@[params]
struct ZinitAddArgs {
pub mut:
	// more info see https://github.com/threefoldtech/zinit/tree/master/docs
	name    string
	exec    string // e.g. redis-server --port 7777
	test    string // e.g. redis-cli -p 7777 PING
	after   string // e.g. 'copy,tmuxp'  these are the names of the ones we are depending on, is comma separated
	log     ZinitLogType
	signal  string // could be 'SIGKILL' as well, if not specified then 'SIGTERM'
	oneshot bool   // is the command supposed to stop and only be used one, so not as daemon
}

struct ZinitItem #

struct ZinitItem {
pub mut:
	name    string
	exec    string   // e.g. redis-server --port 7777
	test    string   // e.g. redis-cli -p 7777 PING
	after   []string // e.g. copy,tmuxp  these are the names of the ones we are depending on
	log     ZinitLogType
	signal  string // could be 'SIGKILL' as well, if not specified then 'SIGTERM'
	oneshot bool   // is the command supposed to stop and only be used one, so not as daemon	
}

fn (ZinitItem) check #

fn (mut i ZinitItem) check() !

fn (ZinitItem) render #

fn (mut i ZinitItem) render() !string