FROM registry.hub.docker.com/library/golang:1.25-alpine3.22 AS build

# Install build depdencies for all supported arches
RUN apk --no-cache add bash build-base cmake device-mapper findutils git \
                       libc6-compat linux-headers ndctl-dev pkgconfig python3 thin-provisioning-tools wget zfs && \
    echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf && \
    rm -rf /var/cache/apk/*

RUN wget https://sourceforge.net/projects/perfmon2/files/libpfm4/libpfm-4.11.0.tar.gz && \
  echo "112bced9a67d565ff0ce6c2bb90452516d1183e5  libpfm-4.11.0.tar.gz" | sha1sum -c  && \
  tar -xzf libpfm-4.11.0.tar.gz && \
  rm libpfm-4.11.0.tar.gz

RUN export DBG="-g -Wall" && \
  make -e -C libpfm-4.11.0 && \
  make install -C libpfm-4.11.0

# ipmctl only supports Intel x86_64 processors.
# https://github.com/intel/ipmctl/issues/163

# Disable libipmctl due to https://github.com/google/cadvisor/issues/3482
#RUN if [ "$(uname --machine)" = "x86_64" ]; then \
    #git clone -b v02.00.00.3885 https://github.com/intel/ipmctl/ && \
    #cd ipmctl && \
    #mkdir output && \
    #cd output && \
    #cmake -DRELEASE=ON -DCMAKE_INSTALL_PREFIX=/ -DCMAKE_INSTALL_LIBDIR=/usr/local/lib .. && \
    #make -j all && \
    #make install; fi

WORKDIR /go/src/github.com/google/cadvisor

# Cache Golang Dependencies for faster incremental builds
ADD go.mod go.sum ./
RUN go mod download
ADD cmd/go.mod cmd/go.sum ./cmd/
RUN cd cmd && go mod download

ADD . .

ARG VERSION

# libipmctl only works on x86_64 CPUs.
RUN export GO_TAGS="libpfm,netgo"; \
    if [ "$(uname --machine)" = "x86_64" ]; then \
          # Disable libipmctl due to https://github.com/google/cadvisor/issues/3482
          #export GO_TAGS="$GO_TAGS,libipmctl"; \
          export GO_TAGS="$GO_TAGS"; \
    fi; \
    GO_FLAGS="-tags=$GO_TAGS" ./build/build.sh

FROM mirror.gcr.io/library/alpine:3.22
MAINTAINER dengnan@google.com vmarmol@google.com vishnuk@google.com jimmidyson@gmail.com stclair@google.com

RUN apk --no-cache add libc6-compat device-mapper findutils ndctl thin-provisioning-tools zfs && \
    echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf && \
    rm -rf /var/cache/apk/*

# Grab cadvisor,libpfm4 and libipmctl from "build" container if they exist (libipmctl only works on amd64/x86_64).
COPY --from=build /usr/local/lib/libpfm.so* /usr/local/lib/
# Disable libipmctl due to https://github.com/google/cadvisor/issues/3482
#COPY --from=build /usr/local/lib/libipmctl.so* /usr/local/lib/
COPY --from=build /go/src/github.com/google/cadvisor/_output/cadvisor /usr/bin/cadvisor

COPY deploy/entrypoint.sh /usr/bin/entrypoint.sh
RUN chmod +x /usr/bin/entrypoint.sh

COPY deploy/healthcheck.sh /usr/bin/healthcheck.sh
RUN chmod +x /usr/bin/healthcheck.sh

# Default port is 8080, but can be changed with -port flag
# Users should expose their custom port with: docker run -p <custom>:<custom>
EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s \
  CMD /usr/bin/healthcheck.sh

# Use entrypoint wrapper
ENTRYPOINT ["/usr/bin/entrypoint.sh"]

