Starting Docker with go

Starting Docker with go

#gocon 2018 spring LT

923237754890d536819892ad42451555?s=128

sakajunquality

April 15, 2018
Tweet

Transcript

  1. 4.

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

    a Go app - Developing a Go app
  2. 10.

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

    Memory, File System etc. for each process - Docker is a one implementation of container
  3. 15.

    - Runtime engine for docker - Used to be within

    Docker containerd and runC https://containerd.io/
  4. 19.

    containerd and runC in a nutshell Docker / moby containerd

    runC OCI standard Something else (maybe)
  5. 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
  6. 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
  7. 25.
  8. 26.

    but

  9. 28.

    and

  10. 30.
  11. 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
  12. 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"]
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 43.

    For running golang app - Golang itself is not required

    - Source code is also not required
  19. 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"]
  20. 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
  21. 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
  22. 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"]
  23. 50.

    <3

  24. 51.