Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active June 11, 2024 00:17
Show Gist options
  • Save miguelmota/c13bd2f5cc5493c82689c40117846571 to your computer and use it in GitHub Desktop.
Save miguelmota/c13bd2f5cc5493c82689c40117846571 to your computer and use it in GitHub Desktop.
Makefile docker push to AWS Elastic Container Registry (ECR)
# Login to AWS registry (must have docker running)
docker-login:
$$(aws ecr get-login --no-include-email --region us-east-1 --profile=mycompany)
# Build docker target
docker-build:
docker build -f Dockerfile --no-cache -t mycompany/myapp .
# Tag docker image
docker-tag:
$(eval REV=$(shell git rev-parse HEAD | cut -c1-7))
docker tag mycompany/myapp:latest 123.dkr.ecr.us-east-1.amazonaws.com/mycompany/myapp:latest
docker tag mycompany/myapp:latest 123.dkr.ecr.us-east-1.amazonaws.com/mycompany/myapp:$(REV)
# Push to registry
docker-push:
$(eval REV=$(shell git rev-parse HEAD | cut -c1-7))
docker push 123.dkr.ecr.us-east-1.amazonaws.com/mycompany/myapp:latest
docker push 123.dkr.ecr.us-east-1.amazonaws.com/mycompany/myapp:$(REV)
# Build docker image and push to AWS registry
docker-build-and-push: docker-login docker-build docker-tag docker-push 
@awill1988
Copy link

any ideas how to make it work with aws ecr get-login-password? get-login was removed with the new cli version and when you try to pipe it into docker login, it either complains that it's not a tty device or gives cryptic error:

/bin/sh: Login: command not found
make: *** [repo-login] Error 127

with:

$$(aws-vault exec $(PROFILE) -- aws ecr get-login-password --region $(AWS_CLI_REGION) | docker login --username AWS --password-stdin $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_CLI_REGION).amazonaws.com)

@awill1988
Copy link

Answered my own question, haha. Use bash:

docker/login:
    bash -c 'aws-vault exec $(PROFILE) -- aws ecr get-login-password --region $(AWS_CLI_REGION) | docker login --username AWS --password-stdin $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_CLI_REGION).amazonaws.com'

@majanpaul
Copy link

❤️ $$(...)

@wodCZ
Copy link

wodCZ commented Nov 24, 2020

This one works for me:

repo-login: ## Auto login to AWS-ECR unsing aws-cli
	$(eval AWS_ACCOUNT_ID=$(shell aws sts get-caller-identity --query Account --output text))
	aws ecr get-login-password --region $(AWS_CLI_REGION) | docker login --password-stdin --username AWS "$(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_CLI_REGION).amazonaws.com"

@philvarner
Copy link

I ended up with this Makefile, which uses get-login-password and uses variables to reduce duplication:

.phony: all

all: docker-build docker-push 	

IMG_NAME = myapp
$(eval AWS_ACCOUNT_ID=$(shell aws sts get-caller-identity --query Account --output text))
$(eval REV=$(shell git rev-parse HEAD | cut -c1-7))
$(eval REG=$(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com)
$(eval REPO=$(REG)/$(IMG_NAME))

# Login to ECR
ecr-login:
	aws ecr get-login-password --region $(AWS_REGION) | docker login --password-stdin --username AWS $(REG)

# Build and tag docker image
docker-build:
	docker build -f Dockerfile --no-cache -t $(IMG_NAME) .

# Push docker image
docker-push: ecr-login
	docker tag $(IMG_NAME):latest $(REPO):latest
	docker tag $(IMG_NAME):latest $(REPO):$(REV)
	docker push $(REPO):latest
	docker push $(REPO):$(REV)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment