博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
创建最小的golang容器镜像
阅读量:6209 次
发布时间:2019-06-21

本文共 4375 字,大约阅读时间需要 14 分钟。

hot3.png

Create the smallest and secured golang docker image based on scratch

When we are building a docker Image, the first idea is using the default official image.

FROM golangFROM nginxFROM openjdk

There is an .

$ docker image listgolang    latest       1c1309ff8e0d        10 days ago         779MB

Ouch 779 MB just for an empty image … this is crazy ?

There is lightweight Alpine .

Check the  for more informations.

FROM golang:alpine

This is smaller but too large for a production image with nothing

$ docker image listgolang     alpine      bbab7aea1231        7 weeks ago         269MB

Use Multi-stage builds

Thanks to docker multi-stage builds, we can build our application in a docker alpine image an produce a small image with only a binary in a scratch image.

OK, it’s time to build a smaller image with multi-stage build

Before that we gonna see , only 2MB image. Perfect for our go static binary.

# STEP 1 build executable binary
FROM golang:alpine as builderCOPY . $GOPATH/src/mypackage/myapp/WORKDIR $GOPATH/src/mypackage/myapp/
#get dependanciesRUN go get -d -v
#build the binaryRUN go build -o /go/bin/hello
# STEP 2 build a small image
# start from scratchFROM scratch
# Copy our static executableCOPY --from=builder /go/bin/hello /go/bin/helloENTRYPOINT ["/go/bin/hello"]

Oh cool only 21.2MB with everything i need for my go app.

$ docker image listhello    latest        bbab7aea1234       3 hours ago         21.2MB

But we can optimize it, by removing debug informations and compile only for linux target and disabling cross compilation.

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags=”-w -s” -o /go/bin/backoffice

Now we have a small image only 13.6MB, almost ready for production

$ docker image listhello    latest        bbab7aea1234       2hours ago         13.6MB

Let’s build a more secure docker image

Just some reminders :

  • Run only one process in a container.
  • Never run a process as root in a container.
  • Never store data in a container, do it in a volume
  • Never store credentials in a container, do it in a volume
  • Keep your image up to date
  • Verify third-party container repositories
  • Use tool like 
  • May the force be with you ?

OK, let’s do that with scratch image.

We have to create a new user on the builder image and copy the /etc/passwd file from the builder to te scratch image. Finally we can use appuser to launch the binary.

# STEP 1 build executable binary
FROM golang:alpine as builder
# Create appuserRUN adduser -D -g '' appuser
COPY . $GOPATH/src/mypackage/myapp/WORKDIR $GOPATH/src/mypackage/myapp/
#get dependanciesRUN go get -d -v
#build the binaryRUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags=”-w -s” -o /go/bin/backoffice
# STEP 2 build a small image
# start from scratchFROM scratch
COPY --from=builder /etc/passwd /etc/passwd# Copy our static executableCOPY --from=builder /go/bin/hello /go/bin/helloUSER appuser
ENTRYPOINT ["/go/bin/hello"]

Always export with port > 1024 as possible

OK, now we have a more secure image. But if we expose our docker with a port < 1024, we need some privileges for that.

OK so let’s expose always our binary with a port > 1024

COPY --from=builder /etc/passwd /etc/passwd# Copy our static executableCOPY --from=builder /go/bin/hello /go/bin/helloUSER appuser
EXPOSE 9292ENTRYPOINT ["/go/bin/hello"]

Add SSL ca certificates

Perfect, but to be secure we need to expose our services with SSL isn’t it ?

By default scratch image is not provided with SSL CA certificates. But with multi-step we can provide it.

# STEP 1 build executable binary
FROM golang:alpine as builder
# Install SSL ca certificatesRUN apk update && apk add git && apk add ca-certificates
# Create appuserRUN adduser -D -g '' appuser
COPY . $GOPATH/src/mypackage/myapp/WORKDIR $GOPATH/src/mypackage/myapp/
#get dependanciesRUN go get -d -v
#build the binaryRUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags=”-w -s” -o /go/bin/backoffice
# STEP 2 build a small image
# start from scratchFROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/COPY --from=builder /etc/passwd /etc/passwd# Copy our static executableCOPY --from=builder /go/bin/hello /go/bin/helloUSER appuser
ENTRYPOINT ["/go/bin/hello"]
If you have any advice about security, please let me know :)

转载于:https://my.oschina.net/u/2306127/blog/1628935

你可能感兴趣的文章
《C语言深度剖析》学习笔记三
查看>>
自主做一个类似于微博的项目(计划篇)
查看>>
深入理解Java:注解(Annotation)
查看>>
Erlang并发机制 –进程调度
查看>>
Java环境搭建若干问题
查看>>
VBA and Access
查看>>
不可不说的Java“锁”事
查看>>
分布式搜索elasticsearch配置文件详解
查看>>
ElasticSearch
查看>>
postman 请求参数为数组及JsonObject
查看>>
XEN--转载自鸟哥的linux私房菜
查看>>
Android 自定义View实现画背景和前景(ViewGroup篇)
查看>>
keepalived实现高可用nginx反向代理的简单案例
查看>>
DPM 2012 SP1---安装并部署DPM 2012 SP1服务器
查看>>
memcache+apache+tomcat(提供软件包)
查看>>
我的专业博客启动了!
查看>>
工业控制系统专业术语(不断完善中)
查看>>
面试经验谈架构
查看>>
搭建hadoop开发环境--基于xp+cygwin
查看>>
MySql中的varchar类型
查看>>