Starting Docker with go

Starting Docker with go

#gocon 2018 spring LT

923237754890d536819892ad42451555?s=128

sakajunquality

April 15, 2018
Tweet

Transcript

  1. Starting Docker with Golang Go Conference 2018 Spring @sakajunquality

  2. Are you using Docker?

  3. Probably Yes

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

    a Go app - Developing a Go app
  5. Probably you know difference between container and virtualization?

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

  7. Let’s just say that containers are less overhead

  8. (Generally speaking)

  9. What’s Container?

  10. Container is... - Using linux namespace, separate resources, like CPU,

    Memory, File System etc. for each process - Docker is a one implementation of container
  11. What’s Docker?

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

  13. Docker ≠ Container

  14. containerd and runC

  15. - Runtime engine for docker - Used to be within

    Docker containerd and runC https://containerd.io/
  16. containerd and runC https://containerd.io/

  17. Forget this for now

  18. containerd and runC in a nutshell Docker / moby containerd

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

    runC OCI standard Something else (maybe)
  20. $ 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
  21. $ 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
  22. You can run container without docker

  23. You can embed container into application

  24. Implementation example in golang

  25. :dogeza:

  26. but

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

  28. and

  29. Container is made of golang (almost)

  30. Almost

  31. Let’s go back to the real world

  32. Most of us will not use container directly

  33. Tips for Running and Building Golang app in Docker

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

  35. 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
  36. 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"]
  37. 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
  38. 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
  39. 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"]
  40. 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
  41. 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
  42. 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
  43. For running golang app - Golang itself is not required

    - Source code is also not required
  44. Separate build image and runtime image

  45. 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"]
  46. 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
  47. 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
  48. 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"]
  49. Conclusion

  50. <3

  51. Thank you