Skip to content

Instantly share code, notes, and snippets.

@lordelph
Last active August 17, 2022 06:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lordelph/bc95306979ca594b7e7e to your computer and use it in GitHub Desktop.
Save lordelph/bc95306979ca594b7e7e to your computer and use it in GitHub Desktop.
# Example Makefile for docker builds
# See http://blog.dixo.net/2015/07/building-docker-containers-with-make-on-coreos/
#-----------------------------------------------------------------------------
# configuration - see also 'make help' for list of targets
#-----------------------------------------------------------------------------
# name of container
CONTAINER_NAME = myregistry.example.com:5000/mycontainer:latest
# list of dependancies in the build context - this example just finds all files
# in the 'files' subfolder
DEPS = $(shell find myfiles -type f -print)
# name of instance and other options you want to pass to docker run for testing
INSTANCE_NAME = mycontainer
RUN_OPTS =
#-----------------------------------------------------------------------------
# default target
#-----------------------------------------------------------------------------
all : ## Build the container - this is the default action
all: build
#-----------------------------------------------------------------------------
# build container
#-----------------------------------------------------------------------------
.built: . $(DEPS)
docker build -t $(CONTAINER_NAME) .
@docker inspect -f '{{.Id}}' $(CONTAINER_NAME) > .built
build : ## build the container
build: .built
clean : ## delete the image from docker
clean: stop
@$(RM) .built
-docker rmi $(CONTAINER_NAME)
re : ## clean and rebuild
re: clean all
#-----------------------------------------------------------------------------
# repository control
#-----------------------------------------------------------------------------
push : ## Push container to remote repository
push: build
docker push $(CONTAINER_NAME)
pull : ## Pull container from remote repository - might speed up rebuilds
pull:
docker pull $(CONTAINER_NAME)
#-----------------------------------------------------------------------------
# test container
#-----------------------------------------------------------------------------
run : ## Run the container as a daemon locally for testing
run: build stop
docker run -d --name=$(INSTANCE_NAME) $(RUN_OPTS) $(CONTAINER_NAME)
stop : ## Stop local test started by run
stop:
-docker stop $(INSTANCE_NAME)
-docker rm $(INSTANCE_NAME)
#-----------------------------------------------------------------------------
# supporting targets
#-----------------------------------------------------------------------------
help : ## Show this help.
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
.PHONY : all build clean re push pull run stop help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment