Skip to content

Instantly share code, notes, and snippets.

@josephbolus
Forked from mpneuried/Makefile
Created Aug 21, 2017
Embed
What would you like to do?
Simple Makefile to build, run, tag and publish a docker containier to AWS-ECR
# Port to run the container
PORT=4000
# Until here you can define all the individual configurations for your app
# You have to define the values in {}
APP_NAME=my-super-app
DOCKER_REPO={account-nr}.dkr.ecr.{region}.amazonaws.com
# optional aws-cli options
AWS_CLI_PROFILE={aws-cli-profile}
AWS_CLI_REGION={aws-cli-region}
# import config
configfile=config.env
ifdef cnf
configfile=$(cnf)
endif
include $(configfile)
export $(shell sed 's/=.*//' $(configfile))
# import deploy config
deployfile=deploy.env
ifdef dpl
deployfile=$(dpl)
endif
include $(deployfile)
export $(shell sed 's/=.*//' $(deployfile))
# grep the version from the mix file
VERSION=$(shell ./version.sh)
# DOCKER TASKS
# Build the container
build:
docker build -t $(APP_NAME) .
# Build the container without cache
build-nc:
docker build --no-cache -t $(APP_NAME) .
# Run container on port configured in `config.env`
run:
docker run -i -t --rm --env-file=./config.env -p=$(PORT):$(PORT) --name="$(APP_NAME)" $(APP_NAME)
# Build and run the container
up: build run
stop:
docker stop $(APP_NAME); docker rm $(APP_NAME)
# Docker release - build, tag and push the container
release: build-nc publish
# Docker publish
publish: repo-login publish-latest publish-version
publish-latest: tag-latest
@echo 'publish latest to $(DOCKER_REPO)'
docker push $(DOCKER_REPO)/$(APP_NAME):latest
publish-version: tag-version
@echo 'publish $(VERSION) to $(DOCKER_REPO)'
docker push $(DOCKER_REPO)/$(APP_NAME):$(VERSION)
# Docker tagging
tag: tag-latest tag-version
tag-latest:
@echo 'create tag latest'
docker tag $(APP_NAME) $(DOCKER_REPO)/$(APP_NAME):latest
tag-version:
@echo 'create tag $(VERSION)'
docker tag $(APP_NAME) $(DOCKER_REPO)/$(APP_NAME):$(VERSION)
# HELPERS
# generate script to login to aws docker repo
CMD_REPOLOGIN := "eval $$\( aws ecr"
ifdef AWS_CLI_PROFILE
CMD_REPOLOGIN += " --profile $(AWS_CLI_PROFILE)"
endif
ifdef AWS_CLI_REGION
CMD_REPOLOGIN += " --region $(AWS_CLI_REGION)"
endif
CMD_REPOLOGIN += " get-login \)"
# login to AWS-ECR
repo-login:
@eval $(CMD_REPOLOGIN)
# output to version
version:
@echo $(VERSION)
### THIS IST THE VERSION WITH docker-compose
# import config
configfile=config.env
ifdef cnf
configfile=$(cnf)
endif
include $(configfile)
export $(shell sed 's/=.*//' $(configfile))
# import deploy config
deployfile=deploy.env
ifdef dpl
deployfile=$(dpl)
endif
include $(deployfile)
export $(shell sed 's/=.*//' $(deployfile))
# grep the version from the mix file
VERSION=$(shell ./version.sh)
# DOCKER TASKS
# Build the container
build:
docker-compose build --no-cache $(APP_NAME)
docker-compose run $(APP_NAME) grunt build
docker build -t $(APP_NAME) .
# Run container on port configured in `config.env`
run: stop
docker run -i -t --rm --env-file=./config.env -p=$(PORT):$(PORT) --name="$(APP_NAME)" $(APP_NAME)
dev:
docker-compose build --no-cache test && docker-compose run test
# Build and run the container
up:
docker-compose up --build $(APP_NAME)
stop:
docker stop $(APP_NAME)
rm: stop
docker rm $(APP_NAME)
tc=1
ifdef count
tc=$(count)
endif
test:
docker-compose build --no-cache test
@if [ $(tc) = 1 ]; then\
docker-compose run test\
else\
docker-compose scale test=$(tc)\
docker-compose logs -f test\
fi
test-live:
docker-compose build --no-cache testlive
@if [ $(tc) = 1 ]; then\
docker-compose run testlive\
else\
docker-compose scale testlive=$(tc)\
docker-compose logs -f testlive\
fi
# Docker release - build, tag and push the container
release: build publish
# Docker publish
publish: repo-login publish-latest publish-version
publish-latest: tag-latest
@echo 'publish latest to $(DOCKER_REPO)'
docker push $(DOCKER_REPO)/$(APP_NAME):latest
publish-version: tag-version
@echo 'publish $(VERSION) to $(DOCKER_REPO)'
docker push $(DOCKER_REPO)/$(APP_NAME):$(VERSION)
# Docker tagging
tag: tag-latest tag-version
tag-latest:
@echo 'create tag latest'
docker tag $(APP_NAME) $(DOCKER_REPO)/$(APP_NAME):latest
tag-version:
@echo 'create tag $(VERSION)'
docker tag $(APP_NAME) $(DOCKER_REPO)/$(APP_NAME):$(VERSION)
# HELPERS
# generate script to login to aws docker repo
CMD_REPOLOGIN := "eval $$\( aws ecr"
ifdef AWS_CLI_PROFILE
CMD_REPOLOGIN += " --profile $(AWS_CLI_PROFILE)"
endif
ifdef AWS_CLI_REGION
CMD_REPOLOGIN += " --region $(AWS_CLI_REGION)"
endif
CMD_REPOLOGIN += " get-login \)"
# login to AWS-ECR
repo-login:
@eval $(CMD_REPOLOGIN)
# output to version
version:
@echo $(VERSION)
# INSTALL
# - copy the files deploy.env, config.env, version.sh and Makefile to your repo
# - replace the vars in deploy.env
# - define the version script
# Build the container
make build
# Build and publish the container
make release
# Publish a container to AWS-ECR.
# This includes the login to the repo
make publish
# Run the container
make run
# Build an run the container
make up
# Stop the running container
make stop
# Build the container with differnt config and deploy file
make cnf=another_config.env dpl=another_deploy.env build
# Example version script.
# Please choose one version or create your own
# Node.js: grep the version from a package.json file with jq
jq -rM '.version' package.json
# Elixir: grep the version from a mix file
cat mix.exs | grep version | grep '\([0-9]\+\.\?\)\{3\}' -o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment