prometheusu を dockerコンテナで実行する
はじめに
サーバーマシンを監視する為、監視システムである prometheusu をDockerコンテナで実行します。
そして、監視対象のマシンから prometheusu で収集した監視メトリクスを grafana を用いて可視化したいと思います。
概要
【prometheusu とは】
オープンソースの監視システムおよび時系列データベースであり、インフラやアプリケーションのメトリクス(CPU使用率やメモリ使用量など)を収集・保存・可視化するために利用されます。
データ収集は Prometheus が定期的に対象(ターゲット)に HTTP リクエストを送ってメトリクスを取得する 「Scraping」方式 を採用しています。監視対象には以下のような方法でメトリクスを提供させます。
- Exporter:既存サービス(例:Linux、MySQL、Node.jsなど)の情報を変換して出力
- Client Library:アプリケーションに直接組み込んでメトリクスを出力
- Pushgateway:短時間で終了するバッチ処理のようなプロセス用に、メトリクスをプッシュするための中継サービス
【grafana とは】
データの可視化と分析を行うためのオープンソースのプラットフォームです。Prometheus などのメトリクスデータベースや、Elasticsearch、Loki、MySQL、PostgreSQL など多様なデータソースと連携し、リアルタイムでダッシュボード上に可視化することができます。
クエリベースでデータを取得し、インタラクティブなグラフやアラートなどを柔軟に構成できるのが特徴です。
実行環境と作業の流れ
監視サーバー : Dockerコンテナ(MacBookAir上で構築)
監視対象 : Fedoraサーバー(VMWare仮想マシン)
※ 以下の流れで作業していきます。
- prometheusu のコンテナで環境を構築する ← ※今回はここです
- grafana のコンテナ環境を構築する
- prometheus から取得したデータを grafana で可視化する
prometheus の起動
Dockerコンテナを使用して prometheus を実行する為、コンテナ起動用の DockerCompose ファイルを作成します。今回はホームディレクトリ以下に prometheusu ディレクトリを作成し、そこに DockerCompose ファイルを用意します。
% cd ~
% mkdir prometheus
% cd prometheus
% vi docker-compose.yml
% cat docker-compose.yml
services:
prometheus:
image: prom/prometheus
# 公式の Prometheus イメージを使用
container_name: prometheus
# コンテナの名前
ports:
- '9090:9090'
# ホストとコンテナ間は9090番ポートで通信する
volumes:
- 'prometheus-data:/prometheus'
# ホスト側のボリューム prometheus-data を、コンテナ内の /prometheus にマウント
volumes:
prometheus-data: {}
# prometheus-data という名前のDockerボリュームを定義
prometheusコンテナを起動します。
% docker compose up
[+] Running 2/2
✔ Network prometheus_default Created 0.0s
✔ Container prometheus Created 0.1s
Attaching to prometheus
prometheus | time=2025-07-26T04:03:36.193Z level=INFO source=main.go:1544 msg="updated GOGC" old=100 new=75
prometheus | time=2025-07-26T04:03:36.193Z level=INFO source=main.go:676 msg="Leaving GOMAXPROCS=8: CPU quota undefined" component=automaxprocs
prometheus | time=2025-07-26T04:03:36.193Z level=INFO source=memlimit.go:198 msg="GOMEMLIMIT is updated" component=automemlimit package=github.com/KimMachineGun/automemlimit/memlimit GOMEMLIMIT=3698823168 previous=9223372036854775807
prometheus | time=2025-07-26T04:03:36.194Z level=INFO source=main.go:718 msg="No time or size retention was set so using the default time retention" duration=15d
prometheus | time=2025-07-26T04:03:36.194Z level=INFO source=main.go:769 msg="Starting Prometheus Server" mode=server version="(version=3.5.0, branch=HEAD, revision=8be3a9560fbdd18a94dedec4b747c35178177202)"
prometheus | time=2025-07-26T04:03:36.194Z level=INFO source=main.go:774 msg="operational information" build_context="(go=go1.24.5, platform=linux/arm64, user=root@4451b64cb451, date=20250714-16:18:17, tags=netgo,builtinassets)" host_details="(Linux 6.10.14-linuxkit #1 SMP Sat May 17 08:28:57 UTC 2025 aarch64 de0342f8d216 (none))" fd_limits="(soft=1048576, hard=1048576)" vm_limits="(soft=unlimited, hard=unlimited)"
prometheus | time=2025-07-26T04:03:36.199Z level=INFO source=web.go:656 msg="Start listening for connections" component=web address=0.0.0.0:9090
prometheus | time=2025-07-26T04:03:36.200Z level=INFO source=main.go:1288 msg="Starting TSDB ..."
prometheus | time=2025-07-26T04:03:36.201Z level=INFO source=tls_config.go:347 msg="Listening on" component=web address=[::]:9090
prometheus | time=2025-07-26T04:03:36.201Z level=INFO source=tls_config.go:350 msg="TLS is disabled." component=web http2=false address=[::]:9090
prometheus | time=2025-07-26T04:03:36.205Z level=INFO source=head.go:657 msg="Replaying on-disk memory mappable chunks if any" component=tsdb
prometheus | time=2025-07-26T04:03:36.205Z level=INFO source=head.go:744 msg="On-disk memory mappable chunks replay completed" component=tsdb duration=10.041µs
prometheus | time=2025-07-26T04:03:36.205Z level=INFO source=head.go:752 msg="Replaying WAL, this may take a while" component=tsdb
prometheus | time=2025-07-26T04:03:36.213Z level=INFO source=head.go:825 msg="WAL segment loaded" component=tsdb segment=0 maxSegment=1 duration=7.33825ms
prometheus | time=2025-07-26T04:03:36.213Z level=INFO source=head.go:825 msg="WAL segment loaded" component=tsdb segment=1 maxSegment=1 duration=71.334µs
prometheus | time=2025-07-26T04:03:36.213Z level=INFO source=head.go:862 msg="WAL replay completed" component=tsdb checkpoint_replay_duration=17.625µs wal_replay_duration=7.453ms wbl_replay_duration=167ns chunk_snapshot_load_duration=0s mmap_chunk_replay_duration=10.041µs total_replay_duration=7.492084ms
prometheus | time=2025-07-26T04:03:36.216Z level=INFO source=main.go:1309 msg="filesystem information" fs_type=EXT4_SUPER_MAGIC
prometheus | time=2025-07-26T04:03:36.216Z level=INFO source=main.go:1312 msg="TSDB started"
prometheus | time=2025-07-26T04:03:36.216Z level=INFO source=main.go:1497 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
prometheus | time=2025-07-26T04:03:36.218Z level=INFO source=main.go:1537 msg="Completed loading of configuration file" db_storage=3.083µs remote_storage=3.125µs web_handler=250ns query_engine=1µs scrape=1.885292ms scrape_sd=29.833µs notify=77.875µs notify_sd=7.167µs rules=4.625µs tracing=3µs filename=/etc/prometheus/prometheus.yml totalDuration=2.24925ms
prometheus | time=2025-07-26T04:03:36.218Z level=INFO source=main.go:1273 msg="Server is ready to receive web requests."
prometheus | time=2025-07-26T04:03:36.218Z level=INFO source=manager.go:176 msg="Starting rule manager..." component="rule manager"
コンテナが起動できているか確認する為、http://localhost:9090 にアクセスしてみます。

無事に起動できてました。
続いて、prometheusの設定ファイルを作成します。
% vi prometheus.yml
% cat prometheus.yml
global:
scrape_interval: 15s
# 全ての job の監視間隔のデフォルトを15秒ごとに設定
scrape_configs:
- job_name: 'prometheus'
# Web UI やメトリクスに表示される job の識別子
scrape_interval: 5s
# この job に対してだけ、監視間隔を5秒ごとに設定
static_configs:
- targets: ['localhost:9090']
# この job は localhost:9090 を監視
続いてこの設定ファイルをDockerコンテナにマウントします。
※ - './prometheus.yml:/etc/prometheus/prometheus.yml' を追記します。
% vi prometheus.yml
% cat prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
eiken@eiken prometheus % vi docker-compose.yml
eiken@eiken prometheus % cat docker-compose.yml
services:
prometheus:
image: prom/prometheus
container_name: prometheus
ports:
- '9090:9090'
volumes:
# 以下を追記
- './prometheus.yml:/etc/prometheus/prometheus.yml'
- 'prometheus-data:/prometheus'
volumes:
prometheus-data: {}
追記後、docker compose up でコンテナを再起動します。
% docker compose down
% docker compose up
[+] Running 2/2
✔ Network prometheus_default Created 0.0s
✔ Container prometheus Created 0.0s
Attaching to prometheus
prometheus | time=2025-07-26T04:38:20.972Z level=INFO source=main.go:1544 msg="updated GOGC" old=100 new=75
prometheus | time=2025-07-26T04:38:20.973Z level=INFO source=main.go:676 msg="Leaving GOMAXPROCS=8: CPU quota undefined" component=automaxprocs
prometheus | time=2025-07-26T04:38:20.973Z level=INFO source=memlimit.go:198 msg="GOMEMLIMIT is updated" component=automemlimit package=github.com/KimMachineGun/automemlimit/memlimit GOMEMLIMIT=3698823168 previous=9223372036854775807
prometheus | time=2025-07-26T04:38:20.973Z level=INFO source=main.go:718 msg="No time or size retention was set so using the default time retention" duration=15d
prometheus | time=2025-07-26T04:38:20.973Z level=INFO source=main.go:769 msg="Starting Prometheus Server" mode=server version="(version=3.5.0, branch=HEAD, revision=8be3a9560fbdd18a94dedec4b747c35178177202)"
prometheus | time=2025-07-26T04:38:20.973Z level=INFO source=main.go:774 msg="operational information" build_context="(go=go1.24.5, platform=linux/arm64, user=root@4451b64cb451, date=20250714-16:18:17, tags=netgo,builtinassets)" host_details="(Linux 6.10.14-linuxkit #1 SMP Sat May 17 08:28:57 UTC 2025 aarch64 6b742513f4f0 (none))" fd_limits="(soft=1048576, hard=1048576)" vm_limits="(soft=unlimited, hard=unlimited)"
prometheus | time=2025-07-26T04:38:20.974Z level=INFO source=web.go:656 msg="Start listening for connections" component=web address=0.0.0.0:9090
prometheus | time=2025-07-26T04:38:20.974Z level=INFO source=main.go:1288 msg="Starting TSDB ..."
prometheus | time=2025-07-26T04:38:20.977Z level=INFO source=tls_config.go:347 msg="Listening on" component=web address=[::]:9090
prometheus | time=2025-07-26T04:38:20.977Z level=INFO source=tls_config.go:350 msg="TLS is disabled." component=web http2=false address=[::]:9090
prometheus | time=2025-07-26T04:38:20.978Z level=INFO source=head.go:657 msg="Replaying on-disk memory mappable chunks if any" component=tsdb
prometheus | time=2025-07-26T04:38:20.978Z level=INFO source=head.go:744 msg="On-disk memory mappable chunks replay completed" component=tsdb duration=114.167µs
prometheus | time=2025-07-26T04:38:20.978Z level=INFO source=head.go:752 msg="Replaying WAL, this may take a while" component=tsdb
prometheus | time=2025-07-26T04:38:20.979Z level=INFO source=head.go:825 msg="WAL segment loaded" component=tsdb segment=0 maxSegment=2 duration=904.958µs
prometheus | time=2025-07-26T04:38:20.992Z level=INFO source=head.go:825 msg="WAL segment loaded" component=tsdb segment=1 maxSegment=2 duration=13.122208ms
prometheus | time=2025-07-26T04:38:20.992Z level=INFO source=head.go:825 msg="WAL segment loaded" component=tsdb segment=2 maxSegment=2 duration=109.334µs
prometheus | time=2025-07-26T04:38:20.993Z level=INFO source=head.go:862 msg="WAL replay completed" component=tsdb checkpoint_replay_duration=10.25µs wal_replay_duration=14.356833ms wbl_replay_duration=83ns chunk_snapshot_load_duration=0s mmap_chunk_replay_duration=114.167µs total_replay_duration=14.494209ms
prometheus | time=2025-07-26T04:38:20.996Z level=INFO source=main.go:1309 msg="filesystem information" fs_type=EXT4_SUPER_MAGIC
prometheus | time=2025-07-26T04:38:20.996Z level=INFO source=main.go:1312 msg="TSDB started"
prometheus | time=2025-07-26T04:38:20.996Z level=INFO source=main.go:1497 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
prometheus | time=2025-07-26T04:38:20.997Z level=INFO source=main.go:1537 msg="Completed loading of configuration file" db_storage=958ns remote_storage=1.041µs web_handler=333ns query_engine=542ns scrape=506.208µs scrape_sd=36.875µs notify=1.75µs notify_sd=625ns rules=1.792µs tracing=8.041µs filename=/etc/prometheus/prometheus.yml totalDuration=1.544875ms
prometheus | time=2025-07-26T04:38:20.997Z level=INFO source=main.go:1273 msg="Server is ready to receive web requests."
prometheus | time=2025-07-26T04:38:20.997Z level=INFO source=manager.go:176 msg="Starting rule manager..." component="rule manager"
prometheus.yml が反映されているか http://localhost:9090/targets にアクセスして確認してみます。

job="prometheus" のステータスがupになっているので、問題なさそうです。
※今は prometheus が自分自身(locahost:9090)を監視する job だけが設定されている状態です。
これでprometheusのインストールは完了です。
監視対象の追加
続いて監視対象を追加します。
監視対象である Fedoraサーバー(192.168.0.7)に node exporter をダウンロードします。
root@fedora:~# cd ~
root@fedora:~# mkdir prometheus
root@fedora:~# cd prometheus/
root@fedora:~/prometheus# wget https://github.com/prometheus/node_exporter/releases/download/v1.9.1/node_exporter-1.9.1.linux-arm64.tar.gz
ダウンロードしたファイルを展開します。
root@fedora:~/prometheus# tar xzvf node_exporter-1.9.1.linux-arm64.tar.gz
node_exporter-1.9.1.linux-arm64/
node_exporter-1.9.1.linux-arm64/LICENSE
node_exporter-1.9.1.linux-arm64/NOTICE
node_exporter-1.9.1.linux-arm64/node_exporter
root@fedora:~/prometheus# cd node_exporter-1.9.1.linux-arm64/
node exporterを実行します。
root@fedora:~/prometheus/node_exporter-1.9.1.linux-arm64# ./node_exporter
ファイアーウォールの設定を行います。
※Node Exporterのデフォルトポートは 9100 との事。
root@fedora:~# firewall-cmd --add-port=9100/tcp --permanent
success
root@fedora:~# firewall-cmd --reload
success
先ほどDockerコンテナで起動した prometheus の設定ファイルに監視対象を追加します。
% vi prometheus.yml
% cat prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
# 以下を追加
- job_name: 'fedora-node'
static_configs:
- targets: ['192.168.0.7:9100']
prometheusコンテナを再起動する。
% docker compose down
[+] Running 2/2
✔ Container prometheus Removed 0.2s
✔ Network prometheus_default Removed
% docker compose up -d
[+] Running 2/2
✔ Network prometheus_default Created 0.0s
✔ Container prometheus Started
http://localhost:9090/targets にアクセスしてみます。

fedora-node が追加されていました。
これで fedora-node を prometheus から監視する事が可能になります。
さいごに
以上で prometheus で監視環境が構築できました。
次の記事で grafana のコンテナ起動を行います。
Discussion