使用DockerFile制作prometheus镜像

DockerFile制作prometheus镜像

步骤一:在prometheus官网下载二进制文件

官方链接:prometheus.io/download/
以下制作的是prometheus-2.45.5.linux-amd64版本,可以按照各自需求去选择版本。

步骤二:安装Docker

以下是使用的Docker version 24.0.5版本,宿主机环境是Ubuntu 22.04.4。
1、安装Docker

apt -y update && apt install -y docker.io

2、检查Docker是否安装成功

docker -v

成功打印:

Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1

步骤三:编写DockerFile文件

1、创建prometheus-docker目录

mkdir prometheus-docker
cd prometheus-docker

2、使用vim编写DockerFile文件

vim custom-image-prometheus

以下是DockerFile编写内容

# 使用Ubuntu Server作为基础镜像
FROM ubuntu:22.04

# 更新软件包列表并安装必要的依赖项
RUN apt-get update && apt-get install -y net-tools && apt-get install -y  --no-install-recommends \
      # 根据需要安装Prometheus运行所需的依赖项,例如libstdc++等
      # 注意:Prometheus通常是静态链接的,可能不需要额外的库
             && rm -rf /var/lib/apt/lists/*

# 定义版本号
LABEL version="2.45.5"

# 创建需要的目录
RUN mkdir -p /etc/prometheus /prometheus-data /usr/share/prometheus/consoles/libraries /usr/share/prometheus/consoles /bin

# 将Prometheus的二进制文件复制到镜像的/bin目录下
COPY prometheus-2.45.5.linux-amd64/prometheus /bin/prometheus
COPY prometheus-2.45.5.linux-amd64/promtool /bin/promtool

# 将控制台库复制到镜像的/usr/share/prometheus/consoles/libraries目录下
COPY prometheus-2.45.5.linux-amd64/console_libraries /usr/share/prometheus/consoles/libraries

# 将控制台模板复制到镜像的/usr/share/prometheus/consoles目录下
COPY prometheus-2.45.5.linux-amd64/consoles /usr/share/prometheus/consoles

# 将Prometheus的配置文件复制到镜像的/etc/prometheus目录下
COPY prometheus-2.45.5.linux-amd64/prometheus.yml /etc/prometheus/prometheus.yml

# 暴露Prometheus的默认端口
EXPOSE default port

# 设置容器的工作目录
WORKDIR /prometheus-data

# 设置容器启动时运行的命令
CMD ["/bin/prometheus", \
    "--config.file=/etc/prometheus/prometheus.yml", \
    "--storage.tsdb.path=/prometheus-data", \
    "--web.console.libraries=/usr/share/prometheus/consoles/libraries", \
    "--web.console.templates=/usr/share/prometheus/consoles"]

步骤四:构建Docker镜像

注意:构建前需要检查网络环境,是否能正常拉取Ubuntu22.04镜像,如果无法通过pull拉取可以配置镜像加速地址来解决,或者导入一个现有的Ubuntu22.04镜像。
这里使用的是https://yxzrazem.mirror.aliyuncs.com/镜像加速

docker build -t Prometheus-docker:2.45.5 .

注意:-t 参数定义的tag名称只能是小写字母
成功打印:

DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

Sending build context to Docker daemon  235.3MB
Step 1/9 : FROM scratch
 --->
Step 2/9 : LABEL version="2.45.5"
 ---> Running in 1fc5bfbbed68
Removing intermediate container 1fc5bfbbed68
 ---> 6095581366fb
Step 3/9 : COPY prometheus-2.45.5.linux-amd64/prometheus /bin/prometheus
 ---> 6918aab149ea
Step 4/9 : COPY prometheus-2.45.5.linux-amd64/promtool /bin/promtool
 ---> 8042eaf8a389
Step 5/9 : COPY prometheus-2.45.5.linux-amd64/console_libraries /usr/share/prometheus/consoles/libraries
 ---> 470d6687ca62
Step 6/9 : COPY prometheus-2.45.5.linux-amd64/consoles /usr/share/prometheus/consoles
 ---> d7eeb8303ba4
Step 7/9 : COPY prometheus-2.45.5.linux-amd64/prometheus.yml /etc/prometheus/prometheus.yml
 ---> 2d6ac1bbe018
Step 8/9 : WORKDIR /
 ---> Running in b0c5a45dc5b2
Removing intermediate container b0c5a45dc5b2
 ---> a8b1edbb584e
Step 9/9 : CMD ["/bin/prometheus",     "--config.file=/etc/prometheus/prometheus.yml",     "--storage.tsdb.path=/prometheus",     "--web.console.libraries=/usr/share/prometheus/consoles/libraries",     "--web.console.templates=/usr/share/prometheus/consoles"]
 ---> Running in 6bbaf80e0dbf
Removing intermediate container 6bbaf80e0dbf
 ---> 66945ce218a2
Successfully built 66945ce218a2
Successfully tagged prometheus-docker:2.45.5

问题一:如果在构建时出现一下错误,使用的是自定义DockerFile文件名称,所以需要指定文件目录。
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/prometheus-docker/Dockerfile: no such file or directory
解决方法:使用 -f 参数来指定DockerFile文件名

docker build -t prometheus-docker:2.45.5 -f /home/prometheus-docker/custom-image-prometheus .

验证DockerFile是否成功构建镜像

docker images

成功打印:

REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
prometheus-docker   2.45.5    66945ce218a2   30 seconds ago   235MB

步骤五:运行容器并挂载目录

docker run -d --name prometheus -p 9090:9090 -v /path/to/prometheus/data:/prometheus-data -v /path/to/prometheus/:/etc/prometheus/ prometheus-docker:2.45.5 /bin/prometheus

成功打印:

2bb70d0011d0b00ec780005c9196cccb79a2700094e82fa43aa0b05ba559a9ee

步骤六:启动容器

启动前先检查一下配置文件是否存在

docker start prometheus

成功打印:

prometheus

检查容器是否成功启动

docker ps -a

成功打印:

CREATED     			STATUS				
7 minutes ago		Up 5 minutes	

参考文档:https://chat.baidu.com/

使用Dockerfile部署Prometheus是一种常见的做法,它允许你创建一个标准化的容器镜像,包含所有所需的服务、配置和依赖项。以下是一个简单的Dockerfile示例,用于部署Prometheus: ```Dockerfile # 使用官方的Prometheus镜像作为基础 FROM prom/prometheus:v2.34.1 # 设置工作目录 WORKDIR /prometheus # 复制Prometheus配置文件 COPY prometheus.yml config/ # 设置环境变量 ENV.prometheus_config_path=config/prometheus.yml # 拷贝启动脚本(如果有的话),以便自定义启动参数 COPY start_prometheus.sh / # 如果有特定的日志路径,可以添加这里 # VOLUME /logs # 设置Prometheus监听的端口 EXPOSE 9090 # 运行Prometheus CMD ["/start_prometheus.sh"] 或 CMD ["./prometheus", "-config.file=$prometheus_config_path"] ``` 在这个Dockerfile中: - `FROM prom/prometheus:v2.34.1` 选择了Prometheus的最新稳定版本。 - `COPY` 命令将本地的`prometheus.yml` 和`start_prometheus.sh`(如果有)复制到镜像的相应位置。 - `ENV` 设置了一个环境变量,指定Prometheus配置文件的位置。 - `EXPOSE` 映射Prometheus监听的9090端口,让容器外也能访问。 - `CMD` 指定了如何启动Prometheus,可以根据需要执行定制的启动脚本或直接启动Prometheus。 要使用Dockerfile构建镜像,你需要在一个名为`Dockerfile`的文件中放置这段代码,然后在终端中导航到该文件所在的目录,运行 `docker build -t your_image_name .`。接下来,你可以使用 `docker run -p <host_port>:9090 your_image_name` 来运行一个新的Prometheus实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值