|
| 1 | +### |
| 2 | +# |
| 3 | +# Makefile for Docker projects |
| 4 | +# |
| 5 | +# Author: Yves Vindevogel (vindevoy) |
| 6 | +# Version: 1.0.0 |
| 7 | +# Date: 2020-10-22 |
| 8 | +# |
| 9 | +### |
| 10 | + |
| 11 | + |
| 12 | +# |
| 13 | +# Settings: specify to your own need |
| 14 | +# |
| 15 | +# IMAGE_REPO: your repository at DockerHub |
| 16 | +# IMAGE_NAME: the name of your image |
| 17 | +# IMAGE_VERSION: the version number (script also publishes as latest besides this number) |
| 18 | +# |
| 19 | + |
| 20 | + |
| 21 | +IMAGE_REPO=vindevoy |
| 22 | +IMAGE_NAME=debian10-base |
| 23 | +IMAGE_VERSION=1.0.0 |
| 24 | + |
| 25 | + |
| 26 | +###################### |
| 27 | +# DO NOT TOUCH BELOW # |
| 28 | +###################### |
| 29 | + |
| 30 | + |
| 31 | +# |
| 32 | +# Image tags: dvl - image created from the ./src/docker directory using the Dockerfile as written |
| 33 | +# rel - image created from the ./build/docker directory using the optimised Dockerfile |
| 34 | +# latest - the 'rel' image tagged as latest for hub.docker.com |
| 35 | +# IMAGE_VERSION - user defined version based on the latest version of the image |
| 36 | +# |
| 37 | + |
| 38 | + |
| 39 | +.PHONY: build |
| 40 | + |
| 41 | +# make sure we are using bash, which is needed for the if-statements |
| 42 | +SHELL := /bin/bash |
| 43 | + |
| 44 | +# some info for the user |
| 45 | +help: |
| 46 | + @echo "" |
| 47 | + @echo "USAGE:" |
| 48 | + @echo " make init: create the base structure of the project" |
| 49 | + @echo " make clean: remove build directory and as much as possible the existing development images" |
| 50 | + @echo " make compile: create the Docker image line by line, with multiple layers, for fast development" |
| 51 | + @echo " make test: run the compiled Docker image" |
| 52 | + @echo " make build: optimize the Dockerfile and make an image based on it" |
| 53 | + @echo " make run: run the optimized Docker image" |
| 54 | + @echo " make tag: make tags of the 'build' image" |
| 55 | + @echo " make publish: publish the image on hub.docker.com as 'VERSION' and 'latest'" |
| 56 | + @echo " make remove: remove all images of this project" |
| 57 | + @echo " make sysprune: do a big clean up" |
| 58 | + @echo " make help: this help ..." |
| 59 | + @echo "" |
| 60 | + |
| 61 | + |
| 62 | +# init creates the directories and empty Dockerfile |
| 63 | +init: |
| 64 | + mkdir -p ./src/docker |
| 65 | + mkdir -p ./src/resources |
| 66 | + |
| 67 | + touch ./src/docker/Dockerfile |
| 68 | + |
| 69 | + |
| 70 | +# clean removes the develop tag of the image, if it exists |
| 71 | +clean: |
| 72 | + rm -rf ./build |
| 73 | + |
| 74 | + $(eval dvl=$(shell docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | grep ' dvl ' | wc -l)) |
| 75 | + if [[ "$(dvl)" -ne 0 ]]; then docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | grep ' dvl ' | \ |
| 76 | + sed 's/ */ /g' | cut -d ' ' -f3 | xargs docker image remove --force; fi |
| 77 | + |
| 78 | + $(eval rel=$(shell docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | grep ' rel ' | wc -l)) |
| 79 | + if [[ "$(rel)" -ne 0 ]]; then docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | grep ' rel ' | \ |
| 80 | + sed 's/ */ /g' | cut -d ' ' -f3 | xargs docker image remove --force; fi |
| 81 | + |
| 82 | + |
| 83 | +# compile builds the docker image based on the src directory and tags the image as develop |
| 84 | +# compile uses layer after layer to improve development speed, but produces a larger image than needed |
| 85 | +compile: |
| 86 | + docker build -t $(IMAGE_REPO)/$(IMAGE_NAME):dvl ./src/docker |
| 87 | + |
| 88 | + |
| 89 | +# run the image using the develop tag (created with compile) |
| 90 | +test: compile |
| 91 | + docker run -it $(IMAGE_REPO)/$(IMAGE_NAME):dvl |
| 92 | + |
| 93 | + |
| 94 | +# build builds the docker image based on the optimized build directory and tags the image as latest |
| 95 | +# the produced image should be way smaller in size because of the layer optimasation |
| 96 | +build: |
| 97 | + rm -rf ./build |
| 98 | + mkdir -p ./build/docker |
| 99 | + mkdir -p ./build/resources |
| 100 | + |
| 101 | + # optimize the Dockerfile, too complicated to do in shell |
| 102 | + python optimize.py |
| 103 | + |
| 104 | + # only copy if files or directories exist |
| 105 | + if [ `ls ./src/resources/ | wc -w` -gt 0 ]; then cp -R ./src/resources/* ./build/resources/ ; fi |
| 106 | + |
| 107 | + docker build -t $(IMAGE_REPO)/$(IMAGE_NAME):rel ./build/docker |
| 108 | + |
| 109 | + |
| 110 | +run: build |
| 111 | + docker run -it $(IMAGE_REPO)/$(IMAGE_NAME):rel |
| 112 | + |
| 113 | + |
| 114 | +# create the tags IMAGE_VERSION and latest based on a build |
| 115 | +tag: build |
| 116 | + docker tag $(IMAGE_REPO)/$(IMAGE_NAME):rel $(IMAGE_REPO)/$(IMAGE_NAME):$(IMAGE_VERSION) |
| 117 | + docker tag $(IMAGE_REPO)/$(IMAGE_NAME):rel $(IMAGE_REPO)/$(IMAGE_NAME):latest |
| 118 | + |
| 119 | + |
| 120 | +# publish the optimized image |
| 121 | +publish: tag |
| 122 | + docker push $(IMAGE_REPO)/$(IMAGE_NAME):$(IMAGE_VERSION) |
| 123 | + docker push $(IMAGE_REPO)/$(IMAGE_NAME):latest |
| 124 | + |
| 125 | + |
| 126 | +remove: |
| 127 | + $(eval ic=$(shell docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | wc -l)) |
| 128 | + |
| 129 | + @if [[ "$(ic)" -ne 0 ]]; then docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | \ |
| 130 | + sed 's/ */ /g' | cut -d ' ' -f3 | xargs docker image remove --force; fi |
| 131 | + |
| 132 | + |
| 133 | +sysprune: |
| 134 | + docker system prune -f |
| 135 | + |
| 136 | + |
| 137 | +# |
| 138 | +# Information used: |
| 139 | +# https://beenje.github.io/blog/posts/dockerfile-anti-patterns-and-best-practices/ |
| 140 | +# https://stackoverflow.com/questions/46089219/how-to-reduce-the-size-of-rhel-centos-fedora-docker-image |
| 141 | +# https://www.codacy.com/blog/five-ways-to-slim-your-docker-images/ |
| 142 | +# https://github.com/jwilder/docker-squash |
| 143 | +# |
0 commit comments