从官方镜像启动:promprometheus 拉取镜像dockerpullpromprometheus 启动容器 方式1:dockerruntdp9090:9090nameprometheus1promprometheus 方式2:路径挂载dockerrunp9090:9090vpathtoprometheus。yml:etcprometheusprometheus。ymlpromprometheusdockerrunp9090:9090vpathtoconfig:etcprometheuspromprometheus官方Dockerfile分析 官方Dockerfile文件如下ARGARCHamd64ARGOSlinuxFROMquay。ioprometheusbusybox{OS}{ARCH}:latestLABELmaintainerThePrometheusAuthorsprometheusdevelopersgooglegroups。comARGARCHamd64ARGOSlinuxCOPY。build{OS}{ARCH}prometheusbinprometheusCOPY。build{OS}{ARCH}promtoolbinpromtoolCOPYdocumentationexamplesprometheus。ymletcprometheusprometheus。ymlCOPYconsolelibrariesusrshareprometheusconsolelibrariesCOPYconsolesusrshareprometheusconsolesCOPYLICENSELICENSECOPYNOTICENOTICECOPYnpmlicenses。tar。bz2npmlicenses。tar。bz2WORKDIRprometheusRUNlnsusrshareprometheusconsolelibrariesusrshareprometheusconsolesetcprometheuschownRnobody:nobodyetcprometheusprometheusUSERnobodyEXPOSE9090VOLUME〔prometheus〕ENTRYPOINT〔binprometheus〕CMD〔config。fileetcprometheusprometheus。yml,storage。tsdb。pathprometheus,web。console。librariesusrshareprometheusconsolelibraries,web。console。templatesusrshareprometheusconsoles〕 从官网Dockerfile文件可知:使用busybox作为基础镜像拷贝相关文件(prometheus、promtool、prometheus。yml等)到指定目录(bin、etc)指定工作目录prometheus容器内端口9090指定默认匿名卷为prometheus,Prometheus运行产生的数据将写到宿主机相关目录ENTRYPOINT容器启动入口点为binprometheusCMD指定容器启动参数为:config。fileetcprometheusprometheus。yml,storage。tsdb。pathprometheus,web。console。librariesusrshareprometheusconsolelibraries,web。console。templatesusrshareprometheusconsoles 容器内Prometheus启动命令为:binprometheusconfig。fileetcprometheusprometheus。ymlstorage。tsdb。pathprometheusweb。console。librariesusrshareprometheusconsolelibrariesweb。console。templatesusrshareprometheusconsoles 进入容器查看Prometheus的进程:〔rootdocker〕dockerexecitprometheus1shprometheusprometheuspsefgrepprometheu〔s〕1nobody0:53binprometheusconfig。fileetcprometheusprometheus。ymlstorage。tsdb。pathprometheusweb。console。librariesusrshareprometheusconsolelibrariesweb。console。templatesusrshareprometheusconsoles编写自己的Dockerfile 这里编写自己的Dockerfile进行构建,开启Prometheus进程的配置热加载和数据库管理功能。FROMamd64busybox:1。35LABELmaintainerThePrometheusAuthorswuenwuenARGARCHamd64ARGOSlinuxCOPYprometheus。{OS}{ARCH}prometheusprometheusbinCOPYprometheus。{OS}{ARCH}promtoolprometheusbinCOPYprometheus。{OS}{ARCH}prometheus。ymlprometheusetcprometheus。ymlCOPYprometheus。{OS}{ARCH}consolelibrariesprometheusconsolelibrariesCOPYprometheus。{OS}{ARCH}consolesprometheusconsolesCOPYprometheus。{OS}{ARCH}LICENSEprometheusLICENSECOPYprometheus。{OS}{ARCH}NOTICEprometheusNOTICEWORKDIRprometheusRUNlnsprometheusbinprometheusprometheusbinpromtoolbinchownRroot:rootprometheusUSERrootEXPOSE9090VOLUME〔prometheusdata,prometheusetc〕ENTRYPOINT〔binprometheus〕CMD〔config。fileprometheusetcprometheus。yml,storage。tsdb。pathprometheusdata,web。console。librariesprometheusconsolelibraries,web。console。templatesprometheusconsoles,web。enablelifecycle,web。enableadminapi〕 解读:使用amd64busybox:1。35作为基础镜像,将Prometheus的二进制文件放入bin目录,将配置文件统一放入etc目录;将配置文件目录和数据存储目录定义为匿名卷;启动方式中,新增两项配置,开启配置文件的热加载和数据库管理功能。 在同目录下添加一个。dockerignore文件,来屏蔽一些无关构建的文件:prometheus。linuxamd64。tar。gzetc构建镜像: 新建目录,将版本包和Dockerfile文件放入该目录cdrootprometheuslsDockerfileprometheus2。33。1。linuxamd64。tar。gz 解压版本包,同时新建。dockerignore文件,在该文件下添加与构建无关的文件或目录(这表示构建时,不将这些文件复制到构建上下文环境中)tarzxfprometheus2。33。1。linuxamd64。tar。gzcat。dockerignoreprometheus。linuxamd64。tar。gz 执行构建命令dockerbuilddockerbuildtprometheus:v1。SendingbuildcontexttoDockerdaemon200。8MBStep118:FROMamd64busybox:1。3596b2896db672Step218:LABELmaintainerThePrometheusAuthorswuenwuenRunningin04e215d3c3e0Removingintermediatecontainer04e215d3c3e088050ed9e09dStep318:ARGARCHamd64Runningin5ce5d1b12e0fRemovingintermediatecontainer5ce5d1b12e0f370832a62c71Step418:ARGOSlinuxRunningind7ea847b2d90Removingintermediatecontainerd7ea847b2d90cf9781cb7722Step518:COPYprometheus。{OS}{ARCH}prometheusprometheusbin42b4b03f8332Step618:COPYprometheus。{OS}{ARCH}promtoolprometheusbin897066bd0ca3Step718:COPYprometheus。{OS}{ARCH}prometheus。ymlprometheusetcprometheus。ymlb141fa2c22a6Step818:COPYprometheus。{OS}{ARCH}consolelibrariesprometheusconsolelibraries228b62d5e860Step918:COPYprometheus。{OS}{ARCH}consolesprometheusconsoles5c9125f39322Step1018:COPYprometheus。{OS}{ARCH}LICENSEprometheusLICENSEc89f57bbceb6Step1118:COPYprometheus。{OS}{ARCH}NOTICEprometheusNOTICE7e459dfd86feStep1218:WORKDIRprometheusRunningin8f09cea3e03eRemovingintermediatecontainer8f09cea3e03eee1f386d159bStep1318:RUNlnsprometheusbinprometheusprometheusbinpromtoolbinchownRroot:rootprometheusRunningin093e9b6b8d14Removingintermediatecontainer093e9b6b8d14121df7a93221Step1418:USERrootRunninginb4ffed0d491aRemovingintermediatecontainerb4ffed0d491a75e2aada4653Step1518:EXPOSE9090Runninginb09e22947d56Removingintermediatecontainerb09e22947d56c700606bd44dStep1618:VOLUME〔prometheusdata,prometheusetc〕Runningine18dd4f3af2aRemovingintermediatecontainere18dd4f3af2a824d398febddStep1718:ENTRYPOINT〔binprometheus〕Runninginff8fb122f2fbRemovingintermediatecontainerff8fb122f2fb414d2523bb9bStep1818:CMD〔config。fileprometheusetcprometheus。yml,storage。tsdb。pathprometheusdata,web。console。librariesprometheusconsolelibraries,web。console。templatesprometheusconsoles,web。enablelifecycle,web。enableadminapi〕Runningin56a8542d31a9Removingintermediatecontainer56a8542d31a917f5fa014281Successfullybuilt17f5fa014281Successfullytaggedprometheus:v1 查看构建的镜像:dockerimagesREPOSITORYTAGIMAGEIDCREATEDSIZEprometheusv117f5fa0142816minutesago403MBamd64busybox1。3596b2896db6722weeksago1。24MBpromprometheuslatesta3d385fc29f92monthsago201MB可以看到自己构建的镜像比官方镜像大了一倍启动容器: 直接启动:dockerruntdprometheus:v1dockerruntdp9090:9090prometheus:v1 直接启动后,配置文件和数据存储目录将默认使用匿名挂载。 挂载路径使用dockerinspect命令查看 路径挂载(推荐): 选择挂载出配置文件目录,数据存储目录使用默认的匿名挂载就行了。 由于路径挂载时,容器内挂载路径下的文件会被隐藏,同时Prometheus的启动又需要指定配置文件,所以启动容器前,需要提前将配置文件放在挂载点路径下,以避免容器启动后Prometheus进程无法启动,导致容器退出。 新建挂载点,并存放配置文件:treerootprometheusetcrootprometheusetcfirstrules。ymlprometheus。ymlstaticconfignodeexporter。yml 然后就可以使用构建的镜像来启动容器了,命令如下选择其中一个即可dockertdvrootprometheusetc:prometheusetcprometheus:v1dockerruntdp9090:9090nameprometheus1vrootprometheusetc:prometheusetcprometheus:v1dockerruntdp9090:9090nameprometheus1vrootprometheusetc:prometheusetcprometheus:v1 查看容器:dockerpsCONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMESb2ebd62251acprometheus:v1binprometheuscAboutaminuteagoUpAboutaminute0。0。0。0:90909090tcp,:::90909090tcpprometheus1dockerpsformattable{{。Image}}{{。ID}}{{。Ports}}{{。Status}}{{。Names}}IMAGECONTAINERIDPORTSSTATUSNAMESprometheus:v1b2ebd62251ac0。0。0。0:90909090tcp,:::90909090tcpUp6minutesprometheus1 访问Prometheus的UI界面:http:192。168。175。130:9090 原文地址https:www。cnblogs。comMrRebootp15937802。html