docker-makefile Makefile for building docker repos开源项目

我要开发同款
匿名用户2021年11月09日
37阅读
开发技术SHELL
所属分类终端/远程登录、应用工具
授权协议Apache-2.0 License

作品详情

GenericDockerMakefile

WhenworkingwiththeDockerhub,twosmallthingsbotheredme:

WaitingforyourbuildtostartNoeasycontroloverthetagsfortheimages.

Toresolvethesetoissues,IcreatedagenericMakefilethatallowsyoutobuildandreleasedockerimagesbasedupongittags,wheneveryouwant.

Makefiletargets

TheMakefilehasthefollowingtargets:

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,userorimagename

Bydefault,theregistryissettodocker.ioandtheusertothecurrentuser.Tooverridethis,edittheMakefileandsetthevariablesREGISTRY_HOST,USERNAMEandNAME.

includeMakefile.mkREGISTRY_HOST=myregistry.ioUSERNAME=mvanholsteijnNAME=awesome-imageBuildinganimage

tobuildanimage,addaDockerfiletoyourdirectoryandtypemake:

makeRelease

Tomakeareleaseandtagit,commitaddthechangesandtype:

makepatch-release

Thiswillbumpthepatch-releasenumber,buildtheimageandpushittotheregistry.Itwillonlyreleaseiftherearenooutstandingchangesandthecontentofthedirectoryequalsthetaggedcontent.

Alternativelyyoucanchoose'makeminor-release'or'makemajor-release'tobumptheassociatednumber.

Releasenumber

Thereleaseofyourdockerimageiskeptinthefile.releaseandusesthefollowingformat:

release=<major>.<minor>.<patch>

Thenameofthegittagiskeptinthesamefile,andbydefaultwillhavetheformat:

tag=<directory-name>.<minor>.<patch>

ThiswillallowyoutohavetrackandtagmultipleimagesinasingleGitrepository.

Ifyouwanttouseadifferenttagprefix,changeitinthe.release.

Imagenameandtag

Thenameoftheimagewillbecreatedasfollows:

<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│  ├──Makefile

TheMakefileintheimagedirectorieswillincludethegenericMakefile.InthisMakefileyoucanalterthenamesandtailorthebuildbyaddingpreandpostbuildtargets.Checkoutthedirectory(multiple-example)foranexample.

Createthegenericmakedirectory

Tocreatethegenericmakedirectory,type:

mkdirmakecdmakewgethttps://raw.githubusercontent.com/mvanholsteijn/docker-makefile/master/Makefilewgethttps://raw.githubusercontent.com/mvanholsteijn/docker-makefile/master/.make-release-supportCreatedockerimagedirectory

Foreachdockerimages,youcreateasiblingdirectory:

mkdir../image1cd../image1cat>Makefile<<!include../make/MakefileUSERNAME=mvanholsteijnpre-build:@echodosomestuffbeforethedockerbuildpost-build:@echodosomestuffafterthedockerbuild!

Nowyoucanusethemakebuildandreleaseinstructionstobuildtheseimages.

ChangingthebuildcontextandDockerfilepathandname

UsetheDOCKER_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)/.dockerignorepretagcommand

Ifyouwantaddthecurrentreleasetoasourcefile,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-arg

Ifyouwanttoaddanycommandlineoptionstothedockerbuildcommand,specifytheMakevariableDOCKER_BUILD_ARGS.

ThefollowingMakefile,specifiesthebuildargumentTAG_VERSIONtobesettothecurrentvalue.

include../MakefileDOCKER_BUILD_ARGS=--build-argTAG_VERSION=$(VERSION)

checkouttheexampleMake-andDockerfile.

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论