docker.mk 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #########################################
  2. # Building Docker Image
  3. #
  4. # This uses a multi-stage build file. The first stage is a builder (that might
  5. # be large in size). After the build has succeeded, the statically linked
  6. # binary is copied to a new image that is optimized for size.
  7. #########################################
  8. # Testing: output images to out/ with -o out, like this:
  9. # docker buildx build . --progress plain -t step-ca:master -f docker/Dockerfile --platform linux/amd64,linux/arm/v7,linux/386,linux/arm64 -o out
  10. ifeq (, $(shell which docker))
  11. DOCKER_CLIENT_OS := linux
  12. else
  13. DOCKER_CLIENT_OS := $(strip $(shell docker version -f '{{.Client.Os}}'))
  14. endif
  15. DOCKER_PLATFORMS = linux/amd64,linux/386,linux/arm,linux/arm64
  16. DOCKER_IMAGE_NAME = smallstep/step-ca
  17. docker-prepare:
  18. # Ensure, we can build for ARM architecture
  19. ifeq (linux,$(DOCKER_CLIENT_OS))
  20. [ -f /proc/sys/fs/binfmt_misc/qemu-arm ] || docker run --rm --privileged docker/binfmt:a7996909642ee92942dcd6cff44b9b95f08dad64
  21. endif
  22. # Register buildx builder
  23. mkdir -p $$HOME/.docker/cli-plugins
  24. docker buildx >/dev/null || \
  25. (wget -O $$HOME/.docker/cli-plugins/docker-buildx https://github.com/docker/buildx/releases/download/v0.4.1/buildx-v0.4.1.$(DOCKER_CLIENT_OS)-amd64 && \
  26. chmod +x $$HOME/.docker/cli-plugins/docker-buildx)
  27. docker buildx create --use --name mybuilder --platform="$(DOCKER_PLATFORMS)" || true
  28. .PHONY: docker-prepare
  29. #################################################
  30. # Releasing Docker Images
  31. #
  32. # Using the docker build infrastructure, this section is responsible for
  33. # logging into docker hub.
  34. #################################################
  35. # Rely on DOCKER_USERNAME and DOCKER_PASSWORD being set inside the CI or
  36. # equivalent environment
  37. docker-login:
  38. $Q docker login -u="$(DOCKER_USERNAME)" -p="$(DOCKER_PASSWORD)"
  39. .PHONY: docker-login
  40. #################################################
  41. # Targets for different type of builds
  42. #################################################
  43. define DOCKER_BUILDX
  44. # $(1) -- Image Tag
  45. # $(2) -- Push (empty is no push | --push will push to dockerhub)
  46. docker buildx build . --progress plain -t $(DOCKER_IMAGE_NAME):$(1) -f docker/Dockerfile.step-ca --platform="$(DOCKER_PLATFORMS)" $(2)
  47. endef
  48. # For non-master builds don't build the docker containers.
  49. docker-branch:
  50. # For master builds create the docker containers but don't push them.
  51. docker-master: docker-prepare
  52. $(call DOCKER_BUILDX,latest,)
  53. # For all builds with a release candidate tag build and push the containers.
  54. docker-release-candidate: docker-prepare docker-login
  55. $(call DOCKER_BUILDX,$(VERSION),--push)
  56. # For all builds with a release tag build and push the containers.
  57. docker-release: docker-prepare docker-login
  58. $(call DOCKER_BUILDX,latest,--push)
  59. $(call DOCKER_BUILDX,$(VERSION),--push)
  60. .PHONY: docker-branch docker-master docker-release-candidate docker-release
  61. # XXX We put the output for the build in 'output' so we don't mess with how we
  62. # do rule overriding from the base Makefile (if you name it 'build' it messes up
  63. # the wildcarding).
  64. DOCKER_OUTPUT=$(OUTPUT_ROOT)docker/
  65. DOCKER_MAKE=V=$V GOOS_OVERRIDE='GOOS=linux GOARCH=amd64' PREFIX=$(1) make $(1)bin/$(BINNAME)
  66. DOCKER_BUILD=$Q docker build -t $(DOCKER_IMAGE_NAME):latest -f docker/Dockerfile.step-ca --build-arg BINPATH=$(DOCKER_OUTPUT)bin/$(BINNAME) .
  67. docker-dev: docker/Dockerfile.step-ca
  68. mkdir -p $(DOCKER_OUTPUT)
  69. $(call DOCKER_MAKE,$(DOCKER_OUTPUT),step-ca)
  70. $(call DOCKER_BUILD)
  71. .PHONY: docker-dev