Skip to content

Commit

Permalink
dockerfile: Do a clean build in the dockerfile
Browse files Browse the repository at this point in the history
When building a docker container, perform a clean build of the
component within the container instead of relying on the
go toolchain on the invoking machine and whatever the latest
(possibly stale) binary in the directory tree is.

Build dependency caching is enabled to reduce download times.

OCI container labels are added to provide standard container
metadata.

File locations, default invocation etc is the same as the
original Dockerfile.
  • Loading branch information
ringerc committed Jun 19, 2024
1 parent c8d4c39 commit a77234c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
7 changes: 5 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*
!pg_exporter
!pg_exporter.yml
!go.mod
!go.sum
!main.go
!exporter
!pg_exporter.yml
39 changes: 35 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
# syntax=docker/dockerfile:1
FROM golang:1.22-alpine AS builder-env

# Build a self-contained pg_exporter container with a clean environment and no
# dependencies.
#
# build with
#
# docker buildx build -f Dockerfile --tag pg_exporter .
#

WORKDIR /build

COPY go.mod go.sum ./
RUN \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go mod download

COPY . /build
RUN \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -a -o /pg_exporter .

FROM scratch
LABEL maintainer="Vonng <[email protected]>"
LABEL org.opencontainers.image.authors="Vonng <[email protected]>, Craig Ringer <[email protected]>" \
org.opencontainers.image.url="https://github.com/Vonng/pg_exporter" \
org.opencontainers.image.source="https://github.com/Vonng/pg_exporter" \
org.opencontainers.image.licenses="Apache-2.0" \
org.opencontainers.image.title="pg_exporter" \
org.opencontainers.image.description="PostgreSQL metrics exporter for Prometheus"

COPY pg_exporter /bin/pg_exporter
WORKDIR /bin
COPY --from=builder-env /pg_exporter /bin/pg_exporter
COPY pg_exporter.yml /etc/pg_exporter.yml

ENTRYPOINT ["/bin/pg_exporter"]
EXPOSE 9630/tcp
ENTRYPOINT ["/bin/pg_exporter"]

0 comments on commit a77234c

Please sign in to comment.