WhenworkingwiththeDockerhub,twosmallthingsbotheredme:
WaitingforyourbuildtostartNoeasycontroloverthetagsfortheimages.Toresolvethesetoissues,IcreatedagenericMakefilethatallowsyoutobuildandreleasedockerimagesbasedupongittags,wheneveryouwant.
MakefiletargetsTheMakefilehasthefollowingtargets:
makepatch-releaseincrementsthepatchreleaselevel,buildandpushtoregistrymakeminor-releaseincrementstheminorreleaselevel,buildandpushtoregistrymakemajor-releaseincrementsthemajorreleaselevel,buildandpushtoregistrymakereleasebuildthecurrentreleaseandpushtheimagetotheregistrymakebuildbuildsanewversionofyourDockerimageandtagsitmakesnapshotbuildfromthecurrent(dirty)workspaceandpushestheimagetotheregistrymakecheck-statuswillcheckwhetherthereareoutstandingchangesmakecheck-releasewillcheckwhetherthecurrentdirectorymatchesthetaggedreleaseingit.makeshowverwillshowthecurrentreleasetagbasedonthedirectorycontent.Howtouseit.copytheMakefileand.make-release-supportintoyourDockergitproject:
wget-OMakefile.mkhttps://raw.githubusercontent.com/mvanholsteijn/docker-makefile/master/Makefilewgethttps://raw.githubusercontent.com/mvanholsteijn/docker-makefile/master/.make-release-supportChangeregistry,userorimagenameBydefault,theregistryissettodocker.ioandtheusertothecurrentuser.Tooverridethis,edittheMakefileandsetthevariablesREGISTRY_HOST,USERNAMEandNAME.
includeMakefile.mkREGISTRY_HOST=myregistry.ioUSERNAME=mvanholsteijnNAME=awesome-imageBuildinganimagetobuildanimage,addaDockerfiletoyourdirectoryandtypemake:
makeReleaseTomakeareleaseandtagit,commitaddthechangesandtype:
makepatch-releaseThiswillbumpthepatch-releasenumber,buildtheimageandpushittotheregistry.Itwillonlyreleaseiftherearenooutstandingchangesandthecontentofthedirectoryequalsthetaggedcontent.
Alternativelyyoucanchoose'makeminor-release'or'makemajor-release'tobumptheassociatednumber.
ReleasenumberThereleaseofyourdockerimageiskeptinthefile.releaseandusesthefollowingformat:
release=<major>.<minor>.<patch>Thenameofthegittagiskeptinthesamefile,andbydefaultwillhavetheformat:
tag=<directory-name>.<minor>.<patch>ThiswillallowyoutohavetrackandtagmultipleimagesinasingleGitrepository.
Ifyouwanttouseadifferenttagprefix,changeitinthe.release.
ImagenameandtagThenameoftheimagewillbecreatedasfollows:
<registry-host>/<username>/<directoryname>:<tag>Thetagishasthefollowingformat:
formatwhen<release>thecontentsofthedirectoryisequaltotaggedcontentingit<release>-<commit>thecontentsofthedirectoryisnotequaltothetaggedcontent<release>-<commit>-dirtythecontentsofthedirectoryhasuncommittedchangesMultipledockerimagesinasinglegitrepository.Ifyouwanttomaintainmultipledockerimagesinasinglegitrepository,youcanuseanalternatesetupwheretheMakefileislocatedinasilbingdirectory.
├──multiple-example│ ├──...│ ├──image1│ │ ├──.release│ │ ├──Dockerfile│ │ └──Makefile│ ├──image2│ │ ├──.release│ │ ├──Dockerfile│ │ └──Makefile│ └──make│ ├──.make-release-support│ ├──MakefileTheMakefileintheimagedirectorieswillincludethegenericMakefile.InthisMakefileyoucanalterthenamesandtailorthebuildbyaddingpreandpostbuildtargets.Checkoutthedirectory(multiple-example)foranexample.
CreatethegenericmakedirectoryTocreatethegenericmakedirectory,type:
mkdirmakecdmakewgethttps://raw.githubusercontent.com/mvanholsteijn/docker-makefile/master/Makefilewgethttps://raw.githubusercontent.com/mvanholsteijn/docker-makefile/master/.make-release-supportCreatedockerimagedirectoryForeachdockerimages,youcreateasiblingdirectory:
mkdir../image1cd../image1cat>Makefile<<!include../make/MakefileUSERNAME=mvanholsteijnpre-build:@echodosomestuffbeforethedockerbuildpost-build:@echodosomestuffafterthedockerbuild!Nowyoucanusethemakebuildandreleaseinstructionstobuildtheseimages.
ChangingthebuildcontextandDockerfilepathandnameUsetheDOCKER_BUILD_CONTEXTvariabletosetthepathtothecontext.SetDOCKER_FILE_PATHtochangethepathtotheDockerfile(filenameincluded)youwanttouse.Usingthisinconjuctionwiththepre-buildandpost-buildtargetsanda.dockerignorefileallowsyoutomodifythebuildcontext.
DOCKER_BUILD_CONTEXT=../..DOCKER_FILE_PATH=$(DOCKER_BUILD_CONTEXT)/docker/$(NAME)/some.Dockerfilepre-build:check-env-vars.dockerignorecp.dockerignore$(DOCKER_BUILD_CONTEXT)post-build:rm$(DOCKER_BUILD_CONTEXT)/.dockerignorepretagcommandIfyouwantaddthecurrentreleasetoasourcefile,youcanaddthepropertypre\_tag\_commandtothe.releasefile.thiscommandisexecutedwhenthe.releasefileisupdatedandbeforethetagisplaced.Inthecommand@@RELEASE@@isreplacedwiththecurrentreleasebeforeitisexecuted.Forexample:
release=0.1.0tag=v0.1.0pre_tag_command=sed-i""-e's/^version=.*/version="@@RELEASE@@"/'setup.pyAdding--build-argIfyouwanttoaddanycommandlineoptionstothedockerbuildcommand,specifytheMakevariableDOCKER_BUILD_ARGS.
ThefollowingMakefile,specifiesthebuildargumentTAG_VERSIONtobesettothecurrentvalue.
include../MakefileDOCKER_BUILD_ARGS=--build-argTAG_VERSION=$(VERSION)checkouttheexampleMake-andDockerfile.
评论