The power of docker's MICRO images, a Golang journey Vol.1

4 min read15 Sep 2019

I know, It might be difficult sometimes, the process of building your images with any kind of constrain (monetary, computational, size...) was always kept me awake at night.

Docker multistage builds

After all building a super small image is always a good idea. It doesn't matter the outcome, your client might be happier spending less resources, you might be happier to have more space to deploy more services in the same instance or cluster...

Docker has been around for some time now and you probably know already the goodies of it (check the platform journey series to see how ECS can handle your containers).

In this article we are going to work with Docker's multistage builds as well as some tricks and hacks for scratch images. Here we will set the entry point for the Golang journey series.

FROM golang:latest

WORKDIR /go/src/app
COPY . .

RUN go get -d -v ./...
RUN go install -v ./...

CMD ["app"]

FROM scratch

Most Dockerfiles start from a parent image. The scratch image is the most minimal image in Docker. This is the base ancestor for all other images.

There are some reasons to go for an image from scratch beside the size of the resulting size image.

Attack surface is less provable because we reduce the dependencies (layers) on other images, meaning what's really needed to execute your application is in the image. Handling the required dependencies ourselves for our application is the price we pay for the goodies of a base image.

The pipeline

How does a pipeline looks like?

You can learn more about it in this article

Let's assume our pipeline involves upload and tag an image into AWS ECR. After I commit my changes into a repository I get all the feedback about my code analysis and tests in a production like environment.

A basic travis pipeline would looks like the following:

dist: trusty
sudo: true
language: go
go:
- 1.12.x
services:
- docker
env:
  global:
  - IMAGE_NAME=api-jorgechato-com
  - secure: cHNC4ysYBXJdVyod42z3chXLH0V7n231fPeLjDjpYiW2OekAYKcIxIa+egSnXT9VdFQZt/vCNw4t9d5/6d/9zyn3sdBj7ll+zpB+OFdFzd2wey4e8U1ZTa2DLHcWs/CERnbsP1YWw0NHi/FQ771OavskGoDa8CM6K3TtquVMGyefB6V80XC3OJNHZXcCgfA+qbhGOdHQIbcIhU7d0vbJDveuye4Saq+MVltxPeaXRQbUlZmQisvlzSgabtfvDC0nQWy5v+9YJUGIGTWj15/q0aDpVkiE2ehqlj5G5d8Tl3RpqXEb15Dzrh93K9mBP0LnmlGs1eNZ+rJZ1H067yeE76+Fgkp14aD/WZxjZ0sY41W+eS0ySx6Gv1UoDm5658O2PTpTgR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fJ3iwaiHu8GoigWLxtkiry8uBLeYLmjF5JneMZC4knEpuDGAnMo7VvI8IMBZAGL57aaHOQ3wG+gXGk15YJTiDYpB0SXWvco5c3zcMT0bvwmqLUihUMJiRNqnP3pu99rRAD7dnA65NARQjM99Gd4VASpMKESUutTCfVTeN2JjwVA9cdOMPW9u2ZwxHhaluFMvm4X4A2KyJaLmwRP6tyWSdQs=

before_install:
- pip install --user awscli
- export PATH=$PATH:$HOME/.local/bin
install:
- go mod download
- go mod verify

script:
- go test -race -cover -v -coverprofile=coverage.out ./... > test.output
after_success:
- docker build --pull --cache-from ${IMAGE_NAME} --tag ${IMAGE_NAME} .

deploy:
  provider: script
  script: bash lazy.sh
  target-branch: master
  on:
    branch: master
after_script:
- docker images

Where the lazy.sh is nothing but a registry tag and upload.

#!/bin/bash -e

REGISTRY_URL=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com

aws configure set default.region ${AWS_REGION}

$(aws ecr get-login --no-include-email)

docker tag ${IMAGE_NAME} ${REGISTRY_URL}/${IMAGE_NAME}:latest

docker push ${REGISTRY_URL}/${IMAGE_NAME}:latest

If we dive into the Dockerfile we will find two FROM statement in a single file.

FROM golang:1.12-alpine as builder

RUN apk add -U --no-cache git ca-certificates tzdata

COPY . /app
WORKDIR /app

RUN go mod download
RUN go mod verify

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="${LDFLAGS}" -o server server.go


FROM scratch as final

ENV GIN_MODE=release

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /app/server /opt/server

ENTRYPOINT ["/opt/server"]
$ docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
api-jorgechato-com            latest              6ce8bbe28d7e        19 seconds ago      21.2MB
<none>                        <none>              d39b7d2094c1        20 seconds ago      567MB
golang                        1.12-alpine         e0d646523991        5 days ago          350MB

Having a look at the images created, golang:1.12-alpine is the base image that we used to build or service. The residual image is tag as and comparing the size of it with the service image api-jorgechato-com, the difference is huge.

Since the scratch image is dependences free you might end up with errors you have never experience in a development environment. There is a need to be extra cautious about what your service required, SSL request (ca-certificates), current time (zoneinfo)... At the end you are building your environment from SCRATCH.