| 1 |
# Stage 1: Build libvirt exporter |
| 2 |
FROM golang:alpine |
| 3 |
|
| 4 |
# Install dependencies |
| 5 |
RUN apk add --update git gcc g++ make libc-dev portablexdr-dev linux-headers libnl-dev perl libtirpc-dev pkgconfig wget |
| 6 |
RUN wget ftp://xmlsoft.org/libxml2/libxml2-2.9.4.tar.gz -P /tmp && \ |
| 7 |
tar -xf /tmp/libxml2-2.9.4.tar.gz -C /tmp |
| 8 |
WORKDIR /tmp/libxml2-2.9.4 |
| 9 |
RUN ./configure --disable-shared --enable-static && \ |
| 10 |
make -j2 && \ |
| 11 |
make install |
| 12 |
RUN wget https://libvirt.org/sources/libvirt-3.2.0.tar.xz -P /tmp && \ |
| 13 |
tar -xf /tmp/libvirt-3.2.0.tar.xz -C /tmp |
| 14 |
WORKDIR /tmp/libvirt-3.2.0 |
| 15 |
RUN ./configure --disable-shared --enable-static --localstatedir=/var --without-storage-mpath && \ |
| 16 |
make -j2 && \ |
| 17 |
make install && \ |
| 18 |
sed -i 's/^Libs:.*/& -lnl -ltirpc -lxml2/' /usr/local/lib/pkgconfig/libvirt.pc |
| 19 |
|
| 20 |
# Prepare working directory |
| 21 |
ENV LIBVIRT_EXPORTER_PATH=/go/src/github.com/kumina/libvirt_exporter |
| 22 |
RUN mkdir -p $LIBVIRT_EXPORTER_PATH |
| 23 |
WORKDIR $LIBVIRT_EXPORTER_PATH |
| 24 |
COPY . . |
| 25 |
|
| 26 |
# Build and strip exporter |
| 27 |
RUN go get -d ./... && \ |
| 28 |
go build --ldflags '-extldflags "-static"' && \ |
| 29 |
strip libvirt_exporter |
| 30 |
|
| 31 |
# Stage 2: Prepare final image |
| 32 |
FROM scratch |
| 33 |
|
| 34 |
# Copy binary from Stage 1 |
| 35 |
COPY --from=0 /go/src/github.com/kumina/libvirt_exporter/libvirt_exporter . |
| 36 |
|
| 37 |
# Entrypoint for starting exporter |
| 38 |
ENTRYPOINT [ "./libvirt_exporter" ] |