Skip to content

Commit

Permalink
Version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vindevoy committed Oct 24, 2020
1 parent 153e9bf commit ae33cae
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 0 deletions.
143 changes: 143 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
###
#
# Makefile for Docker projects
#
# Author: Yves Vindevogel (vindevoy)
# Version: 1.0.0
# Date: 2020-10-22
#
###


#
# Settings: specify to your own need
#
# IMAGE_REPO: your repository at DockerHub
# IMAGE_NAME: the name of your image
# IMAGE_VERSION: the version number (script also publishes as latest besides this number)
#


IMAGE_REPO=vindevoy
IMAGE_NAME=debian10-base
IMAGE_VERSION=1.0.0


######################
# DO NOT TOUCH BELOW #
######################


#
# Image tags: dvl - image created from the ./src/docker directory using the Dockerfile as written
# rel - image created from the ./build/docker directory using the optimised Dockerfile
# latest - the 'rel' image tagged as latest for hub.docker.com
# IMAGE_VERSION - user defined version based on the latest version of the image
#


.PHONY: build

# make sure we are using bash, which is needed for the if-statements
SHELL := /bin/bash

# some info for the user
help:
@echo ""
@echo "USAGE:"
@echo " make init: create the base structure of the project"
@echo " make clean: remove build directory and as much as possible the existing development images"
@echo " make compile: create the Docker image line by line, with multiple layers, for fast development"
@echo " make test: run the compiled Docker image"
@echo " make build: optimize the Dockerfile and make an image based on it"
@echo " make run: run the optimized Docker image"
@echo " make tag: make tags of the 'build' image"
@echo " make publish: publish the image on hub.docker.com as 'VERSION' and 'latest'"
@echo " make remove: remove all images of this project"
@echo " make sysprune: do a big clean up"
@echo " make help: this help ..."
@echo ""


# init creates the directories and empty Dockerfile
init:
mkdir -p ./src/docker
mkdir -p ./src/resources

touch ./src/docker/Dockerfile


# clean removes the develop tag of the image, if it exists
clean:
rm -rf ./build

$(eval dvl=$(shell docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | grep ' dvl ' | wc -l))
if [[ "$(dvl)" -ne 0 ]]; then docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | grep ' dvl ' | \
sed 's/ */ /g' | cut -d ' ' -f3 | xargs docker image remove --force; fi

$(eval rel=$(shell docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | grep ' rel ' | wc -l))
if [[ "$(rel)" -ne 0 ]]; then docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | grep ' rel ' | \
sed 's/ */ /g' | cut -d ' ' -f3 | xargs docker image remove --force; fi


# compile builds the docker image based on the src directory and tags the image as develop
# compile uses layer after layer to improve development speed, but produces a larger image than needed
compile:
docker build -t $(IMAGE_REPO)/$(IMAGE_NAME):dvl ./src/docker


# run the image using the develop tag (created with compile)
test: compile
docker run -it $(IMAGE_REPO)/$(IMAGE_NAME):dvl


# build builds the docker image based on the optimized build directory and tags the image as latest
# the produced image should be way smaller in size because of the layer optimasation
build:
rm -rf ./build
mkdir -p ./build/docker
mkdir -p ./build/resources

# optimize the Dockerfile, too complicated to do in shell
python optimize.py

# only copy if files or directories exist
if [ `ls ./src/resources/ | wc -w` -gt 0 ]; then cp -R ./src/resources/* ./build/resources/ ; fi

docker build -t $(IMAGE_REPO)/$(IMAGE_NAME):rel ./build/docker


run: build
docker run -it $(IMAGE_REPO)/$(IMAGE_NAME):rel


# create the tags IMAGE_VERSION and latest based on a build
tag: build
docker tag $(IMAGE_REPO)/$(IMAGE_NAME):rel $(IMAGE_REPO)/$(IMAGE_NAME):$(IMAGE_VERSION)
docker tag $(IMAGE_REPO)/$(IMAGE_NAME):rel $(IMAGE_REPO)/$(IMAGE_NAME):latest


# publish the optimized image
publish: tag
docker push $(IMAGE_REPO)/$(IMAGE_NAME):$(IMAGE_VERSION)
docker push $(IMAGE_REPO)/$(IMAGE_NAME):latest


remove:
$(eval ic=$(shell docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | wc -l))

@if [[ "$(ic)" -ne 0 ]]; then docker images | grep '$(IMAGE_REPO)/$(IMAGE_NAME)' | \
sed 's/ */ /g' | cut -d ' ' -f3 | xargs docker image remove --force; fi


sysprune:
docker system prune -f


#
# Information used:
# https://beenje.github.io/blog/posts/dockerfile-anti-patterns-and-best-practices/
# https://stackoverflow.com/questions/46089219/how-to-reduce-the-size-of-rhel-centos-fedora-docker-image
# https://www.codacy.com/blog/five-ways-to-slim-your-docker-images/
# https://github.com/jwilder/docker-squash
#
12 changes: 12 additions & 0 deletions build/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM debian:10-slim

LABEL maintainer="Yves Vindevogel (vindevoy) - [email protected]"

ARG TZ_REGION=Europe
ARG TZ_CITY=Brussels

RUN set -x && \
rm -f /etc/localtime && \
ln -s /usr/share/zoneinfo/$TZ_REGION/$TZ_CITY /etc/localtime

CMD ["/bin/sh"]
57 changes: 57 additions & 0 deletions optimize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/python

SOURCE_FILE = './src/docker/Dockerfile'
BUILD_FILE = './build/docker/Dockerfile'

NO_INDENT_SPACES = 0
EXTRA_INDENT_SPACES = 4
RUN_INDENT_SPACES = 4


def optimize():
optimized = ''
command = ''
previous_command = ''
next_line_continued = False
next_command_continued = False

src = open(SOURCE_FILE, 'r')

for line in src.readlines():
line = line.strip()

if len(line) > 0 and line[0:1] != '#':
if next_line_continued:
extra_spaces = NO_INDENT_SPACES if next_command_continued else EXTRA_INDENT_SPACES
optimized += '\n' + (' ' * (len(command) + extra_spaces + 1)) + '{0}'.format(line)
else:
command = line.split()[0].upper()

if command == 'RUN':
if previous_command != 'RUN':
optimized += '\nRUN set -x'

# 4 is the length of the RUN command and a blank, nothing to do with the indent spaces
optimized += ' && \\ \n{0}{1}'.format(' ' * RUN_INDENT_SPACES, line[RUN_INDENT_SPACES:].strip())
else:
if previous_command == 'RUN':
optimized += '\n'

if previous_command != command and previous_command != '':
optimized += '\n'

optimized += '{0} {1}\n'.format(command.upper(), line[len(command) + 1:].strip())

previous_command = command
next_line_continued = line.endswith('\\')
next_command_continued = line.replace(' ', '').endswith('&&\\')

src.close()

build = open(BUILD_FILE, 'w+')
build.write(optimized)
build.close()


if __name__ == '__main__':
optimize()
20 changes: 20 additions & 0 deletions src/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
###
#
# Yves Vindevogel (vindevoy)
# 2019-10-25
#
# Basic Debian 10 (slim version) image with possibility to change the timezone
#
###

FROM debian:10-slim

LABEL maintainer="Yves Vindevogel (vindevoy) - [email protected]"

ARG TZ_REGION=Europe
ARG TZ_CITY=Brussels

RUN rm -f /etc/localtime
RUN ln -s /usr/share/zoneinfo/$TZ_REGION/$TZ_CITY /etc/localtime

CMD ["/bin/sh"]

0 comments on commit ae33cae

Please sign in to comment.