docen

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2021 License: MIT Imports: 10 Imported by: 0

README

Go Report Card

Docen

Simple utility for generating Dockerfile for golang projects.

Installation

go get github.com/lobz1g/docen

Usage

package main

import (
	"log"

	"github.com/lobz1g/docen"
)

func main() {
	err := docen.New().
		SetGoVersion("1.14.9").
		SetPort("3000").
		SetTimezone("Europe/Moscow").
		SetTestMode(true).
		SetAdditionalFolder("my-folder/some-files").
		SetAdditionalFile("another-folder/some-files/file").
		GenerateDockerfile()
	if err != nil {
		log.Fatal(err)
	}
}

Dockerfile will be created in the root dir of the project.

FROM golang:1.14.9-alpine as builder
RUN apk update && apk add --no-cache git ca-certificates tzdata && update-ca-certificates
RUN adduser -D -g '' appuser
RUN mkdir -p /docen
RUN mkdir -p /docen/my-folder/some-files
RUN mkdir -p /docen/another-folder/some-files
COPY . /docen
WORKDIR /docen
RUN CGO_ENABLED=0 go test ./...
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build  -ldflags="-w -s" -o /docen
FROM scratch
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
ENV TZ=Europe/Moscow
COPY --from=builder /docen /docen
COPY --from=builder /docen/my-folder/some-files /docen/my-folder/some-files
COPY --from=builder /docen/another-folder/some-files /docen/another-folder/some-files
COPY --from=builder /docen/another-folder/some-files/file /docen/another-folder/some-files/file
USER appuser
EXPOSE 3000
ENTRYPOINT ["/docen"]

Options

All methods are optional. That means there are default values for success creating Dockerfile without any settings.

Just use below code for generating Dockerfile with default values:

err := docen.New().GenerateDockerfile()
if err != nil {
    log.Fatal(err)
}
Image

By default, the image name is golang and the tag is {your_golang_version}-aplpine. If the runtime.Version method returns wrong information about your golang version it will be just alpine image tag. You can set the version by method SetGoVersion without any settings.

Expose port

By default, Dockerfile will be without the expose port field. You can set the port by method SetPort. The argument can be a single value of port, for example 3000, or a range of values, for example 3000-4000.

Timezone

By default, Dockerfile will be without the timezone env field. You can set the timezone by method SetTimezone.

Testing

The image is built without testing, but you can test the app before build. Use the method SetTestMode for it.

Additional folders to image

Such folders as assets, config, static and templates are added to the image. You can add additional folders by method SetAdditionalFolder.

Additional files to image

You can set additional files which should be added to the image. Use the SetAdditionalFile method for it. It also adds additional folders for these files.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Docen

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

func New

func New() *Docen

New method creates new instance of generator. By default, the golang version is taken from runtime.Version By default, additional folders are `static`, `templates`, `config` and `assets`.

Example
err := docen.New().
	SetGoVersion("1.14.9").
	SetPort("3000").
	SetTimezone("Europe/Moscow").
	SetTestMode(true).
	SetAdditionalFolder("my-folder/some-files").
	SetAdditionalFile("another-folder/some-files/file").
	GenerateDockerfile()
if err != nil {
	log.Fatal(err)
}
Output:

func (*Docen) GenerateDockerfile

func (d *Docen) GenerateDockerfile() error

GenerateDockerfile method creates Dockerfile file. If vendor mode is enabled then building will be with `-mod=vendor` tag.

Example
err := docen.New().GenerateDockerfile()
if err != nil {
	log.Fatal(err)
}
Output:

func (*Docen) SetAdditionalFile

func (d *Docen) SetAdditionalFile(path string) *Docen

SetAdditionalFolder method allows you to set additional files which will be added to a container.

Example
docen.New().SetAdditionalFile("my-folder/some-files/file")
Output:

func (*Docen) SetAdditionalFolder

func (d *Docen) SetAdditionalFolder(path string) *Docen

SetAdditionalFolder method allows you to set additional folders which will be added to a container.

Example
docen.New().SetAdditionalFolder("my-folder/some-files")
Output:

func (*Docen) SetGoVersion

func (d *Docen) SetGoVersion(version string) *Docen

SetGoVersion method allows you to set a specific version of golang.

Example
docen.New().SetGoVersion("1.14.9")
Output:

func (*Docen) SetPort

func (d *Docen) SetPort(port string) *Docen

SetPort method allows you to set an exposed port. It can be as single port as a range of ports.

Example
docen.New().SetPort("3000-4000")
Output:

func (*Docen) SetTestMode

func (d *Docen) SetTestMode(mode bool) *Docen

SetTestMode method allows you to enable testing before starting to build the app.

Example
docen.New().SetTestMode(true)
Output:

func (*Docen) SetTimezone

func (d *Docen) SetTimezone(timezone string) *Docen

SetTimezone method allows you to set a specific timezone.

Example
docen.New().SetTimezone("Europe/Paris")
Output:

Jump to

Keyboard shortcuts

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