Skip to content

Instantly share code, notes, and snippets.

@JackMoth
Forked from mpneuried/Makefile
Created March 16, 2018 08:46
Show Gist options
  • Save JackMoth/abeed8614579cf766be6130443a5994d to your computer and use it in GitHub Desktop.
Save JackMoth/abeed8614579cf766be6130443a5994d to your computer and use it in GitHub Desktop.
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
include config.env
export $(shell sed 's/=.*//' config.env)
# import deploy config
include deploy.env
export $(shell sed 's/=.*//' deploy.env)
# 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 --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)
# 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 (needs to executed in a seperate shell)
make stop
# 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