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