Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Starting Docker with go

Starting Docker with go

#gocon 2018 spring LT

sakajunquality

April 15, 2018
Tweet

More Decks by sakajunquality

Other Decks in Technology

Transcript

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

    a Go app - Developing a Go app
  2. Container is... - Using linux namespace, separate resources, like CPU,

    Memory, File System etc. for each process - Docker is a one implementation of container
  3. - Runtime engine for docker - Used to be within

    Docker containerd and runC https://containerd.io/
  4. containerd and runC in a nutshell Docker / moby containerd

    runC OCI standard Something else (maybe)
  5. $ 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. $ 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. but

  8. and

  9. 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
  10. 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"]
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. For running golang app - Golang itself is not required

    - Source code is also not required
  17. 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"]
  18. 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
  19. 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
  20. 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"]
  21. <3