iotmaker_docker_builder_golang_dockerfile

package module
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: May 23, 2023 License: Apache-2.0 Imports: 7 Imported by: 1

README

iotmaker.docker.builder.golang.dockerfile

https://github.com/helmutkemper/iotmaker.docker.builder

This module is a standard Golang Dockerfile generator used in the iotmaker.docker.builder project.

Este módulo é um gerador de Dockerfile padrão para Golang usado no projeto iotmaker.docker.builder.

iotmaker.docker.builder

Golang container generator to be integrated into golang codes in a simple and practical way, allowing the creation of integration tests in unit test format, or to write your own container manager.

Gerador de container em golang para ser integrado em códigos golang de forma simples e prática, permitindo a criação de testes de integração em formato de testes unitários, ou escrever seu próprio gerenciador de containers.

Sample code

package main

import (
  "fmt"
  iotmakerdocker "github.com/helmutkemper/iotmaker.docker/v1.0.1"
  "github.com/helmutkemper/util"
  "io/ioutil"
  "log"
  "net/http"
  "strings"
  "time"
)

func ImageBuildViewer(ch *chan iotmakerdocker.ContainerPullStatusSendToChannel) {
  go func(ch *chan iotmakerdocker.ContainerPullStatusSendToChannel) {
    for {

      select {
      case event := <-*ch:
        var stream = event.Stream
        stream = strings.ReplaceAll(stream, "\n", "")
        stream = strings.ReplaceAll(stream, "\r", "")
        stream = strings.Trim(stream, " ")

        if stream == "" {
          continue
        }

        log.Printf("%v", stream)

        if event.Closed == true {
          return
        }
      }
    }
  }(ch)
}

func main() {
  var err error

  var container = ContainerBuilder{}
  // new image name delete:latest
  container.SetImageName("delete:latest")
  // set a folder path to make a new image
  container.SetBuildFolderPath("./test")
  container.MakeDefaultDockerfileForMe()
  // container name container_delete_server_after_test
  container.SetContainerName("container_delete_server_after_test")
  // set a waits for the text to appear in the standard container output to proceed [optional]
  container.SetWaitStringWithTimeout("starting server at port 3000", 10*time.Second)
  // change and open port 3000 to 3030
  container.AddPortToOpen("3000")
  // replace container folder /static to host folder ./test/static
  err = container.AddFiileOrFolderToLinkBetweenConputerHostAndContainer("./test/static", "/static")
  if err != nil {
    panic(err)
  }

  // show image build stram on std out
  ImageBuildViewer(container.GetChannelEvent())

  // inicialize container object
  err = container.Init()
  if err != nil {
    panic(err)
  }

  // builder new image from folder
  err = container.ImageBuildFromFolder()
  if err != nil {
    panic(err)
  }

  // build a new container from image
  err = container.ContainerBuildFromImage()
  if err != nil {
    panic(err)
  }

  // Server is ready for use o port 3000

}
./test/static/index.html file
<!DOCTYPE html><html><body>server is running</body></html>
./test/go.mod file
module teste
go 1.16
./test/main.go
package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	fs := http.FileServer(http.Dir("./static"))
	http.Handle("/", fs)

	fmt.Printf("starting server at port 3000\n")
	err := http.ListenAndServe(":3000", nil)
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangePort added in v1.0.1

type ChangePort struct {
	OldPort string
	NewPort string
}

ChangePort

English:

Receives the relationship between ports to be exchanged

 oldPort: porta original da imagem
 newPort: porta a exporta na rede

Português:

Recebe a relação entre portas a serem trocadas

 oldPort: porta original da imagem
 newPort: porta a exporta na rede

type Copy added in v1.0.17

type Copy struct {
	Src string
	Dst string
}

type DockerfileGolang

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

func (*DockerfileGolang) AddCopyToFinalImage added in v1.0.17

func (e *DockerfileGolang) AddCopyToFinalImage(src, dst string)

AddCopyToFinalImage

English:

Add one instruction 'COPY --from=builder /app/`dst` `src`' to final image builder.

Português:

Adiciona uma instrução 'COPY --from=builder /app/`dst` `src`' ao builder da imagem final.

func (*DockerfileGolang) DisableScratch added in v1.0.14

func (e *DockerfileGolang) DisableScratch()

DisableScratch

English:

Change final docker image from scratch to image name set in SetFinalImageName() function

Português:

Troca a imagem final do docker de scratch para o nome da imagem definida na função
SetFinalImageName()

func (*DockerfileGolang) MountDefaultDockerfile

func (e *DockerfileGolang) MountDefaultDockerfile(
	args map[string]*string,
	changePorts []ChangePort,
	openPorts []string,
	exposePorts []string,
	volumes []mount.Mount,
	installExtraPackages bool,
	useCache bool,
	imageCacheName string,
) (
	dockerfile string,
	err error,
)

MountDefaultDockerfile

English:

Build a default dockerfile for image

 Input:
   args: list of environment variables used in the container
   changePorts: list of ports to be exposed on the network, used in the original image that should be changed. E.g.: 27017 to 27016
   openPorts: list of ports to be exposed on the network
   exposePorts: list of ports to just be added to the project's dockerfile
   volumes: list of folders and files with permission to share between the container and the host.

 Output:
   dockerfile: string containing the project's dockerfile
   err: standard error object

Português:

Monta o dockerfile padrão para a imagem

 Entrada:
   args: lista de variáveis de ambiente usadas no container
   changePorts: lista de portas a serem expostas na rede, usadas na imagem original que devem ser trocadas. Ex.: 27017 para 27016
   openPorts: lista de portas a serem expostas na rede
   exposePorts: lista de portas a serem apenas adicionadas ao dockerfile do projeto
   volumes: lista de pastas e arquivos com permissão de compartilhamento entre o container e o hospedeiro.

 Saída:
   dockerfile: string contendo o dockerfile do projeto
   err: objeto de erro padrão

func (*DockerfileGolang) Prayer added in v1.0.4

func (e *DockerfileGolang) Prayer()

Prayer

English:

Programmer prayer.

Português:

Oração do programador.

func (*DockerfileGolang) SetDefaultSshFileName added in v1.0.19

func (e *DockerfileGolang) SetDefaultSshFileName(name string)

SetDefaultSshFileName

English:

Sets the name of the file used as the ssh key.

Português:

Define o nome do arquivo usado como chave ssh.

func (*DockerfileGolang) SetFinalImageName added in v1.0.15

func (e *DockerfileGolang) SetFinalImageName(name string)

SetFinalImageName

English:

Set a two stage build final image name

Português:

Define o nome da imagem final para construção de imagem em dois estágios.

func (*DockerfileGolang) SetGolangSrc added in v1.0.21

func (e *DockerfileGolang) SetGolangSrc(dst, src string)

SetGolangSrc

English:

Define the destination and source files for go build command e.g. SetGolangSrc("/app/main", "/app/main.go"): RUN go build -ldflags="-w -s" -o (dst) /app/main (src) /app/main.go

Português:

Define os arquivos de destino e fonte para o comando go build, ex.: SetGolangSrc("/app/main", "/app/main.go"): RUN go build -ldflags="-w -s" -o (dst) /app/main (src) /app/main.go

func (*DockerfileGolang) SetWorkDir added in v1.0.21

func (e *DockerfileGolang) SetWorkDir(dir string)

SetWorkDir

English:

Define work dir. e.g. /app

Português:

Define o diretório de trabalho

Jump to

Keyboard shortcuts

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