Skip to content

Commit

Permalink
Create Docker images for the examples
Browse files Browse the repository at this point in the history
When those are published on Dockerhub, people will be able to start those
examples in fewer steps than the current documentation:

  https://prometheus.io/docs/introduction/getting_started/#starting-up-some-sample-targets

While relying on Docker may not be always wished, giving the possibility to
quickly start a daemon that exposes metrics can be useful in many tutorials.

This commit tries really hard to limit the number of per-example maintenance
work by sharing most of the Makefile to build the Docker images. The tiny top
level Makefiles in each example directory are strictly identical.

Updates: prometheus#347
  • Loading branch information
dlespiau committed Sep 11, 2017
1 parent 671c87b commit dbd2945
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
_obj
_test

# Examples
/examples/**/Dockerfile
/examples/random/random
/examples/simple/simple

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
Expand Down
39 changes: 39 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# This Makefile is included by a top-level Makefile in each example directory
#
# Invocation from the root of the project:
# $ make -C examples/simple
#
DOCKER_IMAGE_NAME ?= example-golang-$(EXAMPLE)
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))

all: $(EXAMPLE) docker

$(EXAMPLE): main.go Makefile
@echo ">> fetching dependencies"
@go get -d
@echo ">> building $@"
@CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags '-w'

define DockerfileTemplate
FROM scratch
LABEL maintainer "The Prometheus Authors <[email protected]>"
ADD $(EXAMPLE) $(EXAMPLE)
EXPOSE 8080
ENTRYPOINT ["/$(EXAMPLE)"]
endef

export DockerfileTemplate
Dockerfile: Makefile
@echo ">> writing Dockerfile"
@echo "$$DockerfileTemplate" >> $@

docker: Dockerfile
@echo ">> building docker image"
@docker build -t "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" .

clean:
@rm -f $(EXAMPLE) Dockerfile
@docker rmi -f "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)"

.PHONY: all docker clean
5 changes: 5 additions & 0 deletions examples/random/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
EXAMPLE_DIRECTORY := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
name = $(patsubst %/,%,$(dir $1))
EXAMPLE := $(notdir $(call name,$(EXAMPLE_DIRECTORY)))

include $(EXAMPLE_DIRECTORY)/../Makefile
5 changes: 5 additions & 0 deletions examples/simple/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
EXAMPLE_DIRECTORY := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
name = $(patsubst %/,%,$(dir $1))
EXAMPLE := $(notdir $(call name,$(EXAMPLE_DIRECTORY)))

include $(EXAMPLE_DIRECTORY)/../Makefile

0 comments on commit dbd2945

Please sign in to comment.