Vonng / pg_exporter

Advanced PostgreSQL & Pgbouncer Metrics Exporter for Prometheus
https://pigsty.io
Apache License 2.0
180 stars 45 forks source link

dockerfile: Do a clean build in the dockerfile #54

Closed ringerc closed 5 months ago

ringerc commented 5 months ago

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.

Build

➜  pg_exporter git:(pr-clean-build-dockerfile) ✗ docker buildx build -f Dockerfile --tag pg_exporter .
[+] Building 1.0s (16/16) FINISHED                                                                                                             docker:default
 => [internal] load .dockerignore                                                                                                                        0.0s
 => => transferring context: 94B                                                                                                                         0.0s
 => [internal] load build definition from Dockerfile                                                                                                     0.0s
 => => transferring dockerfile: 1.27kB                                                                                                                   0.0s
 => resolve image config for docker.io/docker/dockerfile:1                                                                                               0.8s
 => CACHED docker-image://docker.io/docker/dockerfile:1@sha256:a57df69d0ea827fb7266491f2813635de6f17269be881f696fbfdf2d83dda33e                          0.0s
 => [internal] load metadata for docker.io/library/golang:1.22-alpine                                                                                    0.0s
 => [internal] load build context                                                                                                                        0.0s
 => => transferring context: 562B                                                                                                                        0.0s
 => [stage-1 1/3] WORKDIR /bin                                                                                                                           0.0s
 => [builder-env 1/6] FROM docker.io/library/golang:1.22-alpine                                                                                          0.0s
 => CACHED [builder-env 2/6] WORKDIR /build                                                                                                              0.0s
 => CACHED [builder-env 3/6] COPY go.mod go.sum ./                                                                                                       0.0s
 => CACHED [builder-env 4/6] RUN   --mount=type=cache,target=/go/pkg/mod   --mount=type=cache,target=/root/.cache/go-build   CGO_ENABLED=0 GOOS=linux g  0.0s
 => CACHED [builder-env 5/6] COPY . /build                                                                                                               0.0s
 => CACHED [builder-env 6/6] RUN   --mount=type=cache,target=/go/pkg/mod   --mount=type=cache,target=/root/.cache/go-build   CGO_ENABLED=0 GOOS=linux g  0.0s
 => CACHED [stage-1 2/3] COPY --from=builder-env /pg_exporter /bin/pg_exporter                                                                           0.0s
 => CACHED [stage-1 3/3] COPY pg_exporter.yml /etc/pg_exporter.yml                                                                                       0.0s
 => exporting to image                                                                                                                                   0.0s
 => => exporting layers                                                                                                                                  0.0s
 => => writing image sha256:a2ec52c037060a59c813f7bef7b977803ae04a1624843af29d76a0a61d1e1fcf                                                             0.0s
 => => naming to docker.io/library/pg_exporter                                                                                                           0.0s

Run with bound unix socket

➜  pg_exporter git:(pr-clean-build-dockerfile) ✗ docker run -it -u $(id -u):$(id -g) -p 127.0.0.1:9630:9630 --rm --mount type=bind,src=/var/run/postgresql,dst=/var/run/postgresql pg_exporter -u 'postgresql://postgres:@:/?sslmode=disable&host=/var/run/postgresql' -c '/etc/pg_exporter.yml' --fail-fast --log.level info
level=info timestamp=2024-06-19T01:50:00.82288681Z caller=utils.go:56 msg="retrieve target url postgresql://postgres:xxxxx@:/?sslmode=disable&host=/var/run/postgresql from command line"
level=info timestamp=2024-06-19T01:50:00.822917932Z caller=utils.go:56 msg="retrieve config path /etc/pg_exporter.yml from command line"
level=info timestamp=2024-06-19T01:50:00.839050229Z caller=utils.go:56 msg="server [postgresql://postgres:xxxxx@:/?sslmode=disable&host=/var/run/postgresql] version changed: from [0] to [140008]"
level=info timestamp=2024-06-19T01:50:00.840291834Z caller=utils.go:56 msg="server [postgresql://postgres:xxxxx@:/?host=/var/run/postgresql&sslmode=disable] datname changed: from [] to [postgres]"
level=info timestamp=2024-06-19T01:50:00.84060547Z caller=utils.go:56 msg="pg_exporter for postgresql://postgres:xxxxx@:/?host=/var/run/postgresql&sslmode=disable start, listen on http://:9630/metrics"
level=info timestamp=2024-06-19T01:50:00.840705459Z caller=tls_config.go:274 msg="Listening on" address=[::]:9630
level=info timestamp=2024-06-19T01:50:00.840719768Z caller=tls_config.go:277 msg="TLS is disabled." http2=false address=[::]:9630
level=info timestamp=2024-06-19T01:50:02.654488233Z caller=utils.go:56 msg="server [postgres] planned with 64 queries, 34 installed, 30 discarded, installed: pg_archiver, pg_checkpoint, pg_table_size, pg_wait, pg_xact, pg_index, pg_setting, pg_wal, pg_indexing, pg_bgwriter, pg_db_14, pg_func, pg_slot_14, pg_seq, pg_subrel, pg_relkind, pg_size, pg_activity, pg_slru_13, pg_sync_standby, pg_ssl, pg_pubrel, pg_primary_only, pg_meta_13, pg_repl_12, pg_vacuuming, pg_downstream, pg_defpart, pg_sub_10_14, pg_lock, pg_clustering, pg_backup, pg_backend, pg_table_13 , discarded: pg_recovery_prefetch, pg_shmem, pgbouncer_pool_116_118, pg_index_bloat, pg_recv_11, pgbouncer_database_108_115, pg_repl_10_11, pg_replica_only, pg_table_bloat, pgbouncer_list, pgbouncer_pool_108_115, pg_slot_13, pg_io, pg_db_10_11, pgbouncer_database_116, pg_slot_10_12, pg_recv_13, pg_recv_10, pg_table_16, pg_meta, pg_query_94_12, pg_query_13, pg_table_10_12, pgbouncer_pool_118, pg_db_confl, pgbouncer_stat, pg_db_12_13, pg_sub_15, pg_origin, pg_recovery"

Docker inspect

➜  pg_exporter git:(pr-clean-build-dockerfile) ✗ docker inspect -f '{{json .Config.Labels}}' pg_exporter | yq --prettyPrint
org.opencontainers.image.authors: Vonng <rh@vonng.com>, Craig Ringer <craig.ringer@enterprisedb.com>
org.opencontainers.image.description: PostgreSQL metrics exporter for Prometheus
org.opencontainers.image.licenses: Apache-2.0
org.opencontainers.image.source: https://github.com/Vonng/pg_exporter
org.opencontainers.image.title: pg_exporter
org.opencontainers.image.url: https://github.com/Vonng/pg_exporter