Uniq Docker image name with git and Makefile


What is Makefile?

In software development, Make is a build automation tool that automatically builds executable programs and libraries from source code by reading files called Makefiles which specify how to derive the target program.

Make tool on GNU website: https://www.gnu.org/software/make/

Wiki article: https://en.wikipedia.org/wiki/Make_(software)

Makefile

Let’s take a closer look at our script. We’re setting four variables:

  • NAME: your project url in your container registry (GCP example)
  • TAG: we’re running git log and doing pretty output to get the uniq hash
  • IMG: just building uniq name from NAME and TAG
  • LATEST: making our image latest

Makefile syntax assumes you’re using tab identation under the targets, so:

  • show: just a example how you can debug something in your Makefile
  • build: as a first step we’re building docker image (Makefile should be located in a root of your project), and adding tag after that
  • push: push compiled image name to container registry
NAME   := gcr.io/PROJECT_NAME/APP_NAME
TAG    := $$(git log -1 --pretty=%H)
IMG    := ${NAME}:${TAG}
LATEST := ${NAME}:latest

show:
	@echo ${NAME}

build:
	@docker build -t ${IMG} .
	@docker tag ${IMG} ${LATEST}
 
push:
	@docker push ${NAME}

Run it!

Lets run our makefile with two arguments:

~/P/project ❯❯❯ make build push
[+] Building 1.7s (15/15) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                    0.0s
 => => transferring dockerfile: 37B                                                                                                                                                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                       0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/ruby:2.6.2                                                                                                                                                                                                           0.7s
 => [ 1/10] FROM docker.io/library/ruby:2.6.2@sha256:d5af6b19da8381014f59e79245ae242dd5ea8dfe1a8a6c0e2bc481366f1e92b9                                                                                                                                                   0.0s
 => [internal] load build context                                                                                                                                                                                                                                       0.2s
 => => transferring context: 108.18kB                                                                                                                                                                                                                                   0.2s
 => CACHED [ 2/10] RUN apt-get update && apt-get install -y build-essential git                                                                                                                                                                                         0.0s
 => CACHED [ 3/10] RUN apt-get install -y libpq-dev                                                                                                                                                                                                                     0.0s
 => CACHED [ 4/10] RUN apt-get install -y nodejs                                                                                                                                                                                                                        0.0s
 => CACHED [ 5/10] RUN mkdir -p /app                                                                                                                                                                                                                                    0.0s
 => CACHED [ 6/10] WORKDIR /app                                                                                                                                                                                                                                         0.0s
 => CACHED [ 7/10] COPY Gemfile Gemfile.lock ./                                                                                                                                                                                                                         0.0s
 => CACHED [ 8/10] RUN gem install bundler && bundle install --jobs 20 --retry 5                                                                                                                                                                                        0.0s
 => [ 9/10] COPY .                                                                                                                                                                                                                                                      0.2s
 => [10/10] RUN chmod +x /app/entrypoint.sh                                                                                                                                                                                                                             0.3s
 => exporting to image                                                                                                                                                                                                                                                  0.2s
 => => exporting layers                                                                                                                                                                                                                                                 0.2s
 => => writing image sha256:2cf5dc6c1497e2f7d8e41324beadbc6454d5c06368d16178fc30012c3fe1e703                                                                                                                                                                            0.0s
 => => naming to gcr.io/XXX/mailer:686e1510158c2345a1d8c9682812d84fe5fb1204                                                                                                                                                                             0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Using default tag: latest
The push refers to repository [gcr.io/XXX/XXX]
ec5d055ae368: Pushed
d46b999c7992: Pushed
32725e93bb5f: Pushed
d187c562ae32: Pushed
5f70bf18a086: Layer already exists
9e3e0e756253: Pushed
d50e90fcad3d: Pushed
d74c03d8f89b: Pushed
23969516fc44: Pushed
eaeee68de836: Pushed
45d35e2dcbbc: Pushed
d43f42c485cd: Pushed
0fe19df8b8f8: Layer already exists
b17cc31e431b: Layer already exists
12cb127eee44: Layer already exists
604829a174eb: Layer already exists
fbb641a8b943: Layer already exists
latest: digest: sha256:105e55071dec6c6c1982a100f83ee791ea128775fa75edc2e71e9299ad332b85 size: 3889
~/P/project ❯❯❯

Super simple and helpful! Stay tuned.