Skip to content

Commit ae33cae

Browse files
committed
Version 1.0.0
1 parent 153e9bf commit ae33cae

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed

Makefile

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
#

build/docker/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM debian:10-slim
2+
3+
LABEL maintainer="Yves Vindevogel (vindevoy) - [email protected]"
4+
5+
ARG TZ_REGION=Europe
6+
ARG TZ_CITY=Brussels
7+
8+
RUN set -x && \
9+
rm -f /etc/localtime && \
10+
ln -s /usr/share/zoneinfo/$TZ_REGION/$TZ_CITY /etc/localtime
11+
12+
CMD ["/bin/sh"]

optimize.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/python
2+
3+
SOURCE_FILE = './src/docker/Dockerfile'
4+
BUILD_FILE = './build/docker/Dockerfile'
5+
6+
NO_INDENT_SPACES = 0
7+
EXTRA_INDENT_SPACES = 4
8+
RUN_INDENT_SPACES = 4
9+
10+
11+
def optimize():
12+
optimized = ''
13+
command = ''
14+
previous_command = ''
15+
next_line_continued = False
16+
next_command_continued = False
17+
18+
src = open(SOURCE_FILE, 'r')
19+
20+
for line in src.readlines():
21+
line = line.strip()
22+
23+
if len(line) > 0 and line[0:1] != '#':
24+
if next_line_continued:
25+
extra_spaces = NO_INDENT_SPACES if next_command_continued else EXTRA_INDENT_SPACES
26+
optimized += '\n' + (' ' * (len(command) + extra_spaces + 1)) + '{0}'.format(line)
27+
else:
28+
command = line.split()[0].upper()
29+
30+
if command == 'RUN':
31+
if previous_command != 'RUN':
32+
optimized += '\nRUN set -x'
33+
34+
# 4 is the length of the RUN command and a blank, nothing to do with the indent spaces
35+
optimized += ' && \\ \n{0}{1}'.format(' ' * RUN_INDENT_SPACES, line[RUN_INDENT_SPACES:].strip())
36+
else:
37+
if previous_command == 'RUN':
38+
optimized += '\n'
39+
40+
if previous_command != command and previous_command != '':
41+
optimized += '\n'
42+
43+
optimized += '{0} {1}\n'.format(command.upper(), line[len(command) + 1:].strip())
44+
45+
previous_command = command
46+
next_line_continued = line.endswith('\\')
47+
next_command_continued = line.replace(' ', '').endswith('&&\\')
48+
49+
src.close()
50+
51+
build = open(BUILD_FILE, 'w+')
52+
build.write(optimized)
53+
build.close()
54+
55+
56+
if __name__ == '__main__':
57+
optimize()

src/docker/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
###
2+
#
3+
# Yves Vindevogel (vindevoy)
4+
# 2019-10-25
5+
#
6+
# Basic Debian 10 (slim version) image with possibility to change the timezone
7+
#
8+
###
9+
10+
FROM debian:10-slim
11+
12+
LABEL maintainer="Yves Vindevogel (vindevoy) - [email protected]"
13+
14+
ARG TZ_REGION=Europe
15+
ARG TZ_CITY=Brussels
16+
17+
RUN rm -f /etc/localtime
18+
RUN ln -s /usr/share/zoneinfo/$TZ_REGION/$TZ_CITY /etc/localtime
19+
20+
CMD ["/bin/sh"]

0 commit comments

Comments
 (0)