Skip to content

Instantly share code, notes, and snippets.

@Anunayj
Last active February 27, 2021 22:19
Show Gist options
  • Save Anunayj/f58463793ef3902eb4d0f4a24ce8b875 to your computer and use it in GitHub Desktop.
Save Anunayj/f58463793ef3902eb4d0f4a24ce8b875 to your computer and use it in GitHub Desktop.
Build Letsdane statically
# DOCKER_BUILDKIT=1 docker build -f static.Dockerfile -o build/ .
FROM golang:alpine AS base
RUN apk update && apk add linux-headers gcc make perl musl-dev expat-dev
#Install Openssl
FROM base as setup-openssl
WORKDIR /tmp
RUN wget https://www.openssl.org/source/openssl-1.1.1j.tar.gz && tar -xzf openssl-1.1.1j.tar.gz
WORKDIR /tmp/openssl-1.1.1j
RUN ./Configure linux-x86_64 --prefix=/opt/install
RUN make && make install
#Install Unbound
FROM setup-openssl as setup-unbound
COPY --from=setup-openssl /opt/install/ /usr/local
WORKDIR /tmp
RUN wget https://www.nlnetlabs.nl/downloads/unbound/unbound-1.13.1.tar.gz && tar -xzf unbound-1.13.1.tar.gz
WORKDIR /tmp/unbound-1.13.1
RUN ./configure --prefix=/opt/install
RUN make && make install
FROM base as builder
#Install Dependencies
COPY --from=setup-unbound /opt/install/ /usr/local
#Optionally export this layer and cache this permanently somewhere.
#Copy stuff overgo mod download
WORKDIR /tmp/dane
COPY go.mod /tmp/dane/go.mod
RUN go mod download
#Will allow caching dependencies in layers.
COPY . /tmp/dane/
WORKDIR /tmp/dane/cmd/letsdane
#Build Static
RUN go build -tags unbound --ldflags '-extldflags "-lunbound -lssl -lcrypto -static"'
FROM scratch
COPY --from=builder /tmp/dane/cmd/letsdane/letsdane /
ENTRYPOINT [ "letsdane" ]
@Anunayj
Copy link
Author

Anunayj commented Feb 27, 2021

Or use this intermediate image

FROM golang:alpine AS base
RUN apk update && apk add linux-headers gcc make perl musl-dev expat-dev


#Install Openssl
FROM base as setup-openssl
WORKDIR /tmp

RUN wget https://www.openssl.org/source/openssl-1.1.1j.tar.gz && tar -xzf openssl-1.1.1j.tar.gz
WORKDIR /tmp/openssl-1.1.1j
RUN ./Configure linux-x86_64 --prefix=/opt/install
RUN make && make install


#Install Unbound
FROM setup-openssl as setup-unbound
COPY --from=setup-openssl /opt/install/ /usr/local
WORKDIR /tmp
RUN wget https://www.nlnetlabs.nl/downloads/unbound/unbound-1.13.1.tar.gz && tar -xzf unbound-1.13.1.tar.gz
WORKDIR /tmp/unbound-1.13.1
RUN ./configure --prefix=/opt/install
RUN make && make install

FROM base as builder
#Install Dependencies
COPY --from=setup-unbound /opt/install/ /usr/local
COPY --from=setup-openssl /opt/install/ /usr/local

and build with this

FROM anunayj/golang-libunbound@sha256:4db0797175be0d38f0a65f81517d1862a72c745cfb42d53b09aed477f00d6e5d as builder
#Copy stuff mod download
WORKDIR /tmp/dane
COPY go.mod /tmp/dane/go.mod
RUN go mod download
#Will allow caching dependencies in layers.

COPY . /tmp/dane/
WORKDIR /tmp/dane/cmd/letsdane
#Build Static
RUN go build -tags unbound --ldflags '-extldflags "-lunbound -lssl -lcrypto -static"'

FROM scratch
COPY --from=builder /tmp/dane/cmd/letsdane/letsdane /
ENTRYPOINT [ "letsdane" ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment