-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker.mk
157 lines (134 loc) · 3.7 KB
/
docker.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#
# Input variables
#
# - DISABLE_DOCKER
#
# - DOCKERFILE
#
# - DOCKER_REGISTRY
# - DOCKER_ORGANISATION
# - CONTAINER_NAME
# - IMAGE_VERSION
#
# - DOCKER_SHELL
# - BIND_VOLUMES
# - EXPOSE_PORTS
# - EXTRA_RUN_OPTS
#
GIT_COMMIT:=$(strip $(shell git rev-parse --short HEAD 2>/dev/null))
GIT_ORIGIN_URL:=$(shell git config --get remote.origin.url 2>/dev/null)
GIT_TAG:=$(shell git describe --tags 2> /dev/null)
GIT_TAG_VSTRIPPED:=$(patsubst v%,%,$(GIT_TAG))
IMAGE_VERSION_DEFAULT?=latest
ifndef IMAGE_VERSION
ifneq ("$(GIT_TAG_VSTRIPPED)_","_")
IMAGE_VERSION:=$(GIT_TAG_VSTRIPPED)
else
IMAGE_VERSION:=$(IMAGE_VERSION_DEFAULT)
endif
endif
ifndef DOCKER_SHELL
DOCKER_SHELL:=/bin/bash
endif
ifndef CONTAINER_NAME
DIR_NAME:=$(notdir $(realpath .))
#CONTAINER_NAME:=$(subst -,,$(DIR_NAME))
CONTAINER_NAME:=$(DIR_NAME)
endif
#
# build up the IMAGE_NAME, optionally including REGISTRY and ORGANISATION
#
IMAGE_NAME:=$(CONTAINER_NAME):$(IMAGE_VERSION)
ifneq ("$(DOCKER_ORGANISATION)x","x")
IMAGE_NAME:=$(DOCKER_ORGANISATION)/$(IMAGE_NAME)
endif
ifneq ("$(DOCKER_REGISTRY)x","x")
IMAGE_NAME:=$(DOCKER_REGISTRY)/$(IMAGE_NAME)
endif
#
# also build image:latest
#
IMAGE_DEFAULT:=$(patsubst %:$(IMAGE_VERSION),%:$(IMAGE_VERSION_DEFAULT),$(IMAGE_NAME))
ifeq ($(DOCKERFILE)x,x)
DOCKERFILE:=$(wildcard Dockerfile)
endif
ifneq ($(DOCKERFILE)x,x)
ifndef EXPOSE_PORTS
EXPOSE_PORTS:=$(shell gawk '/^EXPOSE/ { for(i=2;i<=NF;i++) printf("-p %s:%s ", $$i, $$i) }' Dockerfile)
endif
endif
ifndef BIND_VOLUMES
BIND_VOLUMES:=
endif
ifeq ($(origin DOCKER_BUILDARG_VCSREF),undefined)
DOCKER_VCSREF?=$(GIT_COMMIT)
ifeq ($(DOCKER_VCSREF)x,x)
DOCKER_VCSREF:=unknown
endif
DOCKER_BUILDARG_VCSREF:=--build-arg "VCS_REF=$(DOCKER_VCSREF)"
endif
ifeq ($(origin DOCKER_BUILDARG_VCSURL),undefined)
DOCKER_VCSURL?=$(GIT_ORIGIN_URL)
ifeq ($(DOCKER_VCSURL)x,x)
DOCKER_VCSURL:=unknown
endif
DOCKER_BUILDARG_VCSURL:=--build-arg "VCS_URL=$(DOCKER_VCSURL)"
endif
ifeq ($(origin DOCKER_BUILDARG_BUILDDATE),undefined)
DOCKER_BUILDARG_BUILDDATE:=--build-arg BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
endif
.PHONY: image
image:
$(call PROMPT,docker build)
rm -f $(TAR_FILE)
docker build --rm --force-rm \
-t $(IMAGE_DEFAULT) \
-f $(DOCKERFILE) \
$(DOCKER_BUILDARG_VCSREF) \
$(DOCKER_BUILDARG_VCSURL) \
$(DOCKER_BUILDARG_BUILDDATE) \
.
[ "$(IMAGE_VERSION)" == "$(IMAGE_VERSION_DEFAULT)" ] || docker tag $(IMAGE_DEFAULT) $(IMAGE_NAME)
.PHONY: clean
clean::
rm -f $(TAR_FILE)
-docker kill $(CONTAINER_NAME) 2> /dev/null
-docker rm $(CONTAINER_NAME) 2> /dev/null
.PHONY: clobber
clobber:: clean
-docker rmi -f $(IMAGE_NAME) $(IMAGE_DEFAULT) 2> /dev/null
TAR_FILE:=$(notdir $(subst :,_,$(IMAGE_NAME))).tar
.PHONY: docker-tar
docker-tar:
$(call PROMPT,docker save)
docker save '$(IMAGE_NAME)' > $(TAR_FILE)
gzip $(TAR_FILE)
#
# Push both the versioned and the default "latest" image. They might be the same thing - but,
# if so, then the second one will complete very quickly.
#
.PHONY: docker-push
docker-push:
$(call PROMPT,docker push)
docker push $(IMAGE_NAME)
docker push $(IMAGE_DEFAULT)
.PHONY: docker-run
docker-run: clean
docker run -d $(EXTRA_RUN_OPTS) $(EXPOSE_PORTS) $(BIND_VOLUMES) --name $(CONTAINER_NAME) $(IMAGE_NAME)
.PHONY: docker-exec
docker-exec:
docker exec -ti $(CONTAINER_NAME) $(DOCKER_SHELL)
.PHONY: docker-entry
docker-entry:
docker run -ti $(EXTRA_RUN_OPTS) $(EXPOSE_PORTS) $(BIND_VOLUMES) --entrypoint $(DOCKER_SHELL) $(IMAGE_NAME)
# Useful with a scratch container. Use in Dockerfile as:
#
# ADD ca-bundle.crt /etc/pki/tls/certs/ca-bundle.crt
ca-bundle.crt:
curl -sL https://mkcert.org/generate/ > $@
#
# Clean up general docker environment
#
.PHONY: docker-clean
docker-clean:
docker images | grep '<none>' | gawk '{ print $$3 }' | xargs docker rmi