This Makefile
is used to keep versioning of your Docker images more consistent. The first and most important thing to talk about is how "Versioning" is to be handled. The version is generated using this git command:
git describe --always --dirty --match "v[0-9]*"
Then, based on the result of that command the docker images are tagged with 3 tags:
- The project name, for easily testing locally. (ex:
project-name
) - The project name with a version. (ex:
project-name:v1.0.0-1-g91f0949
) - The project name with either
latest
oredge
.latest
if that commit was tagged, elseedge
. (ex:project-name:edge
)
And, finally, if your working directory has uncommited changes, your version will have a -dirty
at the end of it. And, it will not allow you to push that image to a registry.
That sounds confusing, here are some examples:
-
On a commit not tagged with a version and not following any commits tagged with a version.
Version:1fa88e8
Docker tags:project-name
,project-name:1fa88e8
,project-name:edge
Will push? Yes. -
When on a commit tagged with
v1.0.0
.
Version:v1.0.0
Docker tags:project-name
,project-name:v1.0.0
,project-name:latest
Will push? Yes. -
When on a commit 1 after
v1.0.0
.
Version:v1.0.0-1-g91f0949
Docker tags:project-name
,project-name:v1.0.0-1-g91f0949
,project-name:edge
Will push? Yes. -
When on a commit 1 after
v1.0.0
, but your working directory has uncommited changes..
Version:v1.0.0-1-g91f0949-dirty
Docker tags:project-name
,project-name:v1.0.0-1-g91f0949-dirty
,project-name:edge
Will push? No.
Pretty neat, huh?
Okay, let's get started!
-
Copy
Makefile
into your project, it should be in the same directory as yourDockerfile
-
Set
DOCKER_REGISTRY
,DOCKER_USERNAME
&PROJECT_NAME
at the top of the Makefile as desired. -
(optional) Edit recipes under the
RECIPES
header, or add your own.For example, if you needed to mount a volume, your
run
might look something like:# ==== RECIPES ==== run: build docker run --rm -v /path/on/host:/path/on/container $(PROJECT_NAME) run-it: build docker run --rm -v /path/on/host:/path/on/container --entrypoint /bin/sh -it $(PROJECT_NAME)
Will build & tag the docker image.
Attemps to log you into the docker registry. (See "(Registry Login)[https://github.com/AaronNBrock/docker-makefile#registry-login]")
If the workding directory isn't dirty, will build, tag then push to the docker registry.
Prints the version, and what the docker tags would be if built.
Will build, tag, and run the docker image.
Will build, tag, and run the docker image in interactive mode.
This Makefile
also supports automatically logging in using Environment variables, which can be useful if running this Makefile
inside a build server, like Jenkins.
To do this, simply pass the username and password in as USERNAME
and PASSWORD
.
USERNAME=user1 PASSWORD=123456 make login
(note: You only need to specify the username if it differs from the DOCKER_USERNAME
.)