This repository has been archived by the owner on Aug 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker.mk
71 lines (58 loc) · 2.45 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
.PHONY: clean-image clean-docker clean-containers root-shell shell image
SCMD := $(shell \
if ! id -n -G | grep -q docker; then \
echo sudo; \
fi \
)
NCPU := $(shell \
getconf _NPROCESSORS_ONLN 2>/dev/null || \
getconf NPROCESSORS_ONLN 2>/dev/null || echo 1 \
)
DEVNULL := $(shell rm -rf .images)
export NCPU
clean-image: | .images clean-containers ## Remove the working docker image
if [ -e .images/$(IMAGE_NAME) ]; then \
$(SCMD) docker rmi -f $(IMAGE_NAME); \
fi
clean-docker: ## Remove all docker containers and dangling images
$(SCMD) docker rm -v \
$$($(SCMD) docker ps -a -q 2>/dev/null) 2>/dev/null; \
true
$(SCMD) docker rmi \
$$($(SCMD) docker images -f "dangling=true" -q 2>/dev/null) 2>/dev/null; \
true
$(SCMD) docker volume rm \
$$($(SCMD) docker volume ls -f "dangling=true" -q 2>/dev/null) 2>/dev/null; \
true
clean-containers: ## Remove old docker containers
$(SCMD) docker rm -v \
$$($(SCMD) docker ps -f "status=exited" -q 2>/dev/null) 2>/dev/null; \
true
root-shell: | image ## Open a root-shell in container
$(SCMD) docker run --rm -e "NCPU=$(NCPU)" -e "TERM=$(TERM)" -it $(IMAGE_NAME) /bin/bash; true
shell: | image ## Open a shell in a container
$(SCMD) docker run --rm -e "NCPU=$(NCPU)" -e "DISPLAY=$(DISPLAY)" \
-e "XAUTHORITY=$(XAUTHORITY)" -e "TERM=$(TERM)" \
-u $(shell id -u) -h "$(shell hostname)-$(IMAGE_NAME)" \
-v "$(PWD)":/host -v /tmp/.X11-unix:/tmp/.X11-unix \
-v "$(HOME)/.Xauthority:$(HOME)/.Xauthority" \
-it $(IMAGE_NAME) /bin/bash; true
docker-run: | image ## Run default command in docker
$(SCMD) docker run --rm -e "NCPU=$(NCPU)" -u $(shell id -u) -v "$(PWD)":/host \
-t $(IMAGE_NAME) /bin/bash -c "cd /host && $(DEFAULT_CMD)"
image: | .images .images/$(IMAGE_NAME) ## Build the image
.images:
$(SCMD) docker images --no-trunc | \
sed --posix 's/^\([[:alnum:]_/-]*\).*/\1/g' | \
grep -v -E 'REPOSITORY|^$$' | \
xargs -I DIR mkdir -p .images/DIR
.images/$(IMAGE_NAME): | .images
echo export HUID=\"$(shell id -u)\" > $(DOCKER_DIR)/hid.sh
echo export HNAME=\"$(shell id -n -u)\" >> $(DOCKER_DIR)/hid.sh
echo 'if command -v useradd >/dev/null 2>&1; then' >> $(DOCKER_DIR)/hid.sh
echo ' useradd -u "$$HUID" "$$HNAME" 2> /dev/null' >> $(DOCKER_DIR)/hid.sh
echo 'else' >> $(DOCKER_DIR)/hid.sh
echo ' adduser -u "$$HUID" "$$HNAME" 2> /dev/null' >> $(DOCKER_DIR)/hid.sh
echo 'fi' >> $(DOCKER_DIR)/hid.sh
chmod u+x $(DOCKER_DIR)/hid.sh
$(SCMD) docker build -t $(IMAGE_NAME) $(DOCKER_DIR)