Skip to content

Commit

Permalink
Fix and optimize generator Docker image building (#1045)
Browse files Browse the repository at this point in the history
* Fix Docker image of generator always pointing to latest tag

* new `REPO_TAG` variable can be used to specify the tag/branch to use for a build
* the Makefile default is to use the branch of the current repository `HEAD`
* the Dockerfile default (if building manually with Docker) is `main`
* updated CircleCI configuration to apply the correct `REPO_TAG` values

Signed-off-by: Hugo Hromic <[email protected]>

* Optimize Docker image of generator

* use multi-stage build to reduce final image size
* ensure to use the same Debian version (`bookworm`) for the builder and final images
* use `--no-install-recommends` to reduce unnecessary dependency downloads
* removed unnecessary `unzip` dependency in the image
* use modern syntax (using equal sign) for `ENV` Dockerfile directive

Signed-off-by: Hugo Hromic <[email protected]>

---------

Signed-off-by: Hugo Hromic <[email protected]>
  • Loading branch information
hhromic authored Nov 21, 2023
1 parent 323d1b0 commit db7cb38
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ jobs:
steps:
- prometheus/setup_build_environment:
docker_version: ""
- run: make -C generator docker DOCKER_IMAGE_TAG=main
- run: make -C generator docker DOCKER_REPO=quay.io/prometheus DOCKER_IMAGE_TAG=main
- run: make -C generator docker REPO_TAG=main DOCKER_IMAGE_TAG=main
- run: make -C generator docker REPO_TAG=main DOCKER_REPO=quay.io/prometheus DOCKER_IMAGE_TAG=main
- run: docker images
- run: docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD
- run: docker login -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io
Expand All @@ -67,8 +67,8 @@ jobs:
steps:
- prometheus/setup_build_environment:
docker_version: ""
- run: make -C generator docker DOCKER_IMAGE_TAG=$CIRCLE_TAG
- run: make -C generator docker DOCKER_IMAGE_TAG=$CIRCLE_TAG DOCKER_REPO=quay.io/prometheus
- run: make -C generator docker REPO_TAG=$CIRCLE_TAG DOCKER_IMAGE_TAG=$CIRCLE_TAG
- run: make -C generator docker REPO_TAG=$CIRCLE_TAG DOCKER_IMAGE_TAG=$CIRCLE_TAG DOCKER_REPO=quay.io/prometheus
- run: docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD
- run: docker login -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io
- run: |
Expand Down
22 changes: 16 additions & 6 deletions generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
FROM golang:latest
FROM golang:bookworm AS builder

RUN apt-get update && \
apt-get install -y libsnmp-dev unzip && \
go install github.com/prometheus/snmp_exporter/generator@latest
RUN apt-get update \
&& apt-get install -y --no-install-recommends libsnmp-dev

ARG REPO_TAG=main
RUN go install github.com/prometheus/snmp_exporter/generator@"$REPO_TAG"

FROM debian:bookworm-slim

WORKDIR "/opt"

ENTRYPOINT ["/go/bin/generator"]
ENTRYPOINT ["/bin/generator"]

ENV MIBDIRS mibs
ENV MIBDIRS=mibs

CMD ["generate"]

RUN apt-get update \
&& apt-get install -y --no-install-recommends libsnmp40 \
&& rm -rf /var/lib/apt/lists/*

COPY --from=builder /go/bin/generator /bin/generator
6 changes: 4 additions & 2 deletions generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ MIB_PATH := 'mibs'

CURL_OPTS ?= -L --no-progress-meter --retry 3 --retry-delay 3 --fail

REPO_TAG ?= $(shell git rev-parse --abbrev-ref HEAD)

DOCKER_IMAGE_NAME ?= snmp-generator
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
DOCKER_IMAGE_TAG ?= $(subst /,-,$(REPO_TAG))
DOCKER_REPO ?= prom

SANITIZED_DOCKER_IMAGE_TAG := $(subst +,-,$(DOCKER_IMAGE_TAG))
Expand Down Expand Up @@ -79,7 +81,7 @@ parse_errors: generator mibs

.PHONY: docker
docker:
docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(SANITIZED_DOCKER_IMAGE_TAG)" .
docker build --build-arg REPO_TAG="$(REPO_TAG)" -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(SANITIZED_DOCKER_IMAGE_TAG)" .

.PHONY: docker-generate
docker-generate: docker mibs
Expand Down

0 comments on commit db7cb38

Please sign in to comment.