23rd June 2017

23rd June 2017 at 12:56pm
@digarcia Docker Journal
Basically I have it on the root directory, and then hold all the dockerfiles in subdirectories under the “dockerfiles” dir, so you can pass as a parameter the name of the container youy want to build.
Nothing too crazy, and sure I’m doing something wrong ^_^
 
├── Makefile
├── README
├── dockerfiles
│   ├── base
│   │   ├── Dockerfile
│   │   └── entrypoint.sh
│   ├── consul
│   │   └── Dockerfile
│   ├── java
│   │   └── Dockerfile
│   ├── mucous
│   │   ├── Dockerfile
│   │   └── config
│   ├── museekd
│   │   ├── Dockerfile
│   │   └── config.xml
│   ├── node-npm
│   │   ├── Dockerfile
│   │   └── entrypoint.sh
│   ├── ssh-server
│   │   └── Dockerfile
│   └── terraform
│       └── Dockerfile

The Makefile
 
# Project Dir
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
 
# Directory that holds lambda source code
SOURCE_DIR = $(ROOT_DIR)/dockerfiles
 
# Container prefix
PREFIX:= local
 
# Container name to build
CONTAINER_NAME := base
 
# Container name to build
VERSION := latest
 
# Known Functions in source Dir
CONTAINER_DIR_NAMES := ${shell cd $(SOURCE_DIR) && find -maxdepth 1 -mindepth 1 -type d -exec basename {} \;}
 
.DEFAULT_TARGET := build
#.PHONY: build
 
build: _check-params
      pushd $(SOURCE_DIR)/$(CONTAINER_NAME); \
      docker build -t $(PREFIX)/$(CONTAINER_NAME):$(VERSION) . ;\
      popd
      @$(DONE)
 
build-clean: _check-params
      pushd $(SOURCE_DIR)/$(CONTAINER_NAME); \
      docker build -t $(PREFIX)/$(CONTAINER_NAME):$(VERSION) --rm . ;\
      popd
      @$(DONE)
 
build-all:
      @for f in $(CONTAINER_DIR_NAMES); do \
            make build CONTAINER_NAME=$$f; \
      done
      @$(DONE)
 
test: _check-params
      pushd $(SOURCE_DIR)/$(CONTAINER_NAME); \
      docker run -it --rm $(PREFIX)/$(CONTAINER_NAME):$(VERSION) bash ;\
      popd
      @$(DONE)
 
tag_latest: _check-params
      pushd $(SOURCE_DIR)/$(CONTAINER_NAME); \
      docker tag $(PREFIX)/$(CONTAINER_NAME):$(VERSION) $(PREFIX)/$(CONTAINER_NAME):latest ;\
      popd
      @$(DONE)
 
release:  _check-params test tag_latest
      pushd $(SOURCE_DIR)/$(CONTAINER_NAME); \
      @if ! docker images $(PREFIX)/$(CONTAINER_NAME) | awk '{ print $$2 }' | grep -q -F $(VERSION); then echo "$(PREFIX)/$(CONTAINER_NAME) version $(VERSION) is not yet built. Please run 'make build'"; false; fi ;\
      docker push $(PREFIX)/$(CONTAINER_NAME) ;\
      @echo "*** Don't forget to create a tag. git tag rel-$(VERSION) && git push origin rel-$(VERSION)" ;\
      popd
      @$(DONE)
 
run: _check-params
      pushd $(SOURCE_DIR)/$(CONTAINER_NAME); \
      docker run -i -P $(PREFIX)/$(CONTAINER_NAME):$(VERSION) ;\
      popd
      @$(DONE)
 
_check-params:
ifndef CONTAINER_NAME
      @echo "You must provide a CONTAINER_NAME variable";
      @echo "Select a valid container name
      @false;
endif