Skip to content

Instantly share code, notes, and snippets.

@mwmahlberg
Last active February 10, 2024 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwmahlberg/83e6e1d17d9aaed6e005cf7bea73f4e9 to your computer and use it in GitHub Desktop.
Save mwmahlberg/83e6e1d17d9aaed6e005cf7bea73f4e9 to your computer and use it in GitHub Desktop.
Code for the question 77971921 on Stackoverflow
.docker-build.*

The images both exist as mwmahlberg/stackoverflow-77971921:entrypoint and mwmahlberg/stackoverflow-77971921:non-interactive in the according repo on dockerhub

Building the images

Entrypoint

docker build -t $YOURORG/$IMAGENAME:entrypoint -f Dockerfile.entrypoint

Non-interactive

docker build -t $YOURORG/$IMAGENAME:non-interactive -f Dockerfile.non-interactive

Makefile

Prerequisites

  • git
  • make (Duh!)
  • docker
  • A Dockerhub account should you want to push

Variables

Variable description default
DOCKERORG The organization on dockerhub under which the image resides mwmahlberg
DOCKERREPO The repository for the image under $DOCKERORG stackoverflow-77971921

You can set the variable per call via:

DOCKERORG="gcr.io/foobar" make

Targets

The Makefile has the following targets:

Target description
all builds both entrypoint and non-interactive
entrypoint builds just the entrypoint variant
non-interactive builds just the non-interactive variant
push pushes all images of $DOCKERORG/$DOCKERREPO
clean removes the temporary files
image-clean removes the images.
all-clean an alias for clean image-clean

Running the images

Entrypoint

docker run --rm  $YOURORG/$IMAGENAME:entrypoint

Non-interactive

docker run --rm $YOURORG/$IMAGENAME:non-interactive
Hello World
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -eo
env
FROM alpine:3
RUN apk add --no-cache bash
ADD --chmod=755 docker-entrypoint.sh /docker-entrypoint.sh
ENV MY_VAR="Hello World"
ENTRYPOINT ["/docker-entrypoint.sh"]
FROM mwmahlberg/stackoverflow-77971921:entrypoint
# The Environment variable MY_VAR is set in the parent image
ENTRYPOINT [ "/bin/bash","-c", "echo $MY_VAR"]
DOCKERORG ?= mwmahlberg
DOCKERREPO ?= stackoverflow-77971921
TAGS=entrypoint non-interactive
GIT_HASH = $(shell git log --format="%h" -n 1)
.PHONY: all push clean image-clean entrypoint non-interactive all-clean
all: $(TAGS)
entrypoint: .docker-build.entrypoint
non-interactive: .docker-build.non-interactive
.docker-build.entrypoint: Dockerfile.entrypoint docker-entrypoint.sh
docker buildx build --platform linux/amd64,linux/arm64 \
-t $(DOCKERORG)/$(DOCKERREPO):entrypoint \
-t $(DOCKERORG)/$(DOCKERREPO):entrypoint-git$(GIT_HASH) \
-f $< .
@touch $@
.docker-build.%: Dockerfile.%
docker buildx build --platform linux/amd64,linux/arm64 \
-t $(DOCKERORG)/$(DOCKERREPO):$* \
-t $(DOCKERORG)/$(DOCKERREPO):$*-git$(GIT_HASH) \
-f $< .
@touch $@
push: $(patsubst %,.docker-build.%,$(TAGS))
docker push -a $(DOCKERORG)/$(DOCKERREPO)
clean:
$(RM) .docker-build.*
image-clean:
docker rmi $(DOCKERORG)/$(DOCKERREPO):entrypoint
docker rmi $(DOCKERORG)/$(DOCKERREPO):non-interactive
docker rmi $(DOCKERORG)/$(DOCKERREPO):entrypoint-$(GIT_HASH)
docker rmi $(DOCKERORG)/$(DOCKERREPO):non-interactive-$(GIT_HASH)
all-clean: clean image-clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment