Skip to content

Instantly share code, notes, and snippets.

@letenkov
Forked from SmartFinn/DockerHelper.mk
Created December 31, 2017 14:50
Show Gist options
  • Save letenkov/ba64e57129aff75ec4dab8a8cdedc39f to your computer and use it in GitHub Desktop.
Save letenkov/ba64e57129aff75ec4dab8a8cdedc39f to your computer and use it in GitHub Desktop.
Improved Makefile for Docker
OWNER ?= $(USER)
REPO ?= $(notdir $(CURDIR))
TAG ?= latest
IMAGE ?= $(OWNER)/$(REPO):$(TAG)
NAME ?= $(REPO)
DOCKER ?= $(shell which docker)
OPTIONS ?= --interactive --rm --tty
CMD ?=
all: build
attach diff inspect logs port stats top:
@$(DOCKER) $@ $(NAME)
build:
$(DOCKER) $@ --force-rm --tag $(IMAGE) $(CURDIR)
cleanup:
@$(DOCKER) images -q -f "dangling=true" | \
xargs -I {} $(DOCKER) rmi {}
@$(DOCKER) volume ls -q -f "dangling=true" | \
xargs -I {} $(DOCKER) volume rm {}
destroy: stop rm
history pull:
@$(DOCKER) $@ $(IMAGE)
kill pause restart start stop unpause:
@$(DOCKER) $@ $(NAME) | \
xargs -I {} echo " ---> $@ container '{}'"
ps:
@$(DOCKER) $@ -a --filter=name=$(NAME)
purge: stop rm rmi cleanup
push:
$(DOCKER) login
$(DOCKER) $@ $(IMAGE)
rebuild: stop rm rmi build
rm:
@$(DOCKER) $@ --force --volumes $(NAME) | \
xargs -I {} echo " ---> remove container '{}'"
rmi:
@$(DOCKER) $@ $(IMAGE) | \
xargs -I {} echo " ---> {}"
run:
$(DOCKER) $@ $(OPTIONS) --name $(NAME) $(IMAGE) $(CMD)
shell:
@$(DOCKER) exec -it $(NAME) sh -c "/bin/bash || /bin/sh"
supervisorctl:
@$(DOCKER) exec -it $(NAME) $@
up: run
.PHONY: all attach build cleanup destroy diff history inspect \
kill logs pause port ps pull purge push rebuild restart rm \
rmi run shell start stats stop supervisorctl top unpause up

Usage

~/P/dockerfiles/tftpd $ tree

.
├── Dockerfile
├── Makefile
└── Dockerfile.mk

0 directories, 3 files

~/P/dockerfiles/tftpd $ cat Dockerfile

FROM ubuntu:trusty

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && \
        apt-get install -y tftpd-hpa

VOLUME /tftpboot

EXPOSE 69/udp

ENTRYPOINT ["in.tftpd"]
CMD ["--foreground", "--verbose", "--user", "tftp", "--secure", "/tftpboot"]

~/P/dockerfiles/tftpd $ cat Makefile

REPO    := tftpd
OWNER   := smartfinn
OPTIONS := --detach=true --publish=69:69/udp \
        --volume=/etc/localtime:/etc/localtime:ro \
        --volume=$(CURDIR):/tftpboot

include ./Dockerfile.mk

~/P/dockerfiles/tftpd $ make

/usr/bin/docker build --force-rm --tag smartfinn/tftpd:latest .
Sending build context to Docker daemon  5.12 kB
Step 1 : FROM ubuntu:trusty
 ---> 4ae3d38a34c6
Step 2 : ENV DEBIAN_FRONTEND noninteractive
 ---> Using cache
 ---> 12c6a726b2ce
Step 3 : RUN apt-get update &&  apt-get install -y tftpd-hpa
 ---> Running in ad03f2514a97
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:2 http://archive.ubuntu.com trusty-security InRelease [65.9 kB]
[...]
invoke-rc.d: policy-rc.d denied execution of start.
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
Processing triggers for ureadahead (0.100.0-16) ...
 ---> 86af2933b2f7
Removing intermediate container ad03f2514a97
Step 4 : VOLUME /tftpboot
 ---> Running in 6442d2e0b590
 ---> efe7ea8ecf57
Removing intermediate container 6442d2e0b590
Step 5 : EXPOSE 69/udp
 ---> Running in d442bd2d10e3
 ---> ad166fcc8ea8
Removing intermediate container d442bd2d10e3
Step 6 : ENTRYPOINT in.tftpd
 ---> Running in 7c060ddaeef0
 ---> 2fe43536bb67
Removing intermediate container 7c060ddaeef0
Step 7 : CMD --foreground --verbose --user tftp --secure /tftpboot
 ---> Running in a427f7d937b4
 ---> 84cb9f286efe
Removing intermediate container a427f7d937b4
Successfully built 84cb9f286efe

~/P/dockerfiles/tftpd $ make run

/usr/bin/docker run --detach=true --publish=69:69/udp --volume=/etc/localtime:/etc/localtime:ro --volume=/home/finn/Projects/dockerfiles/tftpd:/tftpboot --name tftpd smartfinn/tftpd:latest
9149878cfbe2d830e8e042ec45c3187598c709cf3532c340a92cb82779d99336

~/P/dockerfiles/tftpd $ make ps

CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                NAMES
9149878cfbe2        smartfinn/tftpd:latest   "in.tftpd --foregroun"   2 minutes ago       Up 2 minutes        0.0.0.0:69->69/udp   tftpd

~/P/dockerfiles/tftpd $ make purge

 ---> Stop container 'tftpd'
 ---> Remove container 'tftpd'
 ---> Untagged: smartfinn/tftpd:latest
 ---> Deleted: sha256:84cb9f286efec2d7dbe5ea77381682c0ca8e61581d5e1caafcca49e01a2a720b
 ---> Deleted: sha256:2fe43536bb67e72fa42a320109f041e1d48ce1bb82c309c18d4cef47c80dc44d
 ---> Deleted: sha256:ad166fcc8ea8cf179f2a435a61b07242a385b724e76951690e2f3ee987ac39dd
 ---> Deleted: sha256:efe7ea8ecf57ae0193b419d00e3bc0aa4040386689bad905f31edef047874774
 ---> Deleted: sha256:86af2933b2f7dbc8b59d0bac25575541cd7e8d7d4bf526f3e9c8851c6ad38a49
 ---> Deleted: sha256:4c3b53c383c45ed9d189b4bf8660cae56078596cc5b072258c783bf0020a57c4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment