Building GoLang With Docker

Johnny Estilles

GoManilaPh

October 2018

GoManilaPh

October 2018

Johnny Estilles

Regional Director of Engineering
Freelancer.com

@JohnnyEstilles

johnny@estilles.com

GoManilaPh

October 2018

What is Docker anyway?

GoManilaPh

October 2018

... a computer program that performs operating-system-level virtualization, also known as  containerization.

What is Docker?

GoManilaPh

October 2018

Linux features used by Docker

  • kernel namespaces

  • cgroups

  • union file systems

GoManilaPh

October 2018

Linux kernel namespaces

namespace purpose
PID process isolation
NET managing network interfaces
IPC managing access to IPC resources
MNT managing filesystem mount points
UTS isolating kernel and version identifiers

GoManilaPh

October 2018

Linux cgroups

cgroup purpose
Memory managing accounting, limits and notifications
HugeTBL accounting usage of huge pages by process group
CPU managing user / system CPU time and usage
CPUSet binding a group to specific CPU
net_cls/net_prio tagging the traffic control
Devices reading / writing access devices
Freezer freezing a group

GoManilaPh

October 2018

Union-capable File Systems

  • Filesystem Isolation (rootfs/chroot)

  • Layering

  • Copy-on-Write

  • Caching

  • Diff'ing

GoManilaPh

October 2018

GoManilaPh

October 2018

GoManilaPh

October 2018

Docker Images vs Containers

GoManilaPh

October 2018

package main

import (
	"fmt"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "<h1>Hello GoManilaPh</h1>")
}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

main.go

GoManilaPh

October 2018

GoLang Images in Docker Hub

GoManilaPh

October 2018

Dockerfile (basic)

FROM golang:1.11.1

COPY . /src

WORKDIR /src

RUN go build

EXPOSE 8080

CMD /app/golang-docker

GoManilaPh

October 2018

Dockerfile (multi step)

FROM golang:1.11.1-alpine as builder

RUN apk update && apk add g++ gcc make musl

COPY . /src

WORKDIR /src

RUN go build -o entrypoint

FROM alpine

COPY --from=builder /src/entrypoint /entrypoint

EXPOSE 8080

ENTRYPOINT [ "/entrypoint" ]

GoManilaPh

October 2018

Dockerfile (from scratch)

FROM golang:1.11.1 as builder

COPY . /src

WORKDIR /src

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \
  go build -o entrypoint -ldflags '-w -s' main.go

FROM scratch

COPY --from=builder /src/entrypoint /entrypoint

EXPOSE 8080

ENTRYPOINT [ "/entrypoint" ]

GoManilaPh

October 2018

References

Building GoLang projects using Docker

By Johnny Estilles

Building GoLang projects using Docker

  • 293