Skip to content

Instantly share code, notes, and snippets.

@SmartFinn
Last active December 3, 2022 11:51
Show Gist options
  • Save SmartFinn/7bb86b078726f0763ce0 to your computer and use it in GitHub Desktop.
Save SmartFinn/7bb86b078726f0763ce0 to your computer and use it in GitHub Desktop.
Makefile for Docker as a simple replacement for docker-compose for working with a single service
# DockerHelper.mk is a simple replacement for docker-compose for
# working with a single service.
#
# Usage Examples:
# - https://gist.github.com/SmartFinn/7bb86b078726f0763ce0#file-usage-md
# - https://github.com/SmartFinn/Dockerfiles
#
# @author: Sergei Eremenko (https://github.com/SmartFinn)
# @license: Unlicense (http://unlicense.org)
# @link: https://gist.github.com/SmartFinn/7bb86b078726f0763ce0
NAME ?= $(notdir $(CURDIR))
IMAGE ?= $(NAME)
DOCKER ?= $(shell command -v podman docker | head -1)
BUILD_OPTIONS ?= --force-rm
PODMAN_SYSTEMD_OPTIONS ?= --new
POD_NAME ?= $(shell echo $(RUN_OPTIONS) | grep -Po -- "--pod[= '\"]\K[^ '\"]+")
NET_NAME ?= $(shell echo $(RUN_OPTIONS) | grep -Po -- "--net(|work)[= '\"]\K[^ '\"]+")
NET_OPTIONS ?=
RUN_COMMAND ?=
RUN_OPTIONS ?= --interactive --rm --tty
default: build
diff logs port stats top:
@$(DOCKER) container $@ $(NAME)
build:
$(DOCKER) build $(BUILD_OPTIONS) --tag $(IMAGE) $(CURDIR)
destroy: down
down: stop rm
images: ls
history ls pull:
@$(DOCKER) image $@ $(IMAGE)
kill pause restart start stop unpause:
@$(DOCKER) $@ $(NAME) | \
xargs -I {} echo " ---> $@ container '{}'"
_network:
@if $(DOCKER) network ls -q --filter name='^$(NET_NAME)$$' | xargs -r false; then \
case "$(NET_NAME)" in \
""|none|container:*|host|ns:*|private|slirp4netns*) true ;; \
*) $(DOCKER) network create $(NET_OPTIONS) $(NET_NAME) ;; \
esac; \
fi
network:
@$(DOCKER) $@ ls --filter=name=$(NET_NAME)
pre-run post-run::
prune:
@$(DOCKER) system prune -f
ps:
@$(DOCKER) container ls --all --filter=name=$(NAME)
purge: stop rm rmi prune
push:
$(DOCKER) image push $(IMAGE)
rebuild: rmi build
rm:
@$(DOCKER) container rm --force --volumes $(NAME) | \
xargs -I {} echo " ---> remove container '{}'"
rmi:
@$(DOCKER) image rm $(IMAGE) | \
xargs -I {} echo " ---> {}"
_run:
$(DOCKER) run $(RUN_OPTIONS) --name $(NAME) $(IMAGE) $(RUN_COMMAND)
run: _network pre-run _run post-run
shell:
@$(DOCKER) container exec --interactive --tty $(NAME) \
sh -c "/bin/bash || /bin/sh"
systemd:
cd $(if $(filter 0,$(shell id -u)),\
/etc/systemd/system,\
$${XDG_CONFIG_HOME:-~/.config}/systemd/user) && \
$(DOCKER) generate systemd $(or $(POD_NAME),$(NAME)) --name --files \
$(PODMAN_SYSTEMD_OPTIONS)
systemctl $(if $(filter-out 0,$(shell id -u)),--user) daemon-reload
up: run
.PHONY: default build destroy down diff images history kill logs ls \
network _network pause port post-run pre-run prune ps pull purge \
push rebuild restart rm rmi _run run shell start stats stop \
systemd top unpause up

Usage

Run Redis with custom network from image hosted on Docker Hub

~/D/dockerfiles/redis-alpine $ tree

.
├── DockerHelper.mk
└── Makefile

0 directories, 2 files

~/D/dockerfiles/redis-alpine $ cat Makefile

-include DockerHelper.mk

NAME  := redis
IMAGE := redis:alpine
NET_OPTIONS := --driver=bridge
RUN_COMMAND := redis-server --appendonly yes
RUN_OPTIONS := \
	--detach=true \
	--env=TZ=Europe/Kiev \
	--network=backend \
	--restart=unless-stopped \
	--user=$(shell id -u):$(shell id -g) \
	--volume="$(HOME)/.redis":/data

# create directory before run if not exist
pre-run::
	test -d "${HOME}/.redis" || mkdir -p "${HOME}/.redis"

~/D/dockerfiles/redis-alpine $ make run

test -d "/home/sergei/.redis" || mkdir -p "/home/sergei/.redis"
docker run --detach=true --env=TZ=Europe/Kiev --network=backend --restart=unless-stopped --user=65534:65534 --volume=""/home/sergei/.redis"":/data --name redis redis:alpine redis-server --appendonly yes
Unable to find image 'redis:alpine' locally
alpine: Pulling from library/redis
9d48c3bd43c5: Already exists
6bcae78f4e99: Pull complete
8cb2d2938e96: Pull complete
8144cfccc349: Pull complete
4ff1d177b4d1: Pull complete
a67c684f8341: Pull complete
Digest: sha256:50899ea1ceed33fa03232f3ac57578a424faa1742c1ac9c7a7bdb95cdf19b858
Status: Downloaded newer image for redis:alpine
801ebd96137799a8473d94445758f36c889bb6c90846dc3857c18dea9536edce

~/D/dockerfiles/redis-alpine $ make ps

CONTAINER ID    IMAGE           COMMAND                 CREATED         STATUS          PORTS       NAMES
801ebd961377    redis:alpine    "docker-entrypoint.s…"  1 minutes ago   Up 1 minutes    6379/tcp    redis

~/D/dockerfiles/redis-alpine $ make networks

NETWORK ID          NAME                DRIVER              SCOPE
ecdc7a1d27c3        backend             bridge              local

Build and run Transmission from Dockerfile

~/D/dockerfiles/transmission $ tree

.
├── Dockerfile
├── DockerHelper.mk
├── Makefile
└── transmission.settings.json

0 directories, 4 files

~/D/dockerfiles/transmission $ cat Dockerfile

FROM alpine:latest
LABEL maintainer "SmartFinn (https://github.com/SmartFinn)"

ENV TRANSMISSION_HOME=/config \
    HOME=/config

RUN apk --no-cache add transmission-daemon \
 && test -d /config || mkdir -p /config \
 && chown -R transmission:nobody /config /media

COPY transmission.settings.json /config/settings.json

EXPOSE 9091

USER transmission

VOLUME ["/config", "/media"]

ENTRYPOINT ["transmission-daemon"]
CMD ["--foreground", "--log-error"]

~/D/dockerfiles/transmission $ cat Makefile

-include DockerHelper.mk

IMAGE := transmission
RUN_OPTIONS := \
	--detach=true \
	--publish=9091:9091 \
	--restart=unless-stopped \
	--blkio-weight=400 \
	--cpu-shares=512 \
	--user=$(shell id -u):$(shell id -g) \
	--volume=/etc/localtime:/etc/localtime:ro \
	--volume=$(HOME)/.transmission:/config \
	--volume=/media:/media

# create directory if not exist and copy the config
pre-run::
	test -d "${HOME}/.transmission" || mkdir -p "${HOME}/.transmission"
	test -f "${HOME}/.transmission/settings.json" \
	|| cp transmission.settings.json "${HOME}/.transmission/settings.json")

~/D/dockerfiles/transmission $ make build

docker build --force-rm --tag transmission:latest /home/sergei/Development/dockerfiles/transmission
Sending build context to Docker daemon  10.24kB
Step 1/10 : FROM alpine:latest
 ---> 961769676411
[ ... ]
Step 10/10 : CMD ["--foreground", "--log-error"]
 ---> Running in ffddb75c296e
Removing intermediate container ffddb75c296e
 ---> 1c97ff194f17
Successfully built 1c97ff194f17
Successfully tagged transmission:latest

~/D/dockerfiles/transmission $ make ls

REPOSITORY     TAG            IMAGE ID        CREATED              SIZE
transmission   latest         1c97ff194f17    About a minute ago   8.63MB

~/D/dockerfiles/transmission $ make run

test -d "/home/sergei/.transmission" || mkdir -p "/home/sergei/.transmission"
test -f "/home/sergei/.transmission/settings.json" || cp transmission.settings.json "/home/sergei/.transmission/settings.json"
docker run --detach=true --publish=9091:9091 --restart=unless-stopped --blkio-weight=400 --cpu-shares=512 --user=1000:1000 --volume=/etc/localtime:/etc/localtime:ro --volume=/home/sergei/.transmission:/config --volume=/media:/media --name transmission transmission:latest
40d9dc1b482fa9fbe487ac9af1f1b21be3827c312a7c9a16517afe26fb1676f1

~/D/dockerfiles/transmission $ make ps

CONTAINER ID    IMAGE                 COMMAND                  CREATED              STATUS              PORTS                    NAMES
40d9dc1b482f    transmission:latest   "transmission-daemon…"   About a minute ago   Up About a minute   0.0.0.0:9091->9091/tcp   transmission

~/D/dockerfiles/transmission $ make destroy

 ---> stop container 'transmission'
 ---> remove container 'transmission

~/D/dockerfiles/transmission $ make rmi

 ---> Untagged: transmission:latest
 ---> Deleted: sha256:1c97ff194f17c19b48119da2a5d0309e241a02855304a865e5c519ee9991c382
 ---> Deleted: sha256:5528b39d926446121fe27c25bc2f3ee83b9e770914b06d0d099b8de10afe3e25
 ---> Deleted: sha256:c573d330813332267d9506f50865c8584aa2e25e81eb5e7b99a81ead8bbdf558
 ---> Deleted: sha256:b4246ad27ae313f76415d7948961395d4cf4d5b7912fa867db068f41be7203c1
 ---> Deleted: sha256:527771c6a2fecf4109d7142eebab201d099a30c43c0f15d955b37f0834766ac0
 ---> Deleted: sha256:cf0fe6c9500b061566f5d868049c5044a06fb2adf15ecf208ea4f3e00dabcd0e
 ---> Deleted: sha256:66f823940fee9b30213aef3eb76ca1644df6f54a0f9fd4b6d912ba6eedfccaaf
 ---> Deleted: sha256:1c6b8e09583cfac98712ca7a6fa592ca21168617acf2c732d6ea5fe96062edef
 ---> Deleted: sha256:b8818a84d03d8466e63deed56d53c65670a53743c3838aaee65bc5bb1f242cd2
 ---> Deleted: sha256:6db46b590a7b93787e58453af8c0a70968b6c0c7fe54b624b9fe7b5d5deb1f9a

More examples https://github.com/SmartFinn/Dockerfiles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment