Slide 1

Slide 1 text

Starting Docker with Golang Go Conference 2018 Spring @sakajunquality

Slide 2

Slide 2 text

Are you using Docker?

Slide 3

Slide 3 text

Probably Yes

Slide 4

Slide 4 text

Using docker for... - Building a Go app - Running a Go app - Developing a Go app

Slide 5

Slide 5 text

Probably you know difference between container and virtualization?

Slide 6

Slide 6 text

Container and VM - https://www.docker.com/what-container#/virtual_machines

Slide 7

Slide 7 text

Let’s just say that containers are less overhead

Slide 8

Slide 8 text

(Generally speaking)

Slide 9

Slide 9 text

What’s Container?

Slide 10

Slide 10 text

Container is... - Using linux namespace, separate resources, like CPU, Memory, File System etc. for each process - Docker is a one implementation of container

Slide 11

Slide 11 text

What’s Docker?

Slide 12

Slide 12 text

Docker is... - A runtime for application using container https://www.docker.com/what-docker

Slide 13

Slide 13 text

Docker ≠ Container

Slide 14

Slide 14 text

containerd and runC

Slide 15

Slide 15 text

- Runtime engine for docker - Used to be within Docker containerd and runC https://containerd.io/

Slide 16

Slide 16 text

containerd and runC https://containerd.io/

Slide 17

Slide 17 text

Forget this for now

Slide 18

Slide 18 text

containerd and runC in a nutshell Docker / moby containerd runC Linux Kernel (cgroup etc.)

Slide 19

Slide 19 text

containerd and runC in a nutshell Docker / moby containerd runC OCI standard Something else (maybe)

Slide 20

Slide 20 text

$ go get github.com/opencontainers/runc $ cd $GOPATH/src/github.com/opencontainers/runc $ make // compile $ sudo make install // path Install runC Note: tested on arch linux

Slide 21

Slide 21 text

$ mkdir ~/mygolangcontainer $ cd ~/mygolangcontainer $ mkdir rootfs $ docker export $(docker create golang:1.10.1) | tar -C rootfs -xvf - $ runc spec $ vim config.json // add PATH /go/bin:/usr/local/go/bin $ sudo runc run mygolangcontainer # which go /usr/local/go/bin/go # go Go is a tool for managing Go source code. ... Run container without docker Note: tested on arch linux

Slide 22

Slide 22 text

You can run container without docker

Slide 23

Slide 23 text

You can embed container into application

Slide 24

Slide 24 text

Implementation example in golang

Slide 25

Slide 25 text

:dogeza:

Slide 26

Slide 26 text

but

Slide 27

Slide 27 text

contained has plenty of examples https://github.com/containerd/containerd

Slide 28

Slide 28 text

and

Slide 29

Slide 29 text

Container is made of golang (almost)

Slide 30

Slide 30 text

Almost

Slide 31

Slide 31 text

Let’s go back to the real world

Slide 32

Slide 32 text

Most of us will not use container directly

Slide 33

Slide 33 text

Tips for Running and Building Golang app in Docker

Slide 34

Slide 34 text

Take a look at Dockerhub Golang https://hub.docker.com/_/golang/

Slide 35

Slide 35 text

package main import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "GoCon 2018 Spring") } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8888", nil) } Using simple http app

Slide 36

Slide 36 text

Let’s follow the official example FROM golang:1.10.1 WORKDIR /go/src/github.com/sakajunquality/hello-go-docker COPY main.go main.go RUN go get -d -v ./... RUN go install -v ./... CMD ["hello-go-docker"]

Slide 37

Slide 37 text

It works $ docker build --no-cache --rm -t hello-docker:v1 . $ docker run -d -p 8888:8888 hello-docker:v1 $ curl localhost:8888 > GoCon 2018 Spring

Slide 38

Slide 38 text

But the image is too big $ docker images | grep hello-docker REPOSITORY TAG IMAGE ID CREATED SIZE hello-docker v1 77867ff7da90 About an hour ago 787MB

Slide 39

Slide 39 text

Let’s use alpine FROM golang:1.10.1-alpine WORKDIR /go/src/github.com/sakajunquality/hello-go-docker COPY main.go main.go RUN go get -d -v ./... RUN go install -v ./... CMD ["hello-go-docker"]

Slide 40

Slide 40 text

Still the image is big $ docker images | grep hello-docker REPOSITORY TAG IMAGE ID CREATED SIZE hello-docker v2 6b84ba9a4a72 About an hour ago 382MB hello-docker v1 77867ff7da90 About an hour ago 787MB

Slide 41

Slide 41 text

Why? $ docker images | grep golang REPOSITORY TAG IMAGE ID CREATED SIZE golang 1.10.1-alpine 52d894fca6d4 2 days ago 376MB golang 1.10.1 6fe15d4cbc64 2 weeks ago 780MB

Slide 42

Slide 42 text

Diff $ docker images | grep golang REPOSITORY TAG IMAGE ID CREATED SIZE hello-docker v2 6b84ba9a4a72 About an hour ago 382MB golang 1.10.1-alpine 52d894fca6d4 2 days ago 376MB hello-docker v1 77867ff7da90 About an hour ago 787MB golang 1.10.1 6fe15d4cbc64 2 weeks ago 780MB 6MB 7MB

Slide 43

Slide 43 text

For running golang app - Golang itself is not required - Source code is also not required

Slide 44

Slide 44 text

Separate build image and runtime image

Slide 45

Slide 45 text

Let’s use multi stage build FROM golang:1.10.1-alpine as build WORKDIR /go/src/github.com/sakajunquality/hello-go-docker COPY main.go main.go RUN go get -d -v ./... RUN go install -v ./... FROM alpine RUN apk add --no-cache ca-certificates COPY --from=build /go/bin/hello-go-docker /usr/local/bin/hello-go-docker CMD ["hello-go-docker"]

Slide 46

Slide 46 text

Finally the image is small enough $ docker images | grep hello-docker REPOSITORY TAG IMAGE ID CREATED SIZE hello-docker v3 a5048da4d653 23 minutes ago 11.3MB hello-docker v2 6b84ba9a4a72 About an hour ago 382MB hello-docker v1 77867ff7da90 About an hour ago 787MB

Slide 47

Slide 47 text

If you need a more small image... - https://github.com/tetsu-koba/minimumgo $ docker export $(docker create alpine) | tar -C rootfs -xvf -ls $ ls rootfs bin dev etc home lib media mnt proc root run sbin srv sys tmp usr var

Slide 48

Slide 48 text

If you need a static build... FROM golang:1.10.1-alpine as build … RUN go install … FROM node:9 as static-build … RUN yarn install … FROM alpine … COPY --from=build /go/bin/myapp /usr/local/bin/myapp COPY --from=build-static /path/to/myapp/static /usr/local/src/myapp/static CMD ["myapp"]

Slide 49

Slide 49 text

Conclusion

Slide 50

Slide 50 text

<3

Slide 51

Slide 51 text

Thank you