从官方镜像启动:prom/prometheus

拉取镜像

$ docker pull prom/prometheus

启动容器

方式1:

$ docker run -td -p 9090:9090 --name prometheus1 prom/prometheus

方式2:路径挂载

$ docker run \
-p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus $ docker run \
-p 9090:9090 \
-v /path/to/config:/etc/prometheus \
prom/prometheus

官方Dockerfile分析

官方Dockerfile文件如下

ARG ARCH="amd64"
ARG OS="linux"
FROM quay.io/prometheus/busybox-${OS}-${ARCH}:latest
LABEL maintainer="The Prometheus Authors <prometheus-developers@googlegroups.com>" ARG ARCH="amd64"
ARG OS="linux"
COPY .build/${OS}-${ARCH}/prometheus /bin/prometheus
COPY .build/${OS}-${ARCH}/promtool /bin/promtool
COPY documentation/examples/prometheus.yml /etc/prometheus/prometheus.yml
COPY console_libraries/ /usr/share/prometheus/console_libraries/
COPY consoles/ /usr/share/prometheus/consoles/
COPY LICENSE /LICENSE
COPY NOTICE /NOTICE
COPY npm_licenses.tar.bz2 /npm_licenses.tar.bz2 WORKDIR /prometheus
RUN ln -s /usr/share/prometheus/console_libraries /usr/share/prometheus/consoles/ /etc/prometheus/ && \
chown -R nobody:nobody /etc/prometheus /prometheus USER nobody
EXPOSE 9090
VOLUME [ "/prometheus" ]
ENTRYPOINT [ "/bin/prometheus" ]
CMD [ "--config.file=/etc/prometheus/prometheus.yml", \
"--storage.tsdb.path=/prometheus", \
"--web.console.libraries=/usr/share/prometheus/console_libraries", \
"--web.console.templates=/usr/share/prometheus/consoles" ]

从官网Dockerfile文件可知:

  1. 使用busybox作为基础镜像
  2. 拷贝相关文件(prometheus、promtool、prometheus.yml等)到指定目录(/bin、/etc)
  3. 指定工作目录/prometheus
  4. 容器内端口9090
  5. 指定默认匿名卷为"/prometheus",Prometheus运行产生的数据将写到宿主机相关目录
  6. ENTRYPOINT容器启动入口点为"/bin/prometheus"
  7. CMD指定容器启动参数为:"--config.file=/etc/prometheus/prometheus.yml", "--storage.tsdb.path=/prometheus","--web.console.libraries=/usr/share/prometheus/console_libraries", "--web.console.templates=/usr/share/prometheus/consoles"

容器内Prometheus启动命令为:

/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --web.console.libraries=/usr/share/prometheus/console_libraries --web.console.templates=/usr/share/prometheus/consoles

进入容器查看Prometheus的进程:

[root@docker ~]# docker exec -it prometheus1 sh
/prometheus $
/prometheus $ ps -ef | grep prometheu[s]
1 nobody 0:53 /bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --web.console.libraries=/usr/share/prometheus/console_libraries --web.console.templates=/usr/share/prometheus/consoles

编写自己的Dockerfile

这里编写自己的Dockerfile进行构建,开启Prometheus进程的配置热加载和数据库管理功能。

FROM amd64/busybox:1.35
LABEL maintainer="The Prometheus Authors <wuenwuen>" ARG ARCH="amd64"
ARG OS="linux"
COPY prometheus-*.${OS}-${ARCH}/prometheus /prometheus/bin/
COPY prometheus-*.${OS}-${ARCH}/promtool /prometheus/bin/
COPY prometheus-*.${OS}-${ARCH}/prometheus.yml /prometheus/etc/prometheus.yml
COPY prometheus-*.${OS}-${ARCH}/console_libraries/* /prometheus/console_libraries/
COPY prometheus-*.${OS}-${ARCH}/consoles/* /prometheus/consoles/
COPY prometheus-*.${OS}-${ARCH}/LICENSE /prometheus/LICENSE
COPY prometheus-*.${OS}-${ARCH}/NOTICE /prometheus/NOTICE WORKDIR /prometheus
RUN ln -s /prometheus/bin/prometheus /prometheus/bin/promtool /bin/ && \
chown -R root:root /prometheus USER root
EXPOSE 9090
VOLUME [ "/prometheus/data", "/prometheus/etc" ]
ENTRYPOINT [ "/bin/prometheus" ]
CMD [ "--config.file=/prometheus/etc/prometheus.yml", \
"--storage.tsdb.path=/prometheus/data", \
"--web.console.libraries=/prometheus/console_libraries", \
"--web.console.templates=/prometheus/consoles", \
"--web.enable-lifecycle", \
"--web.enable-admin-api" ]

解读:

  1. 使用amd64/busybox:1.35作为基础镜像,将Prometheus的二进制文件放入bin目录,将配置文件统一放入etc目录;
  2. 将配置文件目录和数据存储目录定义为匿名卷;
  3. 启动方式中,新增两项配置,开启配置文件的热加载和数据库管理功能。

在同目录下添加一个.dockerignore文件,来屏蔽一些无关构建的文件:

prometheus-*.linux-amd64.tar.gz
etc

构建镜像:

新建目录,将版本包和Dockerfile文件放入该目录

# mkdir /root/prometheus;cd /root/prometheus
# ls
Dockerfile prometheus-2.33.1.linux-amd64.tar.gz

解压版本包,同时新建.dockerignore文件,在该文件下添加与构建无关的文件或目录(这表示构建时,不将这些文件复制到构建上下文环境中)

# tar -zxf prometheus-2.33.1.linux-amd64.tar.gz
# cat .dockerignore
prometheus-*.linux-amd64.tar.gz

执行构建命令docker build

$ docker build -t prometheus:v1 .
Sending build context to Docker daemon 200.8MB
Step 1/18 : FROM amd64/busybox:1.35
---> 96b2896db672
Step 2/18 : LABEL maintainer="The Prometheus Authors <wuenwuen>"
---> Running in 04e215d3c3e0
Removing intermediate container 04e215d3c3e0
---> 88050ed9e09d
Step 3/18 : ARG ARCH="amd64"
---> Running in 5ce5d1b12e0f
Removing intermediate container 5ce5d1b12e0f
---> 370832a62c71
Step 4/18 : ARG OS="linux"
---> Running in d7ea847b2d90
Removing intermediate container d7ea847b2d90
---> cf9781cb7722
Step 5/18 : COPY prometheus-*.${OS}-${ARCH}/prometheus /prometheus/bin/
---> 42b4b03f8332
Step 6/18 : COPY prometheus-*.${OS}-${ARCH}/promtool /prometheus/bin/
---> 897066bd0ca3
Step 7/18 : COPY prometheus-*.${OS}-${ARCH}/prometheus.yml /prometheus/etc/prometheus.yml
---> b141fa2c22a6
Step 8/18 : COPY prometheus-*.${OS}-${ARCH}/console_libraries/* /prometheus/console_libraries/
---> 228b62d5e860
Step 9/18 : COPY prometheus-*.${OS}-${ARCH}/consoles/* /prometheus/consoles/
---> 5c9125f39322
Step 10/18 : COPY prometheus-*.${OS}-${ARCH}/LICENSE /prometheus/LICENSE
---> c89f57bbceb6
Step 11/18 : COPY prometheus-*.${OS}-${ARCH}/NOTICE /prometheus/NOTICE
---> 7e459dfd86fe
Step 12/18 : WORKDIR /prometheus
---> Running in 8f09cea3e03e
Removing intermediate container 8f09cea3e03e
---> ee1f386d159b
Step 13/18 : RUN ln -s /prometheus/bin/prometheus /prometheus/bin/promtool /bin/ && chown -R root:root /prometheus
---> Running in 093e9b6b8d14
Removing intermediate container 093e9b6b8d14
---> 121df7a93221
Step 14/18 : USER root
---> Running in b4ffed0d491a
Removing intermediate container b4ffed0d491a
---> 75e2aada4653
Step 15/18 : EXPOSE 9090
---> Running in b09e22947d56
Removing intermediate container b09e22947d56
---> c700606bd44d
Step 16/18 : VOLUME [ "/prometheus/data", "/prometheus/etc" ]
---> Running in e18dd4f3af2a
Removing intermediate container e18dd4f3af2a
---> 824d398febdd
Step 17/18 : ENTRYPOINT [ "/bin/prometheus" ]
---> Running in ff8fb122f2fb
Removing intermediate container ff8fb122f2fb
---> 414d2523bb9b
Step 18/18 : CMD [ "--config.file=/prometheus/etc/prometheus.yml", "--storage.tsdb.path=/prometheus/data", "--web.console.libraries=/prometheus/console_libraries", "--web.console.templates=/prometheus/consoles", "--web.enable-lifecycle", "--web.enable-admin-api" ]
---> Running in 56a8542d31a9
Removing intermediate container 56a8542d31a9
---> 17f5fa014281
Successfully built 17f5fa014281
Successfully tagged prometheus:v1

查看构建的镜像:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
prometheus v1 17f5fa014281 6 minutes ago 403MB
amd64/busybox 1.35 96b2896db672 2 weeks ago 1.24MB
prom/prometheus latest a3d385fc29f9 2 months ago 201MB
# 可以看到自己构建的镜像比官方镜像大了一倍

启动容器:

直接启动:

$ docker run -td prometheus:v1

$ docker run -td -p 9090:9090 prometheus:v1

直接启动后,配置文件和数据存储目录将默认使用匿名挂载。

挂载路径使用docker inspect命令查看

路径挂载(推荐):

选择挂载出配置文件目录,数据存储目录使用默认的匿名挂载就行了。

由于路径挂载时,容器内挂载路径下的文件会被隐藏,同时Prometheus的启动又需要指定配置文件,所以启动容器前,需要提前将配置文件放在挂载点路径下,以避免容器启动后Prometheus进程无法启动,导致容器退出。

新建挂载点,并存放配置文件:

$ tree /root/prometheus/etc
/root/prometheus/etc
├── first_rules.yml
├── prometheus.yml
└── static_config
└── node_exporter.yml

然后就可以使用构建的镜像来启动容器了,命令如下

# 选择其中一个即可
$ docker -td -v /root/prometheus/etc:/prometheus/etc prometheus:v1 $ docker run -td -p 9090:9090 --name prometheus-1 -v /root/prometheus/etc:/prometheus/etc prometheus:v1 $ docker run \
-td -p 9090:9090 --name prometheus-1 \
-v /root/prometheus/etc:/prometheus/etc \
prometheus:v1

查看容器:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b2ebd62251ac prometheus:v1 "/bin/prometheus --c…" About a minute ago Up About a minute 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus-1 $ docker ps --format "table {{.Image}}\t{{.ID}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
IMAGE CONTAINER ID PORTS STATUS NAMES
prometheus:v1 b2ebd62251ac 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp Up 6 minutes prometheus-1

访问Prometheus的UI界面http://192.168.175.130:9090/

Prometheus之Dockerfile编写、镜像构建、容器启动的更多相关文章

  1. 【云计算】Dockerfile、镜像、容器快速入门

    Dockerfile.镜像.容器快速入门 1.1.Dockerfile书写示例 Dockerfile可以用来生成Docker镜像,它明确的定义了Image的生成过程.虽然直接修改容器也可以提交生成镜像 ...

  2. asp.net core webapi 使用ef 对mysql进行增删改查,并生成Docker镜像构建容器运行

    1.构建运行mysql容器,添加数据库user 参考Docker创建运行多个mysql容器,地址 http://www.cnblogs.com/heyangyi/p/9288402.html 添加us ...

  3. dockerfile创建镜像及容器

    第一步: 从王总git上:http://git.oursdata.com/wangyue/dockerfiles.git 进入下图的文件夹中 然后执行以下的说明执行步骤   第二步: 开发环境dock ...

  4. Docker镜像构建

    一.简介 在构建容器化应用时,相当重要的步骤莫过于镜像制作,本文将介绍镜像制作方法以及镜像制作的建议.通常镜像的制作有两种方式: 使用现有的容器使用docker commit 生成镜像 使用Docke ...

  5. 通过dockerfile制作镜像

    Dockerfile是一个用于构建Docker镜像的文本文件,其中包含了创建Docker镜像的全部指令.就是将我们安装环境的每个步骤使用指令的形式存放在一个文件中,最后生成一个需要的环境. Docke ...

  6. Watchtower - 自动更新 Docker 镜像与容器

    git 地址:https://github.com/containrrr/watchtower Docker images docker pull containrrr/watchtower:i386 ...

  7. 7.云原生之Docker容器Dockerfile镜像构建浅析与实践

    转载自:https://www.bilibili.com/read/cv15220707/?from=readlist Dockerfile 镜像构建浅析与实践 描述:Dockerfile是一个文本格 ...

  8. [Linux] 编写Dockerfile文件自动构建镜像

    Dockerfile是一个文本文件,按顺序包含构建给定镜像所需的所有命令Docker通过读取Dockerfile中的指令自动构建图像 . Dockerfile遵循特定的格式和指令集,您可以在Docke ...

  9. docker 应用-2(Dockerfile 编写以及镜像保存提交)

    我们可以从docker hub上pull别人的镜像,也可以将容器进行修改,然后commit镜像,并把镜像push到docker hub上被被人使用.但是,直接pull或者push镜像的方式太过笨重,尤 ...

随机推荐

  1. 深度介绍Flink在字节跳动数据流的实践

    本文是字节跳动数据平台开发套件团队在1月9日Flink Forward Asia 2021: Flink Forward 峰会上的演讲分享,将着重分享Flink在字节跳动数据流的实践. 字节跳动数据流 ...

  2. Mysql存储过程二

    1.MySQL中创建存储过程时通过DEFINER和SQL SECURITY设置访问权限 procedure与function.trigger等创建时紧接着CREATE都有个definer可选项,该de ...

  3. 《剑指offer》面试题32 - III. 从上到下打印二叉树 III

    问题描述 请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推. 例如: 给定二叉树: [3,9,20, ...

  4. 通过HTML+CSS+Javascript实现向下滚动滚动条出现导航栏并出现回到顶部按钮点击按钮回到顶部(一)

    回到顶部实例一 效果:默认隐藏导航栏,当滚动条滚到超过300px后导航栏和按钮出现,点击回到顶部按钮回到顶部,并隐藏导航栏和按钮(导航栏和按钮都是固定定位) <!doctype html> ...

  5. linux下编译支持opencl的opencv for android

    主要的步骤其他人已经写过,请参考这篇:https://www.cnblogs.com/hrlnw/p/4720977.html 操作的细节请参考附件的pdf:  https://files.cnblo ...

  6. mac 更新到big sur 后,parallels虚拟机的一些问题:由于您尚未获得访问其中一些文件的授权,所以您不能恢复“Windows 10

    由于您尚未获得访问其中一些文件的授权,所以您不能恢复"Windows 10 Mac上使用PD虚拟机,打开系统时提示"由于您尚未获得访问其中一些文件的授权,所以您不能恢复" ...

  7. ORA-15081: failed to submit an I/O operation to a disk

    Problem: While restoring controlfile to test environment, from filesystem or tape environment after ...

  8. Android开发-数据库代码编写

    数据库代码主要是查找 package com.example.Utils.database; import android.annotation.SuppressLint; import androi ...

  9. docker镜像制作Dockerfile

    使用 Dockerfile 定制镜像 从刚才的 docker commit 的学习中,我们可以了解到,镜像的定制实际上就是 定制每一层所添加的配置.文件.如果我们可以把每一层修改.安装.构建.操作 的 ...

  10. gcc 11.2 在线升级

    环境:centos 7 1.准备开发环境 $ yum groupinstall "Development Tools" $ yum install glibc-static lib ...